简体   繁体   English

我的包功能的修改版本找不到包的其他内部功能

[英]My modified version of package function can't find the package's other internal functions

I made some ad hoc modifications to a function in the package metafor by copying the function code from Github and replacing the function in my environment as described here : 我通过从Github复制函数代码并在我的环境中替换函数来对包metafor组件中的函数进行一些临时修改, 如下所述

my.rma.uni = function(...) {
# here I simply copied the existing code with no modifications as a test
}

unlockBinding("rma.uni", as.environment("package:metafor"))
assign("rma.uni", rma.uni_mm, as.environment("package:metafor"))
lockBinding("rma.uni", as.environment("package:metafor"))

But when I try to now to run rma.uni , my modified version appears unable to find metafor 's other internal functions: 但是当我现在尝试运行rma.uni ,我修改后的版本似乎无法找到metafor的其他内部函数:

Error in .chkdots(ddd, c("knha", "scale", "link", "outlist")) : 
  could not find function ".chkdots"

I also tried simply calling my.rma.uni directly (which would actually be my preference so that I can have both the standard rma.uni and my version available at the same time), but this yields the same error. 我也试过直接调用my.rma.uni (这实际上是我的偏好,所以我可以同时使用标准的rma.uni和我的版本),但这会产生相同的错误。

How can I ensure that my modified function can access all the internal functions? 如何确保修改后的功能可以访问所有内部功能?

For a short-term hack, I usually just access any necessary hidden functions via triple-colon, eg metafor:::.chkdots . 对于短期黑客攻击,我通常只通过三重冒号访问任何必要的隐藏函数,例如metafor:::.chkdots This is unwieldy (I usually go through and fix them one by one as they fail) and unsuitable for submitting to CRAN etc., but it works for me. 这是笨拙的(我通常会在它们失败时逐个修复它们)并且不适合提交给CRAN等,但它对我有用。

If you need your function to access "private" functions from a namespace, you can set the environment of your function. 如果您需要函数从命名空间访问“私有”函数,则可以设置函数的环境。 Normally functions automatically take the environment of where they are defined. 通常,函数会自动采用定义它们的环境。 But you can programatically change that. 但是你可以以编程方式改变它。

If you just do 如果你这样做

environment(my.rma.uni) <- as.environment("package:metafor")

Then the my.rma.uni function will look up any free symbols in the metafor namespace rather than the global namespace. 然后my.rma.uni函数将查找metafor命名空间中的任何空闲符号,而不是全局命名空间。 You can do this without messing with locking/unlocking the binding and assigning in the namespace. 您可以执行此操作,而无需在命名空间中锁定/解锁绑定和分配。 You only need to do what if you want to completely replace the function in the package. 如果要完全替换包中的函数,只需要执行该操作。 Most times it's better just to create your own copy in the global namespace. 大多数情况下,最好只在全局命名空间中创建自己的副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM