[Rcpp-devel] function within a function

Dirk Eddelbuettel edd at debian.org
Wed May 2 17:18:47 CEST 2012


On 2 May 2012 at 15:49, Marie Auger-Methe wrote:
| 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):

Don't use inline for this. It generates randomized function 'names' at the
C++ level at it is tailored for use _from R_ and the C++ function pointer
gets hidden away in an R function.

It is time to think about packages.  Inline is very convenient, but it has
occassional limits. You are hitting one now.

What you want is trivial and can be fixed the standard C / C++ by declaring
the function headers as eg in 

   extern "C" SEXP foo(SEXP a, SEXP b);

   extern "C" SEXP bar(SEXP c, SEXP d);

before you implement the functions.   Now you can call any way you want foo
calling foo, foo calling bar, bar calling foo, bar calling bar, ...

And of course you do not need to use the   SEXP name(SEXP a, SEXP b)
convention use by R.  You only need this when you call from R via .Call(). At
the C++ level you are open to any other (visible) C++ function -- which is
how we call other libraries.

My last piece of advice would be to write a mock C++ solution first. Write a
short main(), set up some data, call one function, have it call another.

Then work out Rcpp packages which you have meant to do for a while now :-)
and wrap it around your initial C++ program, replacing the main() function
which one you can call from R.  And then you're done.


R/Finance 2012 Conference on May 11 and 12, 2012 at UIC in Chicago, IL
See agenda, registration details and more at http://www.RinFinance.com

More information about the Rcpp-devel mailing list