[Rcpp-devel] [Rcpp] How can i call a R function in Cpp source code?

super rcppmail at 163.com
Tue Aug 5 11:30:47 CEST 2014



Yes , maybe that is very close to my needs , sorry for my pool english expression. But, the following code can't work:
double meanC(NumericVector x) {
   Function mean=Environment("package::base")["mean"];
   return mean(x);   
}
with the error "can't convert SEXP to double"
How can i fix it ? Tks.






At 2014-08-05 04:30:32, "Romain Francois" <romain at r-enthusiasts.com> wrote:
Not sure what you mean by hide the R function. You can extract a function by its name on the C++ side : 


Function bazinga("mean") ;


or extract it from a particular environment, e.g.


Environment("package:base")["mean"]



or 


Environment base("package:base") ; 
Function foo = base["mean"]


Romain


Le 5 août 2014 à 09:26, super <rcppmail at 163.com> a écrit :


Hello Rcppusers,
  I want to call  a  r function in Cpp code, e.g.
 int  meanC( NumericVector x) {
    return mean(x,_["na.rm"]=TRUE);
}
Here function mean is a r function not rewrittened  Rcpp version's mean. I do know one way to do this:
 int  meanC( Function f,NumericVector x) {
    return f(x,_["na.rm"]=TRUE);
}
Then  meanC(mean,1:3)
but can i hidden the true r function called ?  Is  r functions packed into some namespace?






_______________________________________________
Rcpp-devel mailing list
Rcpp-devel at lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20140805/602c29e6/attachment.html>


More information about the Rcpp-devel mailing list