问题
This is my first package in R, I already have working package but I would remove some rewriting function in cpp file, so I do an header file that work with single function.
How can I put this header in package?
Note that header.h and header.cpp are in src/ directory of package
and the #include "header.h" is in the .cpp file where I use this function
I tried to modify the NAMESPACE file with:
import(myheader)
But, when I do:
R CMD INSTALL mypackage
I receive this error:
Error: package or namespace load failed for 'mypackage' in namespaceExport(ns, exports):
undefined exports: myheader
How can I solve this error?
回答1:
As @RalfStubner pointed out in the comments, the NAMESPACE file is meant for exporting and importing R functions and data.
The primary requirement for a NAMESPACE files in a package using Rcpp is to ensure:
- A single function from Rcpp package is imported for registration reasons.
- Generally, either
evalCpporsourceCppis used.
- Generally, either
- Provide the name of the shared object via useDynLib(),
- This is the name of the R package being built.
importFrom(Rcpp, sourceCpp)
useDynLib(<PACKAGE_NAME_HERE>, .registration = TRUE)
where <PACKAGE_NAME_HERE> is the name of the package without <>.
If you're interested in using headers to share code between R packages, consider looking at:
https://github.com/r-pkg-examples/rcpp-shared-cpp-functions
The main design pattern is using inst/include directory to place a header-only library. Then, in src/ write bindings to the library. Ensure that src/Makevars and src/Makevars.win has:
# Register where the header files for the package can be found
PKG_CXXFLAGS=-I../inst/include/
If you want to share function definitions between .cpp files in the same R package, see:
https://github.com/r-pkg-examples/rcpp-headers-src
This avoids a single monolithic .cpp file, but does not allow for sharing the compiled code routines between R packages outside of the exported R wrapper.
来源:https://stackoverflow.com/questions/59266585/importing-an-rcpp-header-file-in-namespace-within-an-r-package