Why does trace(…, edit=TRUE) not work when … = [.d

2019-04-22 05:10发布

To temporarily edit the body of a packaged function func, I frequently use trace(func, edit=TRUE). For some reason, though, R isn't letting me do this when func is [.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

Questions: What might be causing this error? What other functions also trigger it? For such functions, is there some alternative work-around that will allow me to edit them?

FWIW, this doesn't seem to be some general issue with functions in data.table's namespace (see e.g. #1 below) nor is it an issue with subset methods in general (see e.g. #2 below).

## (#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"

I am running R-3.0.0 and data.table_1.8.8 on a Windows XP machine, and get the same error whether I use set options(editor="emacs"), options(editor="notepad") or use the R GUI's default editor.

1条回答
贪生不怕死
2楼-- · 2019-04-22 06:09

This is apparently being caused by the recent addition of curly braces (i.e. {}) at one place in data.table's formal argument list.

First, a MRE to show that braces really do cause trace(..., edit=TRUE) to choke:

## 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

Then, for the record, here are the formals for [.data.table in versions 1.8.6 (for which tracing works) and version 1.8.8 (for which it doesn't):

## 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) 
查看更多
登录 后发表回答