2013-04-10 73 views
9

为什么trace(...,edit = TRUE)不起作用要临时编辑封装函数func的正文,我经常使用trace(func, edit=TRUE)。出于某种原因,不过,R是不是让我做这件事的时候func[.data.table当... = [.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.0data.table_1.8.8,并得到了同样的错误我是使用设置options(editor="emacs")options(editor="notepad")或使用R GUI的默认编辑器。

+2

它为我在2.15.3与data.table_1.8.6,Windows 7的 – 2013-04-10 17:02:10

+3

不过,我得到了同样的错误升级到1.8.8后, 。 – 2013-04-10 17:10:49

+0

@MatthewPlourde - 谢谢!这真的有助于缩小问题范围。 – 2013-04-10 17:12:06

回答

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 

然后,备案,这里是1.8.6版本[.data.table的甲缩醛(针对跟踪作品)和版本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) 
+2

如果有人对'[.data.table'的预配置* trace *感兴趣,我将它构建为[dtq](https://github.com/jangorecki/dtq)包,它比使用'trace' 。 – jangorecki 2015-06-01 10:07:02