【问题标题】:How to modify and use a function of an R package?如何修改和使用 R 包的功能?
【发布时间】:2014-05-03 21:05:30
【问题描述】:

我的问题是关于How do you adjust/control the scale in a treemap (using the 'portfolio' library) in R?

我按照其中一个答案的建议将seq(-1,0 修改为seq(0,1。然后我将整个 map.market 函数复制并粘贴到 R 中,但无法调用我刚刚粘贴的修改版本。当我键入map.market 时,函数“portfolio”的原始定义将打印在 R 编辑器窗口中。如何运行我刚刚粘贴的版本?

【问题讨论】:

  • 将其保存在文件中并使用source("file_path.R")

标签: r function


【解决方案1】:

如果您只是复制和粘贴,该功能并没有真正保存在您的会话中。您需要将其分配给 R 中的一个对象。当您键入函数名称 map.market 时,您会得到代码:

function(...)
{
# all
# the code 
# of the function
}
<bytecode: 0x0000000007dd9aa0>
<environment: namespace:portfolio>

所以,你必须复制&lt;bytecode&gt;&lt;environment&gt; 行之前的所有内容,修改并保存到一个对象中

map.market2 = function(...)
{
# all
# the code 
# of the function (with modifications)
}

现在,您可以根据需要使用新修改的函数map.market2。如果需要,您可以将其命名为 map.market,但请检查它是否不会破坏您的其余代码。例如,如果您以前使用过原来的功能,因为新修改的功能将优先于原来的功能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    • 2018-08-03
    • 1970-01-01
    • 2019-07-15
    • 1970-01-01
    • 2014-07-19
    • 2013-05-05
    相关资源
    最近更新 更多