title: How to avoid reading data from R environment within Rcpp function
body: <p>Even though <code>MyCppFunction(NumericVector x)</code> returns the desired output, I am not sure of a proper/efficient way to avoid reading the data on variable <code>myY</code> withou passing it as a function argument.</p>
  
  <p>The reason I do not pass the data as an argument is that I will eventually pass the C++ function as an objective function to minimize and the minimization routine accepts a function of one argument only, namely <code>myX</code>. <s>Just as an example: in R, I would pass <code>myY</code> to <code>optim(...)</code> in the following way : <code>optim(par,fn=MyRFunction,y=myY)</code>.</s></p>
  
  <p>Any advice on how to properly access <code>myY</code> from within the C++ function is appreciated, here's a minimal example of what I am afraid is a really wrong way to do it:</p>
  
  <p><strong>Update</strong> : I've modified the code to better reflect the context as well as what has been proposed in the answers. Just in case, the focus of my question lies on this line : <code>NumericVector y = env["myY"];  // How to avoid this?</code></p>
  
  <pre><code>#include &lt;Rcpp.h&gt;
  using namespace Rcpp;
  
  // [[Rcpp::export]]
  double MyCppFunction(NumericVector x) {
  
    Environment env = Environment::global_env();
    NumericVector y = env["myY"];  // How to avoid this?
  
    double res = 0;
  
    for (int i = 0; i &lt; x.size(); i++) res = res + (x(i) * y(i));
  
    return res;
  }
  
  double MyCppFunctionNoExport(NumericVector x) {
  
    Environment env = Environment::global_env();
    NumericVector y = env["myY"];  // How to avoid this?
  
    double res = 0;
  
    for (int i = 0; i &lt; x.size(); i++) res = res + (x(i) * y(i));
  
    return res;
  }
  
  // [[Rcpp::export]]
  double MyCppFunction2(NumericVector x, NumericVector y) {
    double res = 0;
  
    for (int i = 0; i &lt; x.size(); i++) res = res + (x(i) * y(i));
  
    return res;
  }
  
  // [[Rcpp::export]]
  double MyRoutine(NumericVector x, Function fn) {
  
    for (int i = 0; i &lt; x.size(); i++) fn(x);
  
    return 0;
  }
  
  // [[Rcpp::export]]
  double MyRoutineNoExport(NumericVector x) {
  
    for (int i = 0; i &lt; x.size(); i++) MyCppFunctionNoExport(x);
  
    return 0;
  }
  
  /*** R
  MyRFunction &lt;- function(x, y=myY) {
    res = 0
    for(i in 1:length(x)) res = res + (x[i]*y[i])
    return (res)
  }
  
  callMyCppFunction2 &lt;- function(x) {
     MyCppFunction2(x, myY)
  }
  
  set.seed(123456)
  
  myY = rnorm(1e3)
  myX = rnorm(1e3)
  
  all.equal(MyCppFunction(myX), MyRFunction(myX), callMyCppFunction2(myX))
  
  require(rbenchmark)
  
  benchmark(MyRoutine(myX, fn=MyCppFunction),
            MyRoutine(myX, fn=MyRFunction),
            MyRoutine(myX, fn=callMyCppFunction2),
            MyRoutineNoExport(myX), order="relative")[, 1:4]
  
  */
  </code></pre>
  
  <p><strong>Output</strong>:</p>
  
  <blockquote>
  <pre><code>$ Rscript -e 'Rcpp::sourceCpp("stack.cpp")'
  &gt; MyRFunction &lt;- function(x, y = myY) {
  +     res = 0
  +     for (i in 1:length(x)) res = res + (x[i] * y[i])
  +     return(res)
  + }
  
  &gt; callMyCppFunction2 &lt;- function(x) {
  +     MyCppFunction2(x, myY)
  + }
  
  &gt; set.seed(123456)
  
  &gt; myY = rnorm(1000)
  
  &gt; myX = rnorm(1000)
  
  &gt; all.equal(MyCppFunction(myX), MyRFunction(myX), callMyCppFunction2(myX))
  [1] TRUE
  
  &gt; require(rbenchmark)
  Loading required package: rbenchmark
  
  &gt; benchmark(MyRoutine(myX, fn = MyCppFunction), MyRoutine(myX, 
  +     fn = MyRFunction), MyRoutine(myX, fn = callMyCppFunction2), 
  +     MyRoutineNoEx .... [TRUNCATED] 
                                       test replications elapsed relative
  4                  MyRoutineNoExport(myX)          100   1.692    1.000
  1      MyRoutine(myX, fn = MyCppFunction)          100   3.047    1.801
  3 MyRoutine(myX, fn = callMyCppFunction2)          100   3.454    2.041
  2        MyRoutine(myX, fn = MyRFunction)          100   8.277    4.892
  </code></pre>
  </blockquote>