Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when building R package using roxygen2

Tags:

r

roxygen2

rcpp

I have 2 files, Rfile.R and Cppfile.cpp.

Contents in Cppfile.cpp:

#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
int CPPF(int k){return ++k;}

Contents in Rfile.R:

RF<-function(k){return(CPPF(k))}

I want to build an R package based on the 2 files. I use the lastest versions of Rstudio and Roxygen2.

I tried 3 ways to build the package with or without Roxygen2, and had different results:

  1. New Project->New Directory->R package->Type:Package w/Rcpp, add both Rfile.R and Cppfile.cpp as source files. Build & reload, everything works out fine. The functions work as they do.

  2. New Project->New Directory->R package->Type:Package w/Rcpp, add both Rfile.R and Cppfile.cpp as source files. Select "Generate documentations with Roxygen", check all its options. Build & Reload, the functions don't work. Inputting "RF" gives the contents of RF, inputting "CPPF" pops "Object not found".

  3. New Project->New Directory->R package->Type:Package w/Rcpp, add only Cppfile.cpp as source files. Select "Generate documentations with Roxygen", check all its options. Build & Reload, the function works. Then copy Rfile.R directly into the project folder->R folder. Build & Reload, everything fine, functions work well.

Am I using the Roxygen wrong or Roxygen has bugs? I need it to document. I can stick to the 3rd way which cost me much energy to find, but wired.

Thanks!

One Way To Solve The Issue: When selecting "Generate documentations with Roxygen", Don't check "NAMESPACE file" option.

like image 937
user2961927 Avatar asked May 26 '14 12:05

user2961927


1 Answers

You're mixing up two things (which are easy to mix up, unfortunately):

First, the // [[Rcpp::export]] attribute is used for auto-generating wrapper functions in two files, RcppExports.cpp and RcppExports.R. A wrapper R function, CPPF, will be auto-generated by Rcpp::compileAttributes() here, and placed into R/RcppExports.R.

Second, roxygen comments can be used to manage the NAMESPACE, e.g. with the @export tag. Note that this is different from // [[Rcpp::export]]!

The auto-generated function is not automatically exported. The Rcpp.package.skeleton() will generate a NAMESPACE file that automatically exports all functions of a given name; ie, the exportPattern("^[[:alpha:]]+") entry. This is good enough for small packages; but as your package gets more complicated you will want more fine-grained control over your namespace. Or you can just adopt a convention where all internal, non-exported functions begin with a .. Either way, this mechanism is what allows the auto-generated function to be exported to your package namespace.

If you want to use roxygen to manage the NAMESPACE, you need to add roxygen comments to your C++ functions if you want them to be exported in the namespace. So you could modify your function as the following:

#include <Rcpp.h>
using namespace Rcpp;

//' @export
// [[Rcpp::export]]
int CPPF(int k){return ++k;}

Note that you might have to run roxygen2::upgradeRoxygen() to ensure that roxygen2 takes over the NAMESPACE, for new versions of roxygen2.

like image 110
Kevin Ushey Avatar answered Sep 27 '22 16:09

Kevin Ushey