Creating Packages Using Rcpp, Attributes Are Not Handled Correctly

I played with the setup of the R package, the purpose of which is to use Rcpp in RStudio, but I'm struggling to get things to work correctly with Rcpp attributes.

My understanding of how this works is pretty slight, but my understanding is this:

  • In C ++ source files, you can add Rcpp attributes, for example, the tag // [[Rcpp::export]] marks the C ++ function for export, which makes it accessible to R.
  • When you create the package, Rcpp then generates the corresponding C ++ code in the RcppExports.cpp file and the wrappers in the R source file RcppExports.R .

This does not work properly (as I expect) when I create my package. Roxygen does not play well with this when creating the NAMESPACE file (so I disabled this). The // [[Rcpp::export]] tag apparently just marks a function for export to R, and not also for marking a function for export to the package namespace.

More importantly, the attribute tag Rcpp // [[Rcpp::depends()]] is not processed correctly. If I copy the code here to a new source file and rebuild my package, gcc throws errors in the RcppExports.cpp file, saying that the BigMatrix identifier is not declared, which indicates that the tage attribute // [[Rcpp::depends(bigmemory)]] not handled correctly.

Since several things do not work the way I expected, what I miss in my understanding of Rcpp attribute tags?

+6
source share
2 answers

This is a problem with the generated RcppExports.cpp file. There is currently no way to teach him to include header files from another place, so he just does not include bigmemory/MatrixAccessor.hpp .

The workaround is to do this:

 #include <Rcpp.h> #include <bigmemory/MatrixAccessor.hpp> Rcpp::NumericVector BigColSums(Rcpp::XPtr<BigMatrix> pBigMat) { // Create the matrix accessor so we can get at the elements of the matrix. MatrixAccessor<double> ma(*pBigMat); // Create the vector we'll store the column sums in. Rcpp::NumericVector colSums(pBigMat->ncol()); for (size_t i=0; i < pBigMat->ncol(); ++i) colSums[i] = std::accumulate(ma[i], ma[i]+pBigMat->nrow(), 0.0); return colSums; } // [[Rcpp::export]] Rcpp::NumericVector BigColSums( SEXP pBigMat ){ return BigColSums( Rcpp::XPtr<BigMatrix>( pBigMat) ) ; } 

To capture a type in a .cpp file, RcppExports.cpp needs to know only SEXP .

+4
source

// [[Rcpp::export]] does not export functions to the NAMESPACE package, it just says "we must make this function available to R" - you don’t care how you want to manage this function in your namespace.

Roxygen works to parse roxygen tags from source file comments; you will need to add the //' @export to the .cpp source files, as described in 3.4 of the vignette attribute .

For your second problem, you still need to make sure that the bigmemory is in your Depends: and LinkingTo: sections of your DESCRIPTION file. I suppose for // [[Rcpp::depends]] it would be possible to automatically modify the DESCRIPTION package DESCRIPTION , but this is quite easy to do yourself.

The RStudio Tips also featured a discussion.

+4
source

Source: https://habr.com/ru/post/952484/


All Articles