Here is a minimal example:
require(Rcpp)
require(inline)
src <- '
Rcpp::Environment glob = Rcpp::Environment::global_env();
glob.assign( "foo" , "function(x) x + 1" );
'
myFun <- cxxfunction(body=src,plugin = "Rcpp")
myFun()
foo
[1] "function(x) x + 1"
Without surprise, what I get is a character variable and not a function.
You need the usual parse/eval
combination to transform the string into an object.
foo <- eval( parse( text = "function(x) x+1") )
foo( 1:10 )
# [1] 2 3 4 5 6 7 8 9 10 11
In Rcpp
, you can use an ExpressionVector
.
// [[Rcpp::export]]
void fun(){
ExpressionVector exp( "function(x) x+1" ) ;
Function f = exp.eval();
Rcpp::Environment glob = Rcpp::Environment::global_env();
glob.assign( "foo" , f );
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With