RCPP导出模块未暴露 [英] Rcpp exported module not exposed

查看:97
本文介绍了RCPP导出模块未暴露的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为multicool的R包,它可以处理多集的排列.当前,在内部,存在一个C ++类,对initMC的调用将创建一个新类Multicool,该对象可以执行我需要它做的所有事情.但是,没有简单的方法来释放分配给该对象的内存.对于简单的使用来说并不重要,但是我有一个应用程序可能会调用数十万次.

I have an R package called multicool which handles permutations of multisets. Currently, internally, there exists a C++ class, and a call to initMC creates a new object of class Multicool which then can do all the things I need it to do. However, there is no simple way to release the memory allocated to this object. It doesn't matter for simple uses, but I have an application which might call this hundreds of thousands of times.

我认为,解决方案是使用Rcpp模块将类公开给R.但是,我尝试按照说明进行操作,但出现错误:

The solution, I think, is to expose the class to R using an Rcpp Module. However, I have tried following the instructions and I get the error:

错误:找不到对象'mcModule'

Error: object 'mcModule' not found

最初-我只想公开该对象及其构造函数.这是我的班级定义

Initially - I would just like to expose the object and its constructor. This is my class definition

#include <Rcpp.h>

using namespace Rcpp;
using namespace std;

class Multicool{
  struct list_el {
    int v;
    struct list_el * n;
  };
  typedef struct list_el item;

  item *h;
  item *t;
  item *i;

  int *m_pnInitialState; 
  int *m_pnCurrState;
  int m_nLength;
  bool m_bFirst;

  public:
   // constructor
  Multicool(IntegerVector x){
    int nx = (int)x.size();
  }
};

然后我用

RCPP_MODULE(mcModule){
  using namespace Rcpp;

  class_<Multicool>("Multicool")

  .constructor<IntegerVector>()
  ;
}

我已经添加了行

import(Rcpp)

到我的NAMESPACE文件

to my NAMESPACE file

我添加了

RcppModules: mcModule

到我的描述文件

并且我在.onLoad函数中添加了对loadRcppModules的调用

and I have added a call to loadRcppModules in the .onLoad function

.onLoad <- function(libname, pkgname) {
  loadRcppModules()
}

所有这些都可以编译,并且程序包的构建没有任何抱怨.但是当我创建一个新的Multicool对象时,出现了上述错误

All of this compiles and the package builds without complaint. But when I got to create a new Multicool object I get the aforementioned error

> library(multicool)
> Multicool = mcModule$Multicool
Error: object 'mcModule' not found

任何帮助或建议将不胜感激

Any help or advice would be appreciated

推荐答案

通常,您通常需要先调用new.在我的RcppRedis包中看到:

You generally need a new call first. See in my RcppRedis package:

RCPP_MODULE(Redis) {
    Rcpp::class_<Redis>("Redis")   

        .constructor("default constructor")  
        [...stuff omitted for brevity...]

    ;
}

R代码(例如,在demo/目录中)的位置

where the R code (eg in the demo/ directory) does

suppressMessages(library(RcppRedis))

redis <- new(Redis)

之后,您可以访问模块中的功能redis$foo()等.

after which you can access functions redis$foo() etc that are part of the module.

所以尝试添加

mcModule <- new(mcModule)

在访问mcModule$Multicool之前.

好像您错过了loadModule("mcModule", TRUE)呼叫.

Looks like you were missing the loadModule("mcModule", TRUE) call.

为了更加明确,我将文件从软件包添加到(正在工作的)testRcppModule中,并对您所做的一项更改(更改为DESCRIPTION),然后进行了另一项更改.一个R文件来加载模块:

Edit 2: To be more explicit, I added your file to the (working) testRcppModule from the package and made the one change you made (to DESCRIPTION) and one more to an R file to load the module:

edd@max:/tmp/rcpp/module$ diff -ru ~/git/rcpp/inst/unitTests/testRcppModule/  testRcppModule/ 
diff -ru /home/edd/git/rcpp/inst/unitTests/testRcppModule/DESCRIPTION testRcppModule/DESCRIPTION
--- /home/edd/git/rcpp/inst/unitTests/testRcppModule/DESCRIPTION        2015-08-26 15:53:03.891830292 -0500
+++ testRcppModule/DESCRIPTION  2015-10-22 21:34:23.716959638 -0500
@@ -10,6 +10,6 @@
 LazyLoad: yes
 Depends: methods, Rcpp (>= 0.8.5)
 LinkingTo: Rcpp
-RcppModules: RcppModuleWorld, stdVector, NumEx
+RcppModules: RcppModuleWorld, stdVector, NumEx, mcModule
 Packaged: 2010-09-09 18:42:28 UTC; jmc

diff -ru /home/edd/git/rcpp/inst/unitTests/testRcppModule/R/zzz.R testRcppModule/R/zzz.R
--- /home/edd/git/rcpp/inst/unitTests/testRcppModule/R/zzz.R    2015-08-26 15:53:03.891830292 -0500
+++ testRcppModule/R/zzz.R      2015-10-22 21:41:41.468532838 -0500
@@ -8,4 +8,5 @@
 loadModule("RcppModuleNumEx", TRUE)
 loadModule("RcppModuleWorld", TRUE)
 loadModule("stdVector", TRUE)
+loadModule("mcModule", TRUE)

Only in testRcppModule/src: multicool.cpp
edd@max:/tmp/rcpp/module$ 

有了这些,一切都很好:

With that, all is good:

$ r --package testRcppModule --eval 'm <- new(mcModule); print(m)'
C++ object <0x757d18> of class 'mcModule' <0x1adeab0>
$ 

这篇关于RCPP导出模块未暴露的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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