在自动生成的 RcppExports.R 中导致检查错误的函数 [英] function leading to check error in automatically generated RcppExports.R

查看:26
本文介绍了在自动生成的 RcppExports.R 中导致检查错误的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Rcpp 0.12.11 和 R 3.4.0.

I am using Rcpp 0.12.11 and R 3.4.0.

当我将 Rcpp 升级到 0.12.11 时,由 Rcpp::compileAttributes 自动生成的 R 文件 RcppExports.R 开始给我稍微不同的函数调用

When I upgraded Rcpp to 0.12.11, the automatically generated R file RcppExports.R by the Rcpp::compileAttributes started to give me slightly different function calls

 run_graph_match <- function(A, B, algorithm_params) {
 # Rcpp 0.12.10
.Call('RGraphM_run_graph_match', PACKAGE = 'RGraphM', A, B, algorithm_params)

 # Rcpp 0.12.11
.Call(RGraphM_run_graph_match, A, B, algorithm_params)
}

是否有一种简单的方法可以解释更改背后的原因?

Is there an easy way to explain the reason behind the change?

后一个函数在检查R包时会导致错误.例如,诸如此类的错误符号RGraphM_run_graph_match"不在命名空间中:.Call(RGraphM_run_graph_match, A, B, algorithm_params)

The latter function leads to errors when checking the R package. For example, errors such as symbol 'RGraphM_run_graph_match' not in namespace: .Call(RGraphM_run_graph_match, A, B, algorithm_params)

推荐答案

恭喜您,您已经体验了 第 5.4 节:注册本机例程 要求在 R 3.4.0 中添加.该要求要求包含一个 src/init.c 文件,用于注册每个 C++ 函数及其参数.因此,Rcpp 0.12.11 在 RcppExports.cpp 内生成此文件.同时,这个问题所基于的 RcppExports.R 文件的上下文取决于用户是否正确设置了 useDynLib(pkgname, .registration=TRUE)useDynLib(pkgname),后者并不理想,因为它没有利用接下来讨论的 Rcpp 0.12.11 中引入的新选项.

Congratulations, you've experienced the Section 5.4: Registering native routines requirement added in R 3.4.0. The requirement mandated the inclusion of a src/init.c file that registered each C++ function and their parameters. Thus, Rcpp 0.12.11 generates this file inside of the RcppExports.cpp. Meanwhile, the RcppExports.R file, which is what this question is based upon, has its context being dependent on whether the user appropriately sets useDynLib(pkgname, .registration=TRUE) or useDynLib(pkgname), where the later is not ideal as it does not take advantage of a new option introduced in Rcpp 0.12.11 discussed next.

由于 CRAN 政策的这种转变,Rcpp 1 属性的创建者 JJ Allaire 受到启发,提出了 Douglas Bates 在 2012 年首次添加属性时提出的建议.具体来说,目标是将调用从基于字符串更改为符号.更改背后的基本原理只是简单地说,当包加载时有一个符号在手,而不是每次运行函数时都必须查找并转换为符号的字符串.因此,与过去 Rcpp 的基于字符串的方法相比,符号查找在重复调用上的开销更低.

As a result of this shift in CRAN policy, JJ Allaire, the creator of Attributes for Rcpp 1, was inspired to advance a suggestion made by Douglas Bates back in 2012 when attributes was first added. Specifically, the goal was to change the call from being string-based to being a symbol. The rationale behind the change is simply put that a symbol is onhand when the package loads vs. a string which has to be looked up and converted into a symbol each time the function is run. Therefore, symbol lookup is less expensive on repetitive calls when compared to the string based method of Rcpp in the past.

基本上,这一行:

.Call('RGraphM_run_graph_match', PACKAGE = 'RGraphM', A, B, algorithm_params)

涉及 R 在包含 R 函数的每次调用中查找符号以访问 C++ 函数.

Involved R looking up the symbol on each call of the encompassing R function to access the C++ function.

与此同时,这一行:

.Call(RGraphM_run_graph_match, A, B, algorithm_params)

是对 C++ 函数的直接调用,因为符号已在内存中.

is a direct call to the C++ function as the symbol is already in memory.

这些主要是 为什么 Rcpp 改变了 RcppExports.R 自动生成方式背后的原因.这种方法的缺点之一是无法像以前那样全局导出所有函数.特别是,一些在其 NAMESPACE 文件中包含全局符号导出语句的用户,例如

And those are primarily the reasons behind why Rcpp changed how RcppExports.R was automatically generated. One of the downside of this approach is the inability to globally export all functions like before. In particular, some users that had in their NAMESPACE file a global symbol export statement e.g.

exportPattern("^[[:alpha:]]+")

不得不删除它并选择手动指定应该导出哪些函数或变量.

had to remove it and opt to manually specify what functions or variables should be exported.

更多细节,你可能希望查看引入此功能的 GitHub PR:

For more details, you may wish to see the GitHub PR that introduced this feature:

https://github.com/RcppCore/Rcpp/pull/694

1:有关属性的更多信息,请参阅我的历史帖子:http://thecoatlessprofessor.com/programming/rcpp/to-rcpp-attributes-and-beyond-from-inline/

1: For more on Attributes, see my history post: http://thecoatlessprofessor.com/programming/rcpp/to-rcpp-attributes-and-beyond-from-inline/

这篇关于在自动生成的 RcppExports.R 中导致检查错误的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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