使用 roxygen2 构建 R 包时出错 [英] Error when building R package using roxygen2

查看:90
本文介绍了使用 roxygen2 构建 R 包时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个文件,Rfile.R 和 Cppfile.cpp.

I have 2 files, Rfile.R and Cppfile.cpp.

Cppfile.cpp 中的内容:

Contents in Cppfile.cpp:

#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
int CPPF(int k){return ++k;}

Rfile.R 中的内容:

Contents in Rfile.R:

RF<-function(k){return(CPPF(k))}

我想基于这两个文件构建一个 R 包.我使用最新版本的 Rstudio 和 Roxygen2.

I want to build an R package based on the 2 files. I use the lastest versions of Rstudio and Roxygen2.

我尝试了 3 种使用或不使用 Roxygen2 的方法来构建包,结果不同:

I tried 3 ways to build the package with or without Roxygen2, and had different results:

  1. New Project->New Directory->R package->Type:Package w/Rcpp,添加Rfile.R和Cppfile.cpp作为源文件.构建与重新加载,一切正常.功能正常工作.

  1. New Project->New Directory->R package->Type:Package w/Rcpp, add both Rfile.R and Cppfile.cpp as source files. Build & reload, everything works out fine. The functions work as they do.

New Project->New Directory->R package->Type:Package w/Rcpp,添加Rfile.R和Cppfile.cpp作为源文件.选择使用 Roxygen 生成文档",选中它的所有选项.构建与重新加载,功能不起作用.输入RF"给出RF的内容,输入CPPF"弹出Object not found".

New Project->New Directory->R package->Type:Package w/Rcpp, add both Rfile.R and Cppfile.cpp as source files. Select "Generate documentations with Roxygen", check all its options. Build & Reload, the functions don't work. Inputting "RF" gives the contents of RF, inputting "CPPF" pops "Object not found".

New Project->New Directory->R package->Type:Package w/Rcpp,只添加Cppfile.cpp作为源文件.选择使用 Roxygen 生成文档",选中它的所有选项.构建与重新加载,该功能有效.然后将Rfile.R直接复制到项目文件夹->R文件夹中.构建与重新加载,一切正常,功能正常.

New Project->New Directory->R package->Type:Package w/Rcpp, add only Cppfile.cpp as source files. Select "Generate documentations with Roxygen", check all its options. Build & Reload, the function works. Then copy Rfile.R directly into the project folder->R folder. Build & Reload, everything fine, functions work well.

我使用 Roxygen 是错误还是 Roxygen 有错误?我需要它来记录.我可以坚持使用第 3 种方式,这种方式花费了我很多精力才能找到,但是是有线的.

Am I using the Roxygen wrong or Roxygen has bugs? I need it to document. I can stick to the 3rd way which cost me much energy to find, but wired.

谢谢!

解决问题的一种方法:选择使用 Roxygen 生成文档"时,不要选中NAMESPACE 文件"选项.

One Way To Solve The Issue: When selecting "Generate documentations with Roxygen", Don't check "NAMESPACE file" option.

推荐答案

你混淆了两件事(不幸的是很容易混淆):

You're mixing up two things (which are easy to mix up, unfortunately):

首先,//[[Rcpp::export]] 属性用于在两个文件中自动生成包装函数,RcppExports.cppRcppExports.R.一个包装 R 函数,CPPF,将在这里由 Rcpp::compileAttributes() 自动生成,并放入 R/RcppExports.R.

First, the // [[Rcpp::export]] attribute is used for auto-generating wrapper functions in two files, RcppExports.cpp and RcppExports.R. A wrapper R function, CPPF, will be auto-generated by Rcpp::compileAttributes() here, and placed into R/RcppExports.R.

其次,roxygen 注释可用于管理NAMESPACE,例如使用 @export 标签.请注意,这与//[[Rcpp::export]]不同

Second, roxygen comments can be used to manage the NAMESPACE, e.g. with the @export tag. Note that this is different from // [[Rcpp::export]]!

自动生成的函数不会自动导出.Rcpp.package.skeleton() 将生成一个 NAMESPACE 文件,该文件自动导出给定名称的所有函数;即,exportPattern("^[[:alpha:]]+") 条目.这对于小包装来说已经足够了;但是随着您的包变得越来越复杂,您将需要对命名空间进行更细粒度的控制.或者,您可以采用一种约定,即所有内部非导出函数都以 . 开头.无论哪种方式,这种机制都允许将自动生成的函数导出到您的包命名空间.

The auto-generated function is not automatically exported. The Rcpp.package.skeleton() will generate a NAMESPACE file that automatically exports all functions of a given name; ie, the exportPattern("^[[:alpha:]]+") entry. This is good enough for small packages; but as your package gets more complicated you will want more fine-grained control over your namespace. Or you can just adopt a convention where all internal, non-exported functions begin with a .. Either way, this mechanism is what allows the auto-generated function to be exported to your package namespace.

如果您想使用 roxygen 来管理 NAMESPACE,您需要将 roxygen 注释添加到您的 C++ 函数中,如果您希望它们在命名空间中导出.所以你可以修改你的函数如下:

If you want to use roxygen to manage the NAMESPACE, you need to add roxygen comments to your C++ functions if you want them to be exported in the namespace. So you could modify your function as the following:

#include <Rcpp.h>
using namespace Rcpp;

//' @export
// [[Rcpp::export]]
int CPPF(int k){return ++k;}

请注意,您可能必须运行 roxygen2::upgradeRoxygen() 以确保 roxygen2 接管 NAMESPACE,对于新版本的roxygen2.

Note that you might have to run roxygen2::upgradeRoxygen() to ensure that roxygen2 takes over the NAMESPACE, for new versions of roxygen2.

这篇关于使用 roxygen2 构建 R 包时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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