[R-pkg-devel] moving from C to C++ with Rcpp in R package

THIOULOUSE JEAN Je@n@Th|ou|ou@e @end|ng |rom un|v-|yon1@|r
Wed Jun 1 11:03:48 CEST 2022


Hello

I need some help to add C++ functions using Rcpp to the ade4 package.

The ade4 package (on CRAN) already contains many C functions, mainly to perform randomization tests, and we would like to replace some of these functions by C++ ones using Rcpp, as well as add more new C++ functions, using also Rcpp.

To start with, I have only one C++ function and one R wrapper function, named respectively RV.randtest and RVrandtestCpp. Both functions work fine, using sourceCpp("RVrandtestCpp.cpp") and calling it with RV.randtest in R from the command line, and I also checked that the RcppExamples package compiles without problem with my configuration. Everything is available in https://github.com/thioulouse/RVrandtest

Now, I want to integrate these two functions in the ade4 package, so I have done the following things:

- added Rcpp and RcppArmadillo to the Imports field in the DESCRIPTION file
- added the LinkingTo field, with Rcpp and RcppArmadillo in the DESCRIPTION file
- added importFrom("Rcpp", "sourceCpp") in the NAMESPACE file
- added export("RV.randtest") and export("RVrandtestCpp") in the NAMESPACE file
- added RV.randtest.Rd in the man directory and \alias{RVrandtestCpp} in ade4-internal.Rd
- added RV.randtest.R in the R directory
- added RVrandtestCpp.cpp in the src directory

Then I used the Rcpp compileAttributes() function to add:

- RcppExports.cpp in the src directory
- RcppExports.R in the R directory

But when I check the modified ade4 package (with R CMD BUILD / R CMD CHECK), I get the following notes:

* checking R code for possible problems ... NOTE
RVrandtestCpp: no visible binding for global variable
  ‘_ade4_RVrandtestCpp’
Undefined global functions or variables:
  _ade4_RVrandtestCpp

And when running the examples I get the following error:

> rv1 <- RV.randtest(pca1$tab, pca2$tab, 99)
Error in RVrandtestCpp(X, Y, nrepet) : 
  object '_ade4_RVrandtestCpp' not found
Calls: RV.randtest -> RVrandtestCpp
Execution halted

So I am probably missing something, and I have the following questions:

Do I have to modify the init.c file in the src directory ? It contains the reference to all the C functions called in the ade4 package, but not the C++ function from Rcpp. Should I add it here ? I tried to do it, with no luck.

The NAMESPACE file contains this first line: 
useDynLib(ade4, .registration = TRUE, .fixes = "C_")
The .fixes argument is related to C function calls. Can this raise a problem for calling C++ functions ?

Why is the _ade4_RVrandtestCpp function (defined by compileAttributes() in RcppExports.cpp) not visible in RVrandtestCpp and not found at runtime ?

Sorry for the long message.
Thank you for any help.

Jean

—-
Jean THIOULOUSE - https://lbbe-web.univ-lyon1.fr/en/annuaires-des-membres/thioulouse-jean
https://orcid.org/0000-0001-7664-0598 (ORCID page)
https://www.springer.com/fr/book/9781493988488 (ade4 Book)
https://pbil.univ-lyon1.fr/JTHome/Biblio/ThioulouseEB2021.pdf (Evol. Biol. paper)



More information about the R-package-devel mailing list