Search code examples
rrcpprcpparmadillo

How to use RCPP_ARMADILLO_RETURN_ANYVEC_AS_VECTOR in an R package?


How do I use RCPP_ARMADILLO_RETURN_ANYVEC_AS_VECTOR in an R package?

File veccany_as_v_test.cpp:

// [[Rcpp::depends(RcppArmadillo)]]
#define RCPP_ARMADILLO_RETURN_ANYVEC_AS_VECTOR
#include <RcppArmadillo.h>

using namespace Rcpp;

// [[Rcpp::export]]
arma::vec veccany_as_v_test(const arma::vec& v) { return(v); }

Reprex:

x <- 1 : 5

Rcpp::sourceCpp("veccany_as_v_test.cpp")

# as expected
veccany_as_v_test(x)
#[1] 1 2 3 4 5

# create basic package
RcppArmadillo::RcppArmadillo.package.skeleton("veccany")
file.copy("veccany_as_v_test.cpp", to = "veccany/src/veccany_as_v_test.cpp")
Rcpp::compileAttributes("veccany")
install.packages("veccany", repos = NULL, type = "source")

# not as expected
veccany::veccany_as_v_test(x)
#     [,1]
#[1,]    1
#[2,]    2
#[3,]    3
#[4,]    4
#[5,]    5

Output sessionInfo():

R version 4.2.2 (2022-10-31)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Ventura 13.2.1

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib

locale:
[1] C/UTF-8/C/C/C/C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_4.2.2           tools_4.2.2              RcppArmadillo_0.12.0.1.0
[4] Rcpp_1.0.10              veccany_1.0                 

Solution

  • You have to ensure it is set before each #include of RcppArmadillo. One way to ensure that is via -D... in src/Makevars. (The trick is we also need to have the effect on src/RcppExports.cpp. There is custom way via an include file -- but using -D... is easier.)

    Simple and quick example:

    Create a package.

    We can call RcppArmadillo.package.skeleton(). I use a handy wrapper from my littler package.

    $ kitten.r --type arma armademo
    

    Modify the file(s)

    Here I edit src/rcpparma_hello_world.cpp, and for good measure I include your vecany() function source code.

    Remember to add PKG_CPPFLAGS=-DRCPP_ARMADILLO_RETURN_ANYVEC_AS_VECTOR in src/Makevars (and src/Makevars.win for Windows).

    Build and install

    Remember to call compileAttributes() (when adding / altering a function (interface)), and then install it. You should see the -D... on the compilation line.

    Test it

    $ Rscript -e 'armademo:::veccany_as_v_test(c(2,3,4,42,99))'
    [1]  2  3  4 42 99
    $ 
    

    I will commit this in a minute to my code from stackoverflow answers repo so you have the full example there. [Done. Files are here.]