Search code examples
rrcpp

Rcpp modules confusion


I'm having trouble getting RCPP_MODULE to work from Rcpp::Rcpp.package.skeleton() and am not sure what I'm missing. I appended the following code to rcpp_hello_world.cpp in my package ab:

class A {                                                                                                                                                                                                                                    
public:                                                                                                                                                                                                                                      
  int foo() {                                                                                                                                                                                                                                
    return 1;                                                                                                                                                                                                                                
  }                                                                                                                                                                                                                                          
};                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                             
RCPP_MODULE(ab_module) {                                                                                                                                                                                                                     
  class_<A>("A").                                                                                                                                                                                                                            
    constructor().                                                                                                                                                                                                                           
    method("foo", &A::foo);                                                                                                                                                                                                                  
}   

I then run Rcpp::compileAttributes() and R CMD build and INSTALL the package. Then:

R> library(ab)
R> Rcpp::Module('ab_module', inline::getDynLib('ab'))
Uninitialized module named "ab_module" from package "ab"
Uninitialized module named "ab_module" from package "~/R/x86_64-pc-linux-gnu-library/3.6/ab/libs/ab.so"
Uninitialized module named "ab_module" from package "FALSE"
Uninitialized module named "ab_module" from package "<pointer: 0x5578d469d0e0>"
Uninitialized module named "ab_module" from package "<pointer: 0x5578d35ff690>"
R> rcpp_hello_world()
[[1]]
[1] "foo" "bar"

[[2]]
[1] 0 1

> sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.4 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=nl_NL.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=nl_NL.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=nl_NL.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=nl_NL.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
[1] ab_1.0

loaded via a namespace (and not attached):
[1] compiler_3.6.3 inline_0.3.15  Rcpp_1.0.5 

How can I initialize the module?

Other Rcpp code does work on this machine but I fail to narrow down the differences between those packages and this code.

I tried putting putting import(Rcpp) or importFrom(Rcpp, evalCpp) in NAMESPACE to no avail.


Solution

  • Two or three quick comments:

    1. It is indeed tricky as Rcpp Modules predates Rcpp Attributes.

    2. Module initialization happens from an R call. Do you have

      loadModule("AB_module", TRUE)

      somewhere? Note that it can be in any file in R/, you are not required to put it in .onLoad().

    3. Updating a package last weekend I also got reminded that you then still may have to add the R_init_ab(DllInfo *dll) call with R_CallMethodDef(). I can probably point you to an example.

    So in sum, we probably needs a simply quickstart vignette for 'how to start a Modules package in 2020'. And/or rewrite the existing vignette.