【发布时间】:2016-12-28 20:25:38
【问题描述】:
我想为debug() 函数编写一个包装器,以便在需要时删除所有调试标志。
对于搜索路径中的函数,它很简单。
.debugged <- NULL
debug.wrapper <- function(fun){
f <- deparse(substitute(fun))
.debugged <<- unique(c(.debugged, f))
debug(f)
}
debug.wrapper.off <- function() {
z=sapply(.debugged, undebug)
.debugged <<- NULL
}
之所以有效,是因为我可以使用函数符号的字符版本。
f <- function() print("hello")
debug.wrapper(f)
isdebugged(f)
# [1] TRUE
debug.wrapper.off()
isdebugged(f)
# [1] FALSE
无论如何使用命名空间它都不起作用:
debug.wrapper(tools:::psnice)
# Error in debug(f) could not find function "tools:::psnice"
还有:
debug(substitute(tools:::psnice))
# Error in debug(fun, text, condition) : argument must be a function
如何存储函数符号以供以后重复使用?
【问题讨论】:
-
试试
match.fun,两者都接受。将行f <- deparse(substitute(fun))更改为f <- match.fun(fun)似乎可以解决问题。 -
@RichScriven:谢谢,我以不同的方式成功了,因为我意识到连接函数符号不涉及制作函数副本。