Begin experienced in R but a complete newbie to C++, I wrote some functions with RcppArmadillo and am pretty enthusiastic about both its usability and speed. I'd now like to turn the functions into a package, using the function RcppArmadillo.package.skeleton()
.
This works okay as long as I explicitly use the arma::
prefix before every Armadillo object (mat, colvec etc). However, if I put using namespace arma;
at the beginning of my cpp file, and omit the arma::
afterwards, I cannot load the newly created package and get tons of errors which make clear that the Armadillo namespace is not being recognized.
Any help/advice on how to fix this greatly appreciated. Thanks,
Fabian
PS: I tried the above 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 (loosely following http://gallery.rcpp.org/articles/simulate-multivariate-normal/) illustrates my point. It works nicely if I source it into R via sourceCpp
(from the Rcpp package), but leads to the problems mentioned above when I try to include it in a new package.
// [[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);
}
EDIT: Details
Here's the error output I get when I do the following:
- Run the command
RcppArmadillo.package.skeleton("test2")
, thus creating folders for a new package "test2"
- Paste the above code into a
.cpp
file, and copy it into the new test2/src
folder
- Trying to load the new
test2
package, by calling load_all("test2")
from the devtools
package
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: The warning message at the end disappears if I drop the // [[Rcpp...
line at the beginning of the file (but then I cannot source it via sourceCpp
, so I let it in).