RcppCore / RcppArmadillo

Rcpp integration for the Armadillo templated linear algebra library
193 stars 56 forks source link

`RCPP_ARMADILLO_RETURN_ANYVEC_AS_VECTOR` in an R package? #411

Closed volemont closed 1 year ago

volemont commented 1 year ago

Hi,

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                 
volemont commented 1 year ago

This is not a bug report. Closing and posting the question on SO: https://stackoverflow.com/questions/75560370/

eddelbuettel commented 1 year ago

Yes, the same way basically -- in front of every #include for RcppArmadillo or alternatively as -D... flag in src/Makevars.