9

为了临时编辑一个打包函数的主体func,我经常使用trace(func, edit=TRUE). 但是,出于某种原因,R 不允许我在以下情况func下这样做[.data.table

## Note: In this and the other cases below, once an editor pops up, I save and 
## and then exit without making any edits to the function. The commented-out
## message below each call to trace() is what is then printed to my R console.

trace("[.data.table", where=data.table, edit=TRUE)
# Error in .makeTracedFunction(def, tracer, exit, at, print, doEdit) : 
#   the editing in trace() can only change the body of the function, not 
#   the arguments or defaults

问题:什么可能导致此错误?还有哪些其他功能也会触发它?对于此类功能,是否有一些替代解决方法可以让我编辑它们?

FWIW,这似乎不是data.table命名空间中的函数的一般问题(见#1下文),也不是一般子集方法的问题(见#2下文)。

## (#1)     
trace("within.data.table", where=data.table, edit=TRUE)
# Tracing function "within.data.table" as seen from package "data.table"
# [1] "within.data.table"

## (#2)
trace("[.Date", edit=TRUE)
# Tracing function "[.Date" in package "base"
# [1] "[.Date"

我在 Windows XP 机器上运行R-3.0.0,无论我使用 set还是使用 R GUI 的默认编辑器都会得到相同的错误。data.table_1.8.8options(editor="emacs")options(editor="notepad")

4

1 回答 1

5

这显然是由于最近在的正式参数列表中的{}一个位置添加了花括号(即)。data.table

首先,一个 MRE 表明牙套确实会导致trace(..., edit=TRUE)窒息:

## Without braces, no problem
func <- function(inColor=FALSE, col = if(inColor) "red" else "grey") { 
    plot(rnorm(99), col=col)}

trace(func, edit=TRUE)
# [1] "func"


## With braces, tracing fails
funcB <- function(inColor=FALSE, col = if(inColor) "red" else {"grey"}) { 
    plot(rnorm(99), col=col)}

trace(funcB, edit=TRUE)
# Error in .makeTracedFunction(def, tracer, exit, at, print, doEdit) : 
#   the editing in trace() can only change the body of the function, not 
#   the arguments or defaults

然后,为了记录,这里是[.data.table版本 1.8.6(跟踪工作)和版本 1.8.8(不跟踪)中的形式:

## Version 1.8.6 -- Tracing worked
function (x, i, j, by, keyby, with=TRUE, nomatch=getOption("datatable.nomatch"), 
    mult="all", roll=FALSE, rolltolast=FALSE, 
    which=FALSE, .SDcols, verbose=getOption("datatable.verbose"), drop=NULL)


## Version 1.8.8 -- Tracing doesn't (See {} in the 'rollends' argument)
function (x, i, j, by, keyby, with=TRUE, nomatch=getOption("datatable.nomatch"), 
    mult = "all", roll = FALSE, 
    rollends = if (roll == "nearest") c(TRUE, 
        TRUE) else {
        if (roll >= 0) 
            c(FALSE, TRUE)
        else c(TRUE, FALSE)
    }, 
    which = FALSE, .SDcols, verbose = getOption("datatable.verbose"), 
    allow.cartesian = getOption("datatable.allow.cartesian"), 
    drop = NULL, rolltolast = FALSE) 
于 2013-04-11T17:13:57.480 回答