Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to use c++ code from R package FOO in package BAR

Tags:

c++

r

rcpp

I am trying to define a function using Rcpp for speedup. The situation is as follows:

  • I have a package FOO with a lot of C++ code (my own package and currently not using Rcpp) which have defined a set of functions e.g. foo_a and foo_b.
  • In another package BAR (using Rcpp) I am defining a function (using Rcpp Attributes) where I want to call functions foo_a and foo_b.

How do I solve this? Looking a bit in other posts I get that I have in some way to include header files in FOO and use attribute // [[Rcpp::depends(FOO)]] in BAR, but I seem to miss some points. Any hints on how to do it?

Best Lars

EDIT: Thanks for the comments I liked Kevin Usheys approach and tried to implement it. However, after some coding I realized that I actually don't need functions from FOO but a class and its public functions. I guess that I cannot do the tricks you suggested for a class. I ended up putting the source files of the class from FOO in the BAR src dir (not the best approach since I now have 2 versions of the same code). However for the moment this hack work for me.

like image 480
Relund Avatar asked Jan 07 '14 21:01

Relund


1 Answers

I would recommend one of these options:

If foo_a and foo_b are simple enough, just have them as inline functions in headers of FOO and put these headers in FOO/inst/include/FOO.h. Rcpp::depends(FOO) will then include this file when you invoke compileAttributes (or perhaps load_all) on BAR.

Otherwise, consider registering the functions using R's registration model. this is a bit more work, but that's bearable. Writing R extensions has the details. I would suggest putting all the registration logic in FOO so that the client package BAR only has to use it. For example, I'd have foo_a like this in FOO's headers, e.g. in FOO/inst/include/FOO.h:

#ifdef COMPILING_FOO
inline int foo_a(int x, double y, const std::string& z){
    typedef int (*Fun)(int, double, const std::string&) ;
    static Fun fun = (Fun)R_GetCCallable( "FOO", "foo_a" ) ;
    return fun(x,y,z) ;
}
#else 
// just declare it
int foo_a(int x, double y, const std::string& z) ;
#endif

and the actual definition of foo_a in some .cpp file in FOO/src:

#define COMPILING_FOO
#include <FOO.h>

int foo_a(int x, double y, const std::string& z){
   // do stuff
}

Then, you need to register foo_a using R_RegisterCCallable in the function R_init_FOO:

extern "C" void R_init_FOO( DllInfo* info ){
    R_RegisterCCallable( "FOO", "foo_a", (DL_FUNC)foo_a );
}
like image 168
Romain Francois Avatar answered Sep 21 '22 16:09

Romain Francois