简体   繁体   中英

Importing an Rcpp header file in NAMESPACE within an R Package

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?

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:

  1. A single function from Rcpp package is imported for registration reasons.
    • Generally, either evalCpp or sourceCpp is used.
  2. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM