【问题标题】:Rcpp Class wrap and asRcpp 类包装和作为
【发布时间】:2014-08-27 13:36:11
【问题描述】:

我创建了一个 Rcpp - 带有包含以下类的模块的包:

更新

C++部分:

class A
{
  public:
  void concat( A & x)
  {
   ....
  }
} 

使用 Rcpp 模块 - 部分

RCPP_MODULE(Foo){
 using namespace Rcpp;

 class_<A>("A")
 .method("concat", &A::concat)
 ;
}

R - 部分

x <- new(A)
y <- new(A)

问题

 x$concat(y)

没有已知的从参数 1 'SEXP' 到 'A' 的转换。

我认为问题在于没有专门的 wrap 和 as 函数。 对我来说不清楚,我是否需要为 A 类创建此模板功能? 我需要创建一个 Rcpp:Xptr 吗?

【问题讨论】:

  • 我不确定我是否理解您的问题。您可能想要扩展一点,我建议您查看一些使用模块的现有包和/或从一个非常简单的包开始。
  • @DirkEddelbuettel,希望现在清楚了吗? :) 问候
  • 因为 R 和 Rcpp 不知道 A 是什么类,你必须相应地创建 as&lt;&gt;()wrap()。这通常不涉及XPtr
  • 但是对于 as() 和 wrap() 生成 R 对象(即向量列表、数据帧...)是不可能的。有没有别的办法?
  • 或者你可以使用支持的RCPP_EXPOSED_CLASS宏。

标签: rcpp


【解决方案1】:

您可以使用RCPP_EXPOSED_CLASS(A) 为您生成所有这些内容。有关详细信息,请参阅https://github.com/RcppCore/Rcpp/blob/master/inst/include/Rcpp/macros/module.h

#include <Rcpp.h>
using namespace Rcpp ;

RCPP_EXPOSED_CLASS(A)

class A{
public:

    A(){}    

    void concat( A & x){
        Rprintf( "hello\n") ;
    }
} ;

RCPP_MODULE(Foo){
    using namespace Rcpp;

    class_<A>("A")
       .default_constructor()
       .method("concat", &A::concat)
    ;
}

这给出了:

> sourceCpp( "/tmp/mod.cpp" )
>
> a <- new(A)
> b <- new(A)
> a$concat(b)
hello

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-25
    相关资源
    最近更新 更多