【问题标题】:Changing the source code of a function via RStudio without creating a new function [duplicate]通过 RStudio 更改函数的源代码而不创建新函数 [重复]
【发布时间】:2014-12-10 11:12:05
【问题描述】:

我需要更改包中名为 mirt 的函数中的几行。

该函数是fscores.internal,不是主函数,您可以使用editfix 更改代码行。

如何在 RStudio 中做到这一点?

我尝试了以下操作:

library(mirt)
data(LSAT7)
LSAT7=expand.table(LSAT7)
mod=mirt(LSAT7, 1,itemtype = '3PL')
fscor=fscores(object = mod,method = "ML",response.pattern = c(1,NA,NA,NA,NA),theta_lim = c(-4, 4))


fscor
     Item.1 Item.2 Item.3 Item.4 Item.5  F1 SE_F1
[1,]      1     NA     NA     NA     NA Inf    NA

这是最终输出。我需要“theta_lim[2]”而不是“Inf”作为 F1 的输出,当您将 Inf 替换为 theta_lim[2] 和 -Inf 替换为 theta_lim[1] 时可以获得(分别在第 278 和 280 行,在 fscores .internal 代码。这是我要做的唯一更改。

使用fixInNamespace("fscores.internal", "mirt")

出现一个窗口,其中包含:

function (object, ...) 
standardGeneric("fscores.internal")

fixInNamespace 没有帮助)

使用后

myfscores.internal <- function() { ... }
assignInNamespace("fscores.internal", myfscores.internal, "mirt")

运行时出现以下错误

fscor=fscores(object = mod,method = "ML",response.pattern = c(1,1,1,1,1),theta_lim = c(-4, 4))



## Error in .local(object, ...) : could not find function "ExtractGroupPars"

然后我单独编译ExtractGroupPars函数。

然后出局是

> fscor=fscores(object = mod,method = "ML",response.pattern = c(1,1,1,1,1),theta_lim = c(-4, 4))
Error in .local(object, ...) : could not find function "rotateLambdas"

在这之后我编译了rotateLambdas函数

我跑了

fscor=fscores(object = mod,method = "ML",response.pattern = c(1,1,1,1,1),theta_lim = c(-4, 4))

然后我得到以下错误:

Error in matrix(rep(sqrt(1 - h2), ncol(F)), ncol = ncol(F)) : 
  non-numeric matrix extent
Called from: .rs.breakOnError(TRUE)

不知道之后该怎么办。

【问题讨论】:

  • fscores.internal 是纯 R 代码,可以从源代码压缩包轻松访问以进行编辑。也就是说,我强烈建议反对使用内部方法。如果包代码有错误,请通知维护者。如果您只是想做一些不同的事情,那么编写一个包装器或辅助函数来修改 fscores 的输出。内部函数并不意味着被搞砸。
  • 您使用了assignInNamespacefixInNamespace?修改函数后尝试重新运行代码后发生了什么?
  • @Thomas,我已进一步编辑问题,请添加您的建议。

标签: r


【解决方案1】:

您可以使用assignInNamespace 重写包命名空间内的函数。在这种情况下,您将定义一个新函数并分配给 mirt 命名空间:

myfscores.internal <- function() { ... }
assignInNamespace("fscores.internal", myfscores.internal, "mirt")

注意:如果从包命名空间导出 fscores.internal,行为可能会有所不同。

【讨论】:

  • 感谢您的回复。在这里,我仍然需要创建一个新函数;没有更好的办法吗?
  • 为什么这对你来说是个问题?您可以随时rm("myfscores.internal") 清理您的工作区。
  • 现在的问题是 fscores.internal 中使用的函数很少;现在我认为新功能 myfscores.internal 无法识别那些已经在包源中使用的功能。
  • 我认为这不是问题,但您可能需要使用 mirt:::otherfunction(...) 等明确引用这些函数,而不仅仅是 otherfunction(...)
  • 问题还没有解决。当我这样做时: myfscores.internal
猜你喜欢
  • 1970-01-01
  • 2019-09-18
  • 1970-01-01
  • 2016-09-25
  • 2013-11-06
  • 2012-04-30
  • 2011-10-04
  • 1970-01-01
  • 2012-06-02
相关资源
最近更新 更多