Start with R, but a complete newbie in C ++, I wrote some functions with RcppArmadillo and is very enthusiastic about its convenience and speed. Now I would like to turn functions into a package using a function RcppArmadillo.package.skeleton().
This works fine if I explicitly use a prefix arma::in front of each Armadillo object (mat, colvec, etc.). However, if I put the using namespace arma;cpp file at the beginning and then lowered it arma::, I could not load the newly created package and get a lot of errors that clearly show that the Armadillo namespace is not recognized.
Any help / advice on how to fix this is greatly appreciated. Thanks,
Fabian
PS: I tried this on both Windows 7 and Ubuntu 12.04, using R 3.0.2 and RcppArmadillo_0.4.000.4 in each case.
PS2: the attached cpp file (weakly following http://gallery.rcpp.org/articles/simulate-multivariate-normal/ ) shows my point. It works fine if I send it to R via sourceCpp(from the Rcpp package), but leads to the problems mentioned above when I try to include it in a new package.
#include <RcppArmadillo.h>
using namespace Rcpp;
using namespace arma;
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);
}
EDIT: Details
Here I get the error output when I do the following:
- Run the command
RcppArmadillo.package.skeleton("test2"), thus creating folders for the new package "test2" - Paste the above code into a file
.cppand copy it to a new foldertest2/src - Trying to download a new package
test2by calling load_all("test2")from a packagedevtools
Error Messages (in 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, ).