简体   繁体   English

在 R 包内的 NAMESPACE 中导入 Rcpp 头文件

[英]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.这是我在R 中的第一个包,我已经有了工作包,但我会删除 cpp 文件中的一些重写功能,所以我做了一个可以使用单个功能的头文件。

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请注意, header.hheader.cpp位于包的src/目录中, #include "header.h"位于我使用此函数的.cpp文件中

I tried to modify the NAMESPACE file with:我尝试使用以下命令修改NAMESPACE文件:

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.正如@RalfStubner 在评论中指出的那样, NAMESPACE文件用于导出和导入R函数和数据。

The primary requirement for a NAMESPACE files in a package using Rcpp is to ensure:使用Rcpp的包中的NAMESPACE文件的主要要求是确保:

  1. A single function from Rcpp package is imported for registration reasons.出于注册原因,从Rcpp包中导入了一个函数。
    • Generally, either evalCpp or sourceCpp is used.通常,使用evalCppsourceCpp
  2. Provide the name of the shared object via useDynLib() ,通过useDynLib()提供共享对象的名称,
    • This is the name of the R package being built.这是正在构建的R包的名称。
importFrom(Rcpp, sourceCpp)
useDynLib(<PACKAGE_NAME_HERE>, .registration = TRUE)

where <PACKAGE_NAME_HERE> is the name of the package without <> .其中<PACKAGE_NAME_HERE>是没有<>的包名。


If you're interested in using headers to share code between R packages, consider looking at:如果您对使用标头在R包之间共享代码感兴趣,请考虑查看:

https://github.com/r-pkg-examples/rcpp-shared-cpp-functions 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.主要的设计模式是使用inst/include目录来放置一个只有头文件的库。 Then, in src/ write bindings to the library.然后,在src/写入库的绑定。 Ensure that src/Makevars and src/Makevars.win has:确保src/Makevarssrc/Makevars.win具有:

# 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:如果要在同一个R包中的.cpp文件之间共享函数定义,请参阅:

https://github.com/r-pkg-examples/rcpp-headers-src 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.这避免了单个单一的.cpp文件,但不允许在导出的R包装器之外的R包之间共享已编译的代码例程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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