在 R 包内的 NAMESPACE 中导入 Rcpp 头文件 [英] Importing an Rcpp header file in NAMESPACE within an R Package

查看:36
本文介绍了在 R 包内的 NAMESPACE 中导入 Rcpp 头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在 R 中的第一个包,我已经有了工作包,但是我会删除 cpp 文件中的一些重写功能,所以我做了一个可以使用单个功能的头文件.

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.

我怎样才能把这个头文件放在包里?注意header.hheader.cpp在包的src/目录下并且 #include "header.h" 位于 .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

我试图修改 NAMESPACE 文件:

import(myheader) 

但是,当我这样做时:

R CMD INSTALL mypackage 

我收到此错误:

Error: package or namespace load failed for 'mypackage' in namespaceExport(ns, exports):
 undefined exports: myheader

我该如何解决这个错误?

How can I solve this error?

推荐答案

正如@RalfStubner 在评论中指出的那样,NAMESPACE 文件用于导出和导入 R函数和数据.

As @RalfStubner pointed out in the comments, the NAMESPACE file is meant for exporting and importing R functions and data.

使用 Rcpp 的包中的 NAMESPACE 文件的主要要求是确保:

The primary requirement for a NAMESPACE files in a package using Rcpp is to ensure:

  1. 出于注册原因,导入Rcpp 包中的单个函数.
    • 通常,使用 evalCppsourceCpp.
  1. A single function from Rcpp package is imported for registration reasons.
    • Generally, either evalCpp or sourceCpp is used.
  • 这是正在构建的 R 包的名称.
  • 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 <>.

如果您对使用标头在 R 包之间共享代码感兴趣,请考虑查看:

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

主要的设计模式是使用 inst/include 目录来放置一个只有头文件的库.然后,在 src/ 中写入库的绑定.确保 src/Makevarssrc/Makevars.win 具有:

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/

<小时>

如果要在同一个 R 包中的 .cpp 文件之间共享函数定义,请参阅:


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

这避免了单一的 .cpp 文件,但不允许在导出的 R 包之外的 R 包之间共享已编译的代码例程> 包装.

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.

这篇关于在 R 包内的 NAMESPACE 中导入 Rcpp 头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆