从R开始经历,但是对C++来说是一个完整的新手,我用RcppArmadillo编写了一些函数,并且对它的可用性和速度非常热衷.我现在想使用该函数将函数转换为包RcppArmadillo.package.skeleton().
只要我arma::在每个Armadillo对象(mat,colvec等)之前明确使用前缀,这都可以.但是,如果我放在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源自R via (来自Rcpp包),它可以很好地工作,但当我尝试将它包含在新包中时会导致上述问题.
// [[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);
}
Run Code Online (Sandbox Code Playgroud)
编辑:详细信息
这是我执行以下操作时得到的错误输出:
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
Run Code Online (Sandbox Code Playgroud)
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())。