【问题标题】:Why does trace(..., edit=TRUE) not work when ... = [.data.table为什么当 ... = [.data.table 时 trace(..., edit=TRUE) 不起作用
【发布时间】:2013-04-10 16:47:51
【问题描述】:

为了临时编辑一个封装函数func的主体,我经常使用trace(func, edit=TRUE)。但是,由于某种原因,当 func[.data.table 时,R 不允许我这样做:

## 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.0data.table_1.8.8,无论我使用 set options(editor="emacs")options(editor="notepad") 还是使用 R GUI 的默认编辑器,都会遇到相同的错误。

【问题讨论】:

  • 它适用于我的 2.15.3 和 data.table_1.8.6,Windows 7
  • 但是,我升级到 1.8.8 后遇到同样的错误。
  • @MatthewPlourde -- 谢谢!这确实有助于缩小问题范围。
  • 我在进一步升级到 1.8.9 后也遇到了同样的错误。尚未尝试 1.8.6 或 1.8.7 来确定哪个版本首先表现出改变的行为 w.r.t。 trace().
  • 嗨。浏览了比较 1.8.6 和 1.8.8 的提交日志和新闻,唯一想到的是 rolltolastdrop=NULL 之后移动,并且添加了 rollends通过调用c() 来设置样式。可能与此有关,因为错误消息可能提到了参数和默认值。

标签: r data.table trace


【解决方案1】:

这显然是由于最近在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) 

【讨论】:

  • 如果有人对 [.data.table 的预配置 trace 感兴趣,我将其构建为 dtq 包,它也比使用 trace 更快。
猜你喜欢
  • 1970-01-01
  • 2020-09-07
  • 2015-06-05
  • 1970-01-01
  • 2013-06-13
  • 2011-09-11
  • 1970-01-01
  • 2015-10-14
  • 1970-01-01
相关资源
最近更新 更多