为什么当...... = [.data.table时,trace(...,edit = TRUE)不起作用

Jos*_*ien 9 trace r data.table

要临时编辑打包函数的主体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
Run Code Online (Sandbox Code Playgroud)

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

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"
Run Code Online (Sandbox Code Playgroud)

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

Jos*_*ien 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
Run Code Online (Sandbox Code Playgroud)

然后,对于记录,这里是[.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) 
Run Code Online (Sandbox Code Playgroud)

  • 如果有人对预先配置的*trace*感兴趣的`[.data.table`我将它构建为[dtq](https://github.com/jangorecki/dtq)包,它也比使用`trace`更快. (2认同)