RcppArmadillo 和 arma 命名空间

标签 r rcpp armadillo

开始有 R 经验,但完全是 C++ 新手,我用 RcppArmadillo 编写了一些函数,并且对它的可用性和速度非常热情。我现在想使用函数 RcppArmadillo.package.skeleton() 将这些函数变成一个包。

只要我在每个 Armadillo 对象(mat、colvec 等)之前明确使用 arma:: 前缀,就可以正常工作。但是,如果我将 using namespace arma; 放在我的 cpp 文件的开头,然后省略 arma::,我将无法加载新创建的包并出现大量错误,这些错误表明无法识别 Armadillo 命名空间。

非常感谢有关如何解决此问题的任何帮助/建议。谢谢,

法比安

PS:我在 Windows 7 和 Ubuntu 12.04 上都尝试了上述方法,在每种情况下都使用 R 3.0.2 和 RcppArmadillo_0.4.000.4。

PS2:随附的 cpp 文件(紧随其后 http://gallery.rcpp.org/articles/simulate-multivariate-normal/ )说明了我的观点。如果我通过 sourceCpp(来自 Rcpp 包)将它输入到 R 中,它工作得很好,但是当我尝试将它包含在一个新包中时会导致上面提到的问题。

// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
using namespace Rcpp;
using namespace arma;

// [[Rcpp::export]]
colvec mvndrawC(colvec mu, mat sig) {

double k = mu.size();
colvec aux = as<colvec>(rnorm(k));
mat csig = chol(sig).t();
colvec out = mu + csig*aux;
return(out);

}

编辑:详细信息

这是我执行以下操作时得到的错误输出:
  • 运行命令 RcppArmadillo.package.skeleton("test2") ,从而为新包“test2”
  • 创建文件夹
  • 将以上代码粘贴到.cpp文件中,复制到新的test2/src文件夹
  • 尝试加载新的 test2 包,通过从 load_all("test2")
  • 调用 devtools
    错误消息(在 Rstudio 中)
    Loading test2
    Re-compiling test2
    '/usr/lib/R/bin/R' --vanilla CMD INSTALL '/home/fabian/test2' --library='/tmp    
    /RtmplfAET0'  \
    --no-R --no-data --no-help --no-demo --no-inst --no-docs --no-exec --no-multiarch  \
    --no-test-load --preclean 
    
    * installing *source* package 'test2' ...
    ** libs
    g++ -I/usr/share/R/include -DNDEBUG   -I"/home/fabian/R/x86_64-pc-linux-gnu-library   
    /3.0/Rcpp/include" -I"/home/fabian/R/x86_64-pc-linux-gnu-library/3.0/RcppArmadillo  
    /include"  -UNDEBUG -Wall -pedantic -g -O0 -fpic  -O3 -pipe  -g  -c RcppExports.cpp -o 
    RcppExports.o
    RcppExports.cpp:10:1: error: 'colvec' does not name a type
    RcppExports.cpp: In function 'SEXPREC* test2_mvndrawC(SEXP, SEXP)':
    RcppExports.cpp:16:40: error: 'colvec' was not declared in this scope
    RcppExports.cpp:16:40: note: suggested alternative:
    /home/fabian/R/x86_64-pc-linux-gnu-library/3.0/RcppArmadillo/include/armadillo_bits   
    /typedef_mat.hpp:65:22: note:   'arma::colvec'
    RcppExports.cpp:16:47: error: template argument 1 is invalid
    RcppExports.cpp:16:55: error: expected initializer before 'mu'
    RcppExports.cpp:17:40: error: 'mat' was not declared in this scope
    RcppExports.cpp:17:40: note: suggested alternative:
    /home/fabian/R/x86_64-pc-linux-gnu-library/3.0/RcppArmadillo/include/armadillo_bits   
    /typedef_mat.hpp:63:22: note:   'arma::mat'
    RcppExports.cpp:17:44: error: template argument 1 is invalid
    RcppExports.cpp:17:52: error: expected initializer before 'sig'
    RcppExports.cpp:18:16: error: expected ';' before '__result'
    RcppExports.cpp:19:9: error: '__result' was not declared in this scope
    make: *** [RcppExports.o] Error 1
    ERROR: compilation failed for package 'test2'
    * removing '/tmp/RtmplfAET0/test2'
    Error: Command failed (1)
    In addition: Warning message:
    The following packages are referenced using Rcpp::depends attributes however are  
    not listed in the Depends and LinkingTo fields of the package DESCRIPTION file: 
    RcppArmadillo 
    

    PS3: 如果我删除文件开头的 // [[Rcpp... 行,末尾的警告消息就会消失(但是我无法通过 sourceCpp 获取它,所以我让它进来了)。

    最佳答案

    我相信这是因为 Rcpp 属性,它将您制作的 C++ 源文件编译成 RcppExports.cpp , 不复制 using namespace arma;语句到那里。

    这很棘手,因为不同的文件可能使用不同的命名空间,因此属性解析器不能只复制所有 using namespace ...声明成RcppExports.cpp -- 它只是默认使用 Rcpp命名空间。如果我们只是复制所有 using namespace声明随意进入 RcppExports.cpp肯定会有冲突。

    无论如何,修复方法是明确前缀 arma::或者您可以修改 RcppExports.cpp文件具有 using namespace arma;在顶部(但每次调用后都必须这样做 compileAttributes() )。

    关于RcppArmadillo 和 arma 命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21944695/

    相关文章:

    Rcpp 没有匹配的函数来调用abs

    R 函数和 Rcpp 函数计算最近正定矩阵的结果不同

    c++ -/usr/include 不能作为 xcode 中的搜索路径?

    sql - 将 MySQL 转储导入 R(不需要 MySQL 服务器)

    r - 为什么要赋值给引用(:=) give different results when running code say two or three times within data. 表格格式?

    引用 dplyr 中的一系列列

    在 R 中保留之前的日期

    r - 无法使 RcppZiggurat 种子设置发挥作用

    Rcpp 和 CULA : segmentation fault

    c++ - 尝试使用 RcppArmadillo 编写 setdiff() 函数会出现编译错误