RcppModules: Manually build/expose C+ classes to R

孤者浪人 提交于 2019-12-07 21:34:04

问题


The goal is to wrap fairly large existing collection of C++ classes to be callable from R. The first approach is to manually define R Reference Classes and call into "SEXP-wrapped" entry points - and this works fine, no problems. Another approach that I'm currently evaluating is RcppModules. I can successfully use it with toy example within R - with Rcpp::SourceCPP. But have troubles with doing this manually. Example:

//---example.cpp

#include "Rcpp.h"

using namespace Rcpp;

class Example
{
  public:
    Example(){};

    SEXP add(SEXP x_, SEXP y_) const
    {
      double x = as<double>(x_);
      double y = as<double>(y_);

      double res = x + y;
      return wrap(res);
    }
};

RCPP_MODULE(Example_Module) {
  class_<Example>( "Example" )
  .constructor()
  .method( "add", &Example::add )
  ;
}

Then build/compile:

g++ "-I...\\R\\win-library\\3.2\\Rcpp\\include" "-I...\\R\\R-3.2.2\\include" -O0 -g3 -Wall -c -fmessage-length=0 -o example.o "..\\example.cpp" 
g++ -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lpthread -Wl,-Bdynamic -lR -shared "-L...\\R\\R-3.2.2\\bin\\x64" -o example.dll example.o -lR 

which produces example.dll with no warnings/errors...

Then, following the commands of Rcpp::SourceCPP (with verbose = TRUE), loading the dll and populating the environment:

`.example` <- dyn.load('example.dll')

library(Rcpp)
populate( Rcpp::Module("Example_Module",`.example`), environment() )

The last call to populate crashes R (within RStudio).

As I understand, additional to the binary, Rcpp must create R code containing generated R class, which is then loaded to environment. Is there a way to see this code? Is it possible (is it suitable) to use RcppModules from outside of R for wrapping large number of C++ classes? And the last question is that existing C++ code is obviously split in header files containing class declarations and source files containing class method implementations. Can RcppModules be applied in this case? - all examples I have seen are in a single cpp file.

Thanks


回答1:


Thanks to @nrussell for engaging you in some clarification here. Your last comment has a few more specific questions I'll answer briefly:

1) Well yes, RCPP_MODULE is always used in a header file. See various examples packages on CRAN and maybe use r-pkg.org to search.

2) Yes. Use a package. See dozens of answers here suggesting a package. DO NOT DO IT BY HAND. Do not write Makefiles manually. Use a package.

3) No canned answer for that.

Look around and study e.g. Rich FitzJohn's RcppR6 which expressively written for the use case of many local classes where Rcpp Modules became too slow at load.

Several of my packages use Rcpp Modules, but these are comparatively small packages.

For extended discussion, subscribe to and post on the rcpp-devel list.



来源:https://stackoverflow.com/questions/33090458/rcppmodules-manually-build-expose-c-classes-to-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!