[英]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.h
和header.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
文件的主要要求是确保:
evalCpp
or sourceCpp
is used.evalCpp
或sourceCpp
。useDynLib()
,useDynLib()
提供共享对象的名称,
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/Makevars
和src/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.