0

I am wondering if there is an easy way to call the pre-assigned values in r when I wrote cppFunction.

Here is my code:

y=45.3
library(Rcpp)
cppFunction(
"NumericVector demo_fun_cpp(double x) {
    NumericVector out1(50);
    out1[0] = 2;
    for (int i=1; i < 50; ++i) {
        out1[i]=x+y+out1[i-1];
    }
    return out1;
}" )

Error: in sourceCpp(code = code, env = env, rebuild = rebuild, cacheDir = cacheDir, : Error 1 occurred building shared library.

I understand if I assign y inside of demo_fun_cpp() function, it will work. However, there are a lot of values that has been assigned in R, so it will be very convenient if I don't have to assign them again.

I am new in C++, I appreciate any help in advance!

RobJan
  • 1,351
  • 1
  • 14
  • 18
Joanna
  • 663
  • 7
  • 21
  • 5
    There are other methods (environment variables, say, or accessing R values) but those are _worse_. Code clarity first. If you need it, pass it through the function signature. – Dirk Eddelbuettel Aug 17 '18 at 16:29
  • 2
    I think you could access to the global environment (assuming the variables are there) but the code is as complex as passing all the neeeded variables to the function... – digEmAll Aug 17 '18 at 16:30
  • Great! I appreciate all suggestions! I will try these solutions! – Joanna Aug 17 '18 at 16:48

0 Answers0