How to build Rcpp packages within Rstudio using RcppArmadillo?

泄露秘密 提交于 2020-03-26 05:46:10

问题


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);
}

which is found here: http://gallery.rcpp.org/articles/simulate-multivariate-normal/

I have placed the file rmvrnorm_arma.cpp within the /src/ directory.

RStudio is providing the following errors:

==> Rcpp::compileAttributes()

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

  • 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'

Exited with status 1.

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

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') 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.

Prior to starting to develop in rcpp package mode within Rstudio, I did not have any issue with sourceCpp().


回答1:


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.

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.

Consider setting up a package with the function we provide: RcppArmadillo.package.skeleton() and start from there.

Edit: 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.



来源:https://stackoverflow.com/questions/23486604/how-to-build-rcpp-packages-within-rstudio-using-rcpparmadillo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!