r - .Call 的 PACKAGE 参数如何工作?

标签 r internals

.Call似乎记录很差; ?.Call给出了 PACKAGE 的解释争论:

PACKAGE: if supplied, confine the search for a character string .NAME to the DLL given by this argument (plus the conventional extension, ‘.so’, ‘.dll’, ...).

This argument follows ... and so its name cannot be abbreviated.

This is intended to add safety for packages, which can ensure by using this argument that no other package can override their external symbols, and also speeds up the search (see ‘Note’).


并在注释中:

If one of these functions is to be used frequently, do specify PACKAGE (to confine the search to a single DLL) or pass .NAME as one of the native symbol objects. Searching for symbols can take a long time, especially when many namespaces are loaded.

You may see PACKAGE = "base" for symbols linked into R. Do not use this in your own code: such symbols are not part of the API and may be changed without warning.

PACKAGE = "" used to be accepted (but was undocumented): it is now an error.


但是没有使用示例。
不清楚 PACKAGE论证有效。例如,在回答 this问题,我认为以下应该有效,但它没有:
.Call(C_BinCount, x, breaks, TRUE, TRUE, PACKAGE = "graphics")
相反,这有效:
.Call(graphics:::C_BinCount, x, breaks, TRUE, TRUE)
这仅仅是因为C_BinCount未导出?即,如果hist.default的内码已添加 PACKAGE = "graphics" ,这会奏效吗?
这看起来很简单,但很难找到这个论点的用法;我发现的所有来源都没有给出更多提及( 12345 )......这个实际工作的例子将不胜感激(即使它只是引用代码在现有包中找到)
(出于自我控制的目的,如果您不想从另一个问题复制粘贴代码,这里是 xbreaks ):
x = runif(100000000, 2.5, 2.6)
nB <- 99
delt <- 3/nB
fuzz <- 1e-7 * c(-delt, rep.int(delt, nB))
breaks <- seq(0, 3, by = delt) + fuzz

最佳答案

C_BinCount是“NativeSymbolInfo”类的对象,而不是命名 C 级函数的字符串,因此 PACKAGE(“限制对字符串 .NAME 的搜索”)是不相关的。 C_BinCount因在 useDynLib() 中提及而成为一个符号在图形包中 NAMESPACE .

作为 R 符号,C_BinCount的解析遵循与其他符号相同的规则——它不是从 NAMESPACE 导出的,因此只能通过 graphics:::C_BinCount 访问.而且,出于这个原因,禁止进行健壮的代码开发。由于C入口点是作为符号导入的,不能作为字符串使用,所以.Call("C_BinCount", ...)不管用。

使用 NativeSymbolInfo 对象告诉 R C 代码所在的位置,因此无需通过 PACKAGE 再次这样做;使用符号而不是字符串的选择是由包开发人员做出的,我认为通常被认为是好的做法。在 NativeSymbolInfo 发明之前开发的许多包都使用 PACKAGE 参数,如果我 grep Bioconductor 源代码树,有 4379 行带有 .Call.*PACKAGE,例如,here .

其他信息(包括示例)位于“编写 R 扩展”部分 1.5.4 中。 .

关于r - .Call 的 PACKAGE 参数如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38439765/

相关文章:

arrays - 仅对包含矩阵列表中完整案例的行进行子集化

memory - 哪种 channel 类型在 Go 中使用最少的内存?

java - 按需初始化持有者习语 - 何时加载类?

c - VxWorks中任务阻塞时,栈中保存的任务上下文是什么?

android - 用于理解 Android 概念的推荐资源

r - Shiny 的observeEvent 在应用程序启动时触发

r - 由于 R 中的缺失而删除了观察

r - 等效于 Rcpp 的 lineprof

git - 什么构成了 Git 中的 merge 冲突?

r - coin::wilcox_test 与 R 中的 wilcox.test