繁体   English   中英

sourceCpp和Rcpp.package.skeleton之间的区别

[英]difference between sourceCpp and Rcpp.package.skeleton

#include <Rcpp.h>
using namespace Rcpp;


// [[Rcpp::export]]
NumericVector condSumRcpp(NumericVector x, NumericVector y, LogicalVector z) {

  int n = x.length();
  int sumLength = sum(z);

  NumericVector res(sumLength);
  int j=0;

  for(int i = 0; i < n; i++) {
    if (!z[i]) continue;
    res[j] = x[i] + y[i];
    j+=1;
  }

  return wrap(res);
 }

当我使用sourceCpp时,这工作正常。 但是当我使用

Rcpp.package.skeleton("testRcpp",
  example_code = FALSE,
  cpp_files="utils.cpp",
  module=FALSE
)

并使用R CMD INSTALL --build testRcpp构建软件包R CMD INSTALL --build testRcpp我得到

Error in .Call("testRcpp_condSumRcpp", PACKAGE = "testRcpp", x, y, z) : 
  "testRcpp_condSumRcpp" not available for .Call() for package "testRcpp"

如果我用相同的方法

// [[Rcpp::export]]
NumericVector colMaxRcpp(NumericMatrix X) {

    int ncol = X.ncol();
    NumericVector res(ncol);

    for (int col = 0; col < ncol; col++){
        res[col]=Rcpp::max(X(_, col)); 
    }

    return wrap(res);
}

然后就可以了!

您不能将Attributes和Rcpp.package.skeleton()随机混合。

通常,我通常首先创建一个空包(通过Rcpp.package.skeleton()或RStudio中的相应功能),然后添加我的函数。 调用compileAttributes() ,您应该会很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM