2013-04-10 30 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