Reputation: 3207
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.
Upvotes: 4
Views: 271
Reputation: 17642
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 );
}
Upvotes: 7