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

查看:148
本文介绍了修改当前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. 

现在说我想对图的左侧而不是右侧绘制刻度线.这可以通过修改quantmod包中的源代码来完成.修改图版面的相关代码位于称为chartSeries.chob隐藏 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.

这可以通过执行以下操作来完成:

This could be done by doing this:

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

,然后在编辑窗口中,将117行从axis(4)修改为axis(2),单击确定",然后再次运行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?

我注意到,在分配InNamespace命令后从控制台键入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天全站免登陆