Error when building R package using roxygen2
Asked Answered
C

2

9

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.

Coarsegrained answered 26/5, 2014 at 12:15 Comment(0)
R
13

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.

Roughage answered 26/5, 2014 at 19:16 Comment(0)
W
3

So if 2. does not work, file it as a (reproducible) bug report with the roxygen2 team.

I see no Rcpp issue; it is possible that something went wrong with the Imports: / NAMESPACE declaration. I see no Rcpp issue here (as 1. works fine).

FWIW I also use roxygen2 on some packages, and I too build them from time to time in RStudio.

Whipple answered 26/5, 2014 at 12:34 Comment(2)
I just found actually the problem is in the NAMESPACE file. Thank you!Coarsegrained
Right -- I never quite fell for the idea of letting a tool modify one's own DESCRIPTION and NAMESPACE. I use roxygen2 solely for the Rd file generation.Whipple

© 2022 - 2024 — McMap. All rights reserved.