[Rcpp-devel] passing function
Dirk Eddelbuettel
edd at debian.org
Wed Jan 26 18:33:13 CET 2011
On 26 January 2011 at 08:30, Davor Cubranic wrote:
| On 2011-01-26, at 7:38 AM, Dirk Eddelbuettel wrote:
|
| > cpp <- '
| > int n = as<int>(N);
| > NumericVector numvec(xvec) ;
| > Function f(fun) ;
| > for( int i=0; i<n; i++){
| > numvec = f( numvec ) ;
| > }
| > return numvec ;
| > '
| >
| > # create a C++ function
| > funx <- cfunction(signature(N = "integer" , xvec = "numeric", fun = "function" ),
| > cpp, , Rcpp = TRUE, include = "using namespace Rcpp; ")
| [...]
| > Now for your questions:
| >
| > I see that f is a Function object but what is fun?
| >
| > There is no 'fun' but 'funx' is an R function created by cfunction() [ of the
| > inline package ] containing executable code created by cfunction() from the
| > source code submitted via variable 'cpp'.
|
| I think the question was about the line 'Function f(fun)'.
|
| And the answer is that 'fun' is the second argument of the C++ function, and is an SEXP for the R function passed in. (See the 'signature' call in assignment to 'funx'.) So 'Function f(fun)' creates an instance of Rcpp::Function called 'f' out of the 'fun' SEXP.
Very good catch -- thank you, Davor.
And a fine example of why asking on list is much better than just emailing
Romain or myself.
Dirk
--
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
More information about the Rcpp-devel
mailing list