使用跟踪在 R 中编辑函数?

Editing a function in R using trace?

我注意到我要使用的包中的一个函数存在错误。 GitHub上已经有issue了,但是作者还没有解决这个问题,我需要的功能尽快

因此我想编辑代码。显然,这可以通过编辑源代码、重新打包和安装整个包来实现,我可以重写函数并重新分配命名空间,但也可以通过使用 trace().

在当前会话中编辑函数来实现

我已经发现我可以做到:

as.list(body(package:::function_inside_function))

我要编辑的行位于函数的第二步。

具体来说就是我需要编辑的代码中的this line。我必须将 ignore.case 更改为 ignore.case=TRUE。例如 link 死亡:

functionx(){if{...} else if(grepl("miRNA", data.type, ignore.case)) {...}}

我还没有真正找到关于如何从这里开始的实际示例,所以任何人都可以向我展示如何执行此操作的示例,或者引导我使用跟踪的实际示例吗?或者也许将函数重新分配给命名空间?

我曾经遇到过类似的问题并使用 assignInNamespace() 解决了它。我没有安装你的包,所以我不能确定这对你有用,但我认为它应该。您将按如下方式进行:

制作你想要的功能版本,如编辑:

# I would just copy the function off github and change the offending line
readTranscripttomeProfiling <- function() {"Insert code here"}

# Get the problematic version of the function out of the package namespace
tmpfun <- get("readTranscripttomeProfiling", 
               envir = asNamespace("TCGAbiolinks"))

# Make sure the new function has the environment of the old 
# function (there are possibly easier ways to do this -- I like 
# to get the old function out of the namespace to be sure I can do 
# it and am accessing what I want to access)
environment(readTranscripttomeProfiling) <- environment(tmpfun)
# Replace the old version of the function in the package namespace
# with your new version
assignInNamespace("readTranscripttomeProfiling", 
                   readTranscripttomeProfiling, ns = "TCGAbiolinks")

我在另一个 Whosebug 回复中找到了这个解决方案,但目前似乎找不到原始解决方案。

对于您的具体情况,您可能确实可以使用 trace 来解决它。

根据您提供的 link 我不知道您为什么说 函数内嵌函数 ,但这应该有效:

# example
trace("grepl", tracer = quote(ignore.case <- TRUE))

grepl("hi", "Hi")
## Tracing grepl("hi", "Hi") on entry 
## [1] TRUE

# your case (I assume)
trace("readTranscriptomeProfiling", tracer = quote(ignore.case <- TRUE))

请注意,如果您要修复的参数 ignore.case 不在调用中的正确位置,这将更加复杂。