如何覆盖包名称空间中的不可见函数? [英] How do I override a non-visible function in the package namespace?

查看:64
本文介绍了如何覆盖包名称空间中的不可见函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上想要改变一个包的不可见功能。对于可见的函数,即在调用方法时没有asterix的函数,我发现了两篇文章,可以实现我的目标:


  1. 使用 assignInNamespace :参见 R-help 使用修正:请参阅 stackoverflow

尽管这两种方法都适用于导出/可见函数(我使用 predict.lm 作为第二种方法的进一步示例,并且使用函数 subset.data.frame )测试了第一种方法,但它们不适用于不可见函数,例如 predict.ar 。这是为什么?是否有解决方法?



以下是一个简单示例:

显示predict.lm可见,预测.ar不是:

 方法(预测)
[1]预测.Arima *预测.HoltWinters *预测。 StructTS *
[4] predict.ar * predict.arima0 * predict.glm
[7] predict.lm predict.loess * predict.mlm
[10] predict.nls * predict。 poly predict.ppr *
[13] predict.prcomp * predict.princomp * predict.smooth.spline *
[16] predict.smooth.spline.fit *

应用 predict.lm

  x < -  rnorm(5)
y <-x + rnorm(5)
predict(lm(y〜x))
#1 2 3 4 5
#1.0783047 1.5288031 0.3268405 0.8373520 -0.9833746

更改 predict.lm ,方法是在函数体的开头输入cat(first line changed for predict.lm \\\

。 (您必须在编辑器中手动执行此操作):

  fix(predict.lm)
predict(lm( y〜x))
#预测的第一行已更改
#1 2 3 4 5
#1.0783047 1.5288031 0.3268405 0.8373520 -0.983374

应用 predict.ar

<$ p (太阳黑子年)
预测(太阳黑子,n.ahead = 25)
#$ pred
#时间系列:
#开始= 1989
#结束= 2013

尝试更改 predict.ar

  fix(predict.ar)#这里,一个空的函数体出现给我
fix(stats ::: predict.ar)#这里还有
fix(stats ::: predict.ar)
# stats ::: predict.ar):'fix'需要一个名字

尝试使用 assignInNamespace 代替。 (请注意,我只是在编辑器中复制了函数 stats ::: predict.ar 并添加了 cat这行(第一行已更改为预测。 ar \ n),因为函数体很长,所以我只显示前几行)

  mypredict<  - 函数(object,newdata,n.ahead = 1,se.fit = TRUE,...)
{
cat(如果(n.ahead <1)
stop(n.ahead必须至少为1),
#Rest变为predict.ar\
的第一行)
统计信息::: predict.ar
}
assignInNamespace(predict.ar,mypredict,ns =stats)
predict(sunspot.ar,n.ahead = 25 )
#预测的第一行改变
#预测的错误(sunspot.ar,n.ahead = 25):
#object'C_artoma'找不到

由于predict.ar的第一行更改实际上已打印到控制台,因此predict.ar必须已更改。然而,为什么没有找到对象'C_artoma'?



更新:好吧,这太令人尴尬了,但我不能删除该帖子:答案是已经在我最后提供Richie Cotton答案的链接上。对不起浪费你的时间!我想我查了一切,然后我没有看到明显的。有人可以发布这个答案,我接受它。

  fixInNamespace(predict.ar,pos =package:stats)


解决方案使用 fixInNamespace 。 :)

  fixInNamespace(predict.ar,stats)



  fixInNamespace(predict.ar, pos =package:stats)






几年后......)

来自Nicholas H的评论:如果你想将一些代码推送到依赖于来自另一个包的内部函数的CRAN,它将抛出一个构建警告并被R核拒绝。如果你需要这个内部函数,你应该使用 ::: 操作符来拷贝它,并自己维护它。

  predict.ar<  -  stats ::: predict.ar 


I basically want to change a non-visible function of a package. For visible functions, i.e. functions that have no asterix when methods is called on them, I found two posts how I could achieve my goal:

  1. Use assignInNamespace: see post on R-help.
  2. Use fix: see post on stackoverflow

Although both approaches work for an exported / visible function (I use predict.lm as an example further below for the second approach and tested the first approach with the function subset.data.frame), they do not work for a non-visible function, e.g. predict.ar. Why is that? Is there a workaround?

Here is a minimal example:

Show that predict.lm is visible, predict.ar is not:

methods(predict)
 [1] predict.Arima*             predict.HoltWinters*       predict.StructTS*         
 [4] predict.ar*                predict.arima0*            predict.glm               
 [7] predict.lm                 predict.loess*             predict.mlm               
[10] predict.nls*               predict.poly               predict.ppr*              
[13] predict.prcomp*            predict.princomp*          predict.smooth.spline*    
[16] predict.smooth.spline.fit*

Apply predict.lm:

x <- rnorm(5)
y <- x + rnorm(5)
predict(lm(y ~ x))
#          1          2          3          4          5 
#  1.0783047  1.5288031  0.3268405  0.8373520 -0.9833746

Change predict.lm by entering cat("First line changed for predict.lm\n") at the beginning of the function body. (You have to do that manually in the editor):

fix(predict.lm)
predict(lm(y ~ x))
# First line changed for predict.lm
#          1          2          3          4          5 
#  1.0783047  1.5288031  0.3268405  0.8373520 -0.983374

Apply predict.ar:

sunspot.ar <- ar(sunspot.year)
predict(sunspot.ar, n.ahead=25)
# $pred
# Time Series:
# Start = 1989 
# End = 2013 

Try to change predict.ar:

fix(predict.ar) #Here, an empty function body appears for me
fix("stats:::predict.ar") #Here as well
fix(stats:::predict.ar)
#Error in fix(stats:::predict.ar) : 'fix' requires a name

Try to use assignInNamespace instead. (Note that I just copied the function stats:::predict.ar in an editor and added the line cat("First line changed for predict.ar\n") at the beginning of the body. Because the function body is quite long, I only show the first couple of lines here)

mypredict <- function (object, newdata, n.ahead = 1, se.fit = TRUE, ...) 
{
    cat("First line changed for predict.ar\n")
    if (n.ahead < 1) 
        stop("'n.ahead' must be at least 1")
    #Rest of body of stats:::predict.ar
}
assignInNamespace("predict.ar", mypredict, ns="stats")
predict(sunspot.ar, n.ahead=25)
# First line changed for predict.ar
# Error in predict.ar(sunspot.ar, n.ahead = 25) : 
#   object 'C_artoma' not found

Since "First line changed for predict.ar" is actually printed to the console, predict.ar must have been changed. However, why is the object 'C_artoma' not found anymore?

UPDATE: OK, this is super embarrassing, but I can't delete that post anymore: The answer was already at the link I provided with Richie Cotton's answer at the end. Sorry for wasting your time! I think I checked everything and then I don't see the obvious. Could someone just post this as an answer an I accept it. Sorry again.

fixInNamespace(predict.ar, pos="package:stats")

解决方案

Use fixInNamespace. :)

fixInNamespace("predict.ar", "stats")

or

fixInNamespace("predict.ar", pos="package:stats")


(Several years later...)
From Nicholas H's comment: if you want to push some code to CRAN that depends upon an internal function from another package, it will throw a build warning and be rejected by R-core. If you want that internal function, you should just take a copy of it using the ::: operator and maintain it yourself.

predict.ar <- stats:::predict.ar

这篇关于如何覆盖包名称空间中的不可见函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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