为什么trace(...,edit = TRUE)在... = [.data.table [英] Why does trace(..., edit=TRUE) not work when ... = [.data.table
问题描述
要暂时编辑一个打包函数的主体 func
,我经常使用 trace(func,edit = TRUE)
。因为某种原因,R不允许我在 func
是 [。data.table
:
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,这似乎不是一些一般的问题, strong> data.table 的命名空间(参见下面的#1
)也不是子集方法的问题(参见例如#2
)。
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"
我正在运行 R-3.0.0 $ c $在Windows XP计算机上,如果使用set
选项(editor =emacs),则会出现相同的错误,并且会出现同样的错误:c> and
, data.table_1.8.8
选项(编辑器=notepad)
或使用R GUI的默认编辑器。
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.
推荐答案
这显然是由于最近在<$ c $中的一个地方添加了大括号(即 {}
c> data.table 的形式参数列表。
This is apparently being caused by the recent addition of curly braces (i.e. {}
) at one place in data.table
's formal argument list.
首先,MRE显示大括号真的导致 trace(...,edit = TRUE)
到choke:
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
然后,对于记录,在版本1.8.6(对于其跟踪工作)和版本1.8.8(对于它不是)中:c $ c> [。data.table ]:
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)
这篇关于为什么trace(...,edit = TRUE)在... = [.data.table的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!