如何使用 RcppArmadillo 在 Rstudio 中构建 Rcpp 包? [英] How to build Rcpp packages within Rstudio using RcppArmadillo?
问题描述
我正在尝试编译一个在 RStudio 中使用 RcppArmadillo 的 Rcpp 包.我只是想编译:
I'm trying to compile a Rcpp package that uses RcppArmadillo within RStudio. I am only trying to compile:
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;
// Implementation of MASS' rmvrnorm()
// [[Rcpp::export]]
arma::mat rmvrnorm_arma2(int n, arma::vec mu, arma::mat sigma) {
int ncols = sigma.n_cols;
arma::mat Y = arma::randn(n, ncols);
return arma::repmat(mu, 1, n).t() + Y * arma::chol(sigma);
}
可以在这里找到:http://gallery.rcpp.org/articles/模拟多元正态/
我已将文件 rmvrnorm_arma.cpp 放在/src/目录中.
I have placed the file rmvrnorm_arma.cpp within the /src/ directory.
RStudio 提供以下错误:
RStudio is providing the following errors:
==> Rcpp::compileAttributes()
==> Rcpp::compileAttributes()
警告消息:以下包被引用使用然而 Rcpp::depends 属性没有在 Depends 和LinkingTo 包描述文件的字段:RcppArmadillo
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
==> Rcmd.exe INSTALL --no-multiarch --with-keep.source Choice38
==> Rcmd.exe INSTALL --no-multiarch --with-keep.source Choice38
- 安装到库F:/Program Files/R/R-3.1.0/library"
- 安装源包'Choice38' ... g++ -m64 -I"F:/PROGRA~2/R/R-31~1.0/include" -DNDEBUG -I"F:/程序文件/R/R-3.1.0/library/Rcpp/include"-I"d:/RCompile/CRANpkg/extralibs64/local/include" -O2 -Wall -mtune=core2 -c RcppExports.cpp -o RcppExports.o** libs RcppExports.cpp:9:1: 错误: 'arma' 没有命名类型 RcppExports.cpp: 在函数 'SEXPREC* Choice38_rmvrnorm_arma(SEXP,SEXP, SEXP)': RcppExports.cpp:16:40: 错误: 'arma' 未在此范围 RcppExports.cpp:16:50: 错误:模板参数 1 是无效的 RcppExports.cpp:16:58: 错误:'mu' 之前的预期初始值设定项RcppExports.cpp:17:50: 错误:参数 1 中的类型/值不匹配'模板结构的模板参数列表Rcpp::traits::input_parameter' RcppExports.cpp:17:50: 错误:
预期一种类型,得到 'arma' RcppExports.cpp:17:58: 错误:预期'sigma' RcppExports.cpp:18:9 之前的初始化程序:错误:'arma' 不是类或命名空间 RcppExports.cpp:18:19: 错误: 预期 ';'前'__result' RcppExports.cpp:19:9: 错误:'__result' 未在此范围使:* [RcppExports.o] 错误 1 警告:正在运行命令'make -f "F:/PROGRA~2/R/R-31~1.0/etc/x64/Makeconf" -f"F:/PROGRA~2/R/R-31~1.0/share/make/winshlib.mk"SHLIB_LDFLAGS='$(SHLIB_CXXLDFLAGS)' SHLIB_LD='$(SHLIB_CXXLD)'SHLIB="Choice38.dll" WIN=64 TCLBIN=64 OBJECTS="RcppExports.ormvrnorm_arma.o"' 状态为 2 错误:包编译失败'Choice38' - 删除F:/Program Files/R/R-3.1.0/library/Choice38"
- installing to library 'F:/Program Files/R/R-3.1.0/library'
- installing source package 'Choice38' ... g++ -m64 -I"F:/PROGRA~2/R/R-31~1.0/include" -DNDEBUG -I"F:/Program Files/R/R-3.1.0/library/Rcpp/include"
-I"d:/RCompile/CRANpkg/extralibs64/local/include" -O2 -Wall -mtune=core2 -c RcppExports.cpp -o RcppExports.o
** libs RcppExports.cpp:9:1: error: 'arma' does not name a type RcppExports.cpp: In function 'SEXPREC* Choice38_rmvrnorm_arma(SEXP,
SEXP, SEXP)': RcppExports.cpp:16:40: error: 'arma' was not declared in
this scope RcppExports.cpp:16:50: error: template argument 1 is
invalid RcppExports.cpp:16:58: error: expected initializer before 'mu'
RcppExports.cpp:17:50: error: type/value mismatch at argument 1 in
template parameter list for 'template struct
Rcpp::traits::input_parameter' RcppExports.cpp:17:50: error:
expected a type, got 'arma' RcppExports.cpp:17:58: error: expected initializer before 'sigma' RcppExports.cpp:18:9: error: 'arma' is not a class or namespace RcppExports.cpp:18:19: error: expected ';' before '__result' RcppExports.cpp:19:9: error: '__result' was not declared in this scope make: * [RcppExports.o] Error 1 Warning: running command 'make -f "F:/PROGRA~2/R/R-31~1.0/etc/x64/Makeconf" -f "F:/PROGRA~2/R/R-31~1.0/share/make/winshlib.mk" SHLIB_LDFLAGS='$(SHLIB_CXXLDFLAGS)' SHLIB_LD='$(SHLIB_CXXLD)' SHLIB="Choice38.dll" WIN=64 TCLBIN=64 OBJECTS="RcppExports.o rmvrnorm_arma.o"' had status 2 ERROR: compilation failed for package 'Choice38' - removing 'F:/Program Files/R/R-3.1.0/library/Choice38'
以状态 1 退出.
我不知道为什么 Rcpp/Rstudio 拒绝注意 rmvrnorm_arma 的 cpp 文件顶部的包含和依赖声明
I'm not sure why Rcpp / Rstudio is refusing to note the includes and dependency statement at the top of the cpp file that rmvrnorm_arma
如果我尝试使用源加载 .cpp,我在第一次尝试加载时收到以下错误:
If I try to load the .cpp using source I receive the following error on the FIRST attempt to load it:
Rcpp::sourceCpp('rmvrnorm_arma.cpp') 警告消息:在normalizePath(path.expand(path), winslash, mustWork) :
路径[1]="F:/Documents/BoxSync/Choice Project/R Scripts/RcppScripts/RcppArmadillo MCMC/Choice38/src/../inst/include":系统找不到指定的路径
Rcpp::sourceCpp('rmvrnorm_arma.cpp') Warning message: In normalizePath(path.expand(path), winslash, mustWork) :
path[1]="F:/Documents/BoxSync/Choice Project/R Scripts/Rcpp Scripts/RcppArmadillo MCMC/Choice38/src/../inst/include": The system cannot find the path specified
在第二次尝试加载时,源被选中.
On the second attempt of loading it, the source is picked up.
在 Rstudio 中开始以 rcpp 包模式进行开发之前,我对 sourceCpp() 没有任何问题.
Prior to starting to develop in rcpp package mode within Rstudio, I did not have any issue with sourceCpp().
推荐答案
[[Rcpp::depends(...)]]
功能用于 sourceCpp()
等,对于包,您应该使用 Imports:
和 Depends:
等 pp 字段来设置包.
The [[Rcpp::depends(...)]]
functionality is for sourceCpp()
et al, for packages you should use Imports:
and Depends:
etc pp fields to set up a package.
我相信现在 RStudio 存在小错误/麻烦,因为您可能需要交换描述中的字段以消除第一个警告.但它的工作方式不同.
I believe there is minor bug / nuisance with RStudio right now as you may need to swap fields in DESCRIPTION to get rid of the first warning. But it works otherwise.
考虑使用我们提供的函数设置一个包:RcppArmadillo.package.skeleton()
并从那里开始.
Consider setting up a package with the function we provide: RcppArmadillo.package.skeleton()
and start from there.
当然,另一种方法是使用 RcppArmadillo 加载 65 个 CRAN 包中的任何一个并在 RStudio 中构建它,然后比较它们的设置(基本上:依赖/导入和 LinkingTo,以及正确的 NAMESPACE 文件)到您当前拥有的内容,因此看看您缺少什么.
And the other approach, of course, would be to load any of the sixty-five CRAN packages using RcppArmadillo and building it inside RStudio to then compare their setup (basically: Depends/Imports and LinkingTo, along with correct NAMESPACE file) to what you currently have and so see what you are lacking.
这篇关于如何使用 RcppArmadillo 在 Rstudio 中构建 Rcpp 包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!