Evaluate Expressions in Rcpp Environments

I am looking to see if it is possible to have the same functionality as with() in R in Rcpp for environments.

For example, in R, I can create an environment, add two variables, and use with() to evaluate the expression using only variable names:

 e <- new.env() e$x <- 1 e$y <- 2 with(e, x + y ) 

I can do something similar in Rcpp, but this requires indexing the environment:

 f <- cxxfunction(signature(env="environment"), ' Environment e(env); double Res = (double)e["x"] + (double)e["y"]; return(wrap( Res )); ', plugin = "Rcpp" ) f(e) 

Is it possible to evaluate an expression using only variable names in Rcpp? The reason I'm asking is because I want to write some kind of dynamic C ++ function where you can add expressions. For example, with some dummy code that doesn't work:

 f <- cxxfunction(signature(env="environment"), sprintf(' Environment e(env); double Res; // Res = with(e, %s ); return(wrap( Res )); ','x + y'), plugin = "Rcpp" ) 
+6
source share
1 answer

I do not think you can: at compile time your variables are unknown. You need to resort to dynamic search, which is what R. does. In essence, you will need to create a parser for the expression x + y .

+2
source

Source: https://habr.com/ru/post/914703/


All Articles