RcppModules:手动构建/向 R 公开 C+ 类 [英] RcppModules: Manually build/expose C+ classes to R

查看:42
本文介绍了RcppModules:手动构建/向 R 公开 C+ 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标是包装相当大的现有 C++ 类集合,以便从 R 调用.第一种方法是手动定义 R 引用类并调用SEXP 包装"入口点 - 这工作正常,没有问题.我目前正在评估的另一种方法是 RcppModules.我可以成功地将它与玩具示例 in R 一起使用 - 使用 Rcpp::SourceCPP.但是在手动执行此操作时遇到麻烦.示例:

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 )
  ;
}

然后构建/编译:

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 

产生 example.dll 没有警告/错误...

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

然后,按照Rcpp::SourceCPP的命令(使用verbose = TRUE),加载dll并填充环境:

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() )

最后一次调用 populate 使 R 崩溃(在 RStudio 中).

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

据我所知,除了二进制文件之外,Rcpp 还必须创建包含生成的 R 类的 R 代码,然后将其加载到环境中.有没有办法看到这个代码?是否可以(是否合适)使用 R 外部的 RcppModules 来包装大量 C++ 类?最后一个问题是,现有的 C++ 代码显然分为包含类声明的头文件和包含类方法实现的源文件.在这种情况下可以应用 RcppModules 吗?- 我看到的所有示例都在一个 cpp 文件中.

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.

谢谢

推荐答案

感谢 @nrussell 在这里对您进行一些澄清.您的最后一条评论有一些更具体的问题,我将简要回答:

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

1) 是的,RCPP_MODULE 总是在头文件中使用.在 CRAN 上查看各种示例包,也许可以使用 r-pkg.org 进行搜索.

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) 是的.使用一个包.在这里看到几十个建议一个包的答案.不要手工操作.不要手动编写 Makefile.使用包.

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) 对此没有预设答案.

3) No canned answer for that.

环顾四周并学习,例如Rich FitzJohn 的 RcppR6明确地为许多本地类的用例编写,其中 Rcpp模块在加载时变得太慢.

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.

我的几个包使用 Rcpp 模块,但这些是相对较小的包.

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

要进行扩展讨论,请订阅 rcpp-devel 列表并在其上发帖.

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

这篇关于RcppModules:手动构建/向 R 公开 C+ 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆