[Rcpp-devel] function within a function

Marie Auger-Methe marie.augermethe at gmail.com
Wed May 2 16:49:58 CEST 2012


Dear list,

Still pretty new to Rcpp and C++. I am trying to make a package that has 
multiple Rcpp functions. Some of the Rcpp functions call other Rcpp 
functions and I am wondering what is the most efficient method to handle 
this situation. Here is a example of how I am handling the function 
within function (examplified using inline rather than .cpp and .h file):

suppressMessages(library(inline))
f1src <- '
   NumericVector x(1);
   x = 9;
   return x;
'
f1 <- cxxfunction(signature(),  body = f1src, plugin = "Rcpp")
f1()

f2src <- '
   Function fx1(f1);
   NumericVector x = fx1();
   x[0] = x[0] + 1;
   return x;
'
f2 <- cxxfunction(signature(f1 = "function"),  body = f2src, plugin = 
"Rcpp")
f2(f1)

I was wondering whether there are more efficient ways to do this. I 
could think of 3 potential options but I cannot make them work.
1. Include a call to the header of function f1 in the .cpp file of 
function f2 or include both functions in one set of header and .cpp 
files? But I'm not sure how to do this properly.
2. Using Rcpp Module? But from what I understand the modules mainly help 
to remove the .Call in the R session not changing things in the Rcpp 
code. But maybe I'm wrong?
3. Defining the function directly within the other Rcpp function? 
Something like (this doesn't work):
suppressMessages(library(inline))
f3src <-'
   NumericVector f1 (){
     NumericVector x = 9;
     return x;
   }

   NumericVector y = f1();
   y[0] = y[0] + 1;
   return y;
'
f3 <- cxxfunction(signature(),  body = f3src, plugin = "Rcpp")

Any help will be appreciated!

Marie






More information about the Rcpp-devel mailing list