修改当前 R 会话的 R 包函数;assignInNamespace 的行为不像 fixInNamespace? [英] Modifying an R package function for current R session; assignInNamespace not behaving like fixInNamespace?

查看:50
本文介绍了修改当前 R 会话的 R 包函数;assignInNamespace 的行为不像 fixInNamespace?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够以自动"方式修改 R 包中的隐藏函数,例如使用 fixInNamespace,但我可以提前编写代码,而不是在fixInNamespace 所做的编辑"窗口.我认为 assignInNamespace 可以完成这项工作,但目前它不起作用.这是问题的一个示例.

I would like to be able to modify a hidden function inside an R package in an "automated" way, like using fixInNamespace, but where I can write the code in advance, and not in an "edit" window which fixInNamespace does. I think assignInNamespace could do the job, but currently it's not working. Here's an example of the problem.

require(quantmod)

getSymbols("AAPL")

chartSeries(AAPL)  # Works fine up to here. 

现在说我想在绘图的左侧而不是右侧绘制 yaxis 刻度.这可以通过修改 quantmod 包中的源代码来完成.修改绘图布局的相关代码在一个名为 chartSeries.chobhidden quantmod 函数中.

Now say I want to yaxis ticks to be drawn on the left side of the plot, instead of the right. This can be done by modifying the source code in the quantmod package. The relevant code for modifying the plot layout is in a hidden quantmod function called chartSeries.chob.

可以这样做:

fixInNamespace("chartSeries.chob", ns = "quantmod")

在编辑窗口中,手动将第117行从axis(4)修改为axis(2),点击OK,再次运行chartSeries(AAPL) (现在 y 轴标签将绘制在绘图的左侧).一切都很好,剧情按预期生成,没有问题.

and in the edit window, manually modify line 117 from axis(4) to axis(2), click OK, and again run chartSeries(AAPL) (now the y axis labels will plot on the left side of the plot). Everything is good, the plot is generated as expected, no problems.

但是......现在假设我想提前(以自动方式)修改 chartSeries.chob,大概是通过获取 chartSeries.chob 函数的修改版本,而不使用编辑窗口.例如,我可能想修改函数中的几十行,每次打开编辑窗口为一个新的 R 会话打开是不切实际的.

But ... now suppose I want to modify chartSeries.chob in advance (in an automated way), presumably by sourcing a modified version of the chartSeries.chob function, without using the edit window. I might want modify dozens of lines in the function for example, and opening the edit window each time for a new R session is not practical.

我该怎么做?

现在我正在这样做,但不起作用:

Right now I am doing this, which is not working:

assignInNamespace("chartSeries.chob", value = chartSeries.chob2, ns = "quantmod")

我从控制台获取 chartSeries.chob 的完整副本以及第 117 行的修改代码.

where I source from the console a full copy of chartSeries.chob with the modified code on line 117.

chartSeries.chob2 <- function (x) 
{
old.par <- par(c("pty", "mar", "xpd", "bg", "xaxs", "las", 
                   "col.axis", "fg"))
  on.exit(par(old.par))
....

[Edit On 117:] axis(2)
...
}

当我从控制台运行时:

chartSeries(AAPL)

quantmod:::chartSeries(AAPL)

我收到错误 - 未找到从 chartSeries.chob 函数中对 quantmod 中其他函数的调用,可能是因为未找到编辑的 chartSeries.chob 函数在 quantmod 命名空间中?

I get errors -- the calls to other functions in quantmod from within the chartSeries.chob function are not found, presumably because the edited chartSeries.chob function is not in the quantmod namespace?

我注意到,在 assignInNamespace 命令后从控制台键入 quantmod:::chartSeries.chob 时,末尾没有 environment: namespace:quantmod函数定义.

I notice that when typing quantmod:::chartSeries.chob from the console after the assignInNamespace command, there is no environment: namespace:quantmod at the end of the function definition.

但是如果我使用 fixInNamespace 修改方法,当我输入 quantmod:::chartSeries.chob 时,我会看到 environment: namespace:quantmod 附加到函数定义的末尾.

But if I do the fixInNamespace modification approach, when I type quantmod:::chartSeries.chob, then I do see environment: namespace:quantmod appended to the end of the function definition.

推荐答案

既然 fixInNamespace 调用 assignInNamespace 你应该可以让它工作,问题可能是环境不一样,可能还有其他一些属性.如果您将它们更改为匹配,那么我希望它可以更好地工作,可能使用如下代码:

Since fixInNamespace calls assignInNamespace you should be able to get it to work, the problem is probably that the environment is not the same and possibly some other attributes. If you change those to match then I would expect it to work better, possibly using code like:

tmpfun <- get("chartSeries.chob", envir = asNamespace("quantmod"))
environment(chartSeries.chob2) <- environment(tmpfun)
attributes(chartSeries.chob2) <- attributes(tmpfun)  # don't know if this is really needed
assignInNamespace("chartSeries.chob", chartseries.chob2, ns="quantmod")

进行某些更改的另一个选项是使用 trace 函数.这将进行临时更改,并且可以很好地插入代码,但是我不知道删除命令或就地修改是否合理(指定更改代码而不是让您更改代码的编辑器可能会使这成为可能).

Another option for some changes would be to use the trace function. This would make temporary changes and would be fine for inserting code, but I don't know if it would be reasonable for deleting commands or modifying in place (specifying an editor that changed the code rather than letting you change it might make this possible).

这篇关于修改当前 R 会话的 R 包函数;assignInNamespace 的行为不像 fixInNamespace?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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