考虑我们已经调用debug()了几个函数来对它们进行断点.当我们找到并解决这个bug时,是否还有一个命令undebug()标记的所有函数debug()?
这是一个很好的基准,看看你提出的方法是否真的完美:
> library(limma) # bioconductor
> debug(read.ilmn)
> read.ilmn("a.txt") # No problem if this file does not exist
Browse[2]> debug(.read.oneilmnfile) # This is the debug browser for read.ilmn()
Browse[2]> Q # To exit debug browser
> undebug.all() # Here run your proposed function to undebug everything!
> read.ilmn("a.txt")
# Now if the debug browser is not started, you are lucky to pass this test!
Run Code Online (Sandbox Code Playgroud)
您可能会在下面看到接受的答案.任何情况下,这个答案不起作用,或更干净的版本是非常受欢迎的.
Ben*_*ker 15
这是我的解决方案......
编辑:修改以处理在命名空间中查找对象.代码已经变得有点狡猾,因为我并不真正理解操作/查询命名空间的方法,因为我正在通过反复试验来工作.更清洁的版本将是受欢迎的.几乎可以肯定其他角落案例都会失败.
## return the names of the objects (from a vector of list of
## names of objects) that are functions and have debug flag set
isdebugged_safe <- function(x,ns=NULL) {
g <- if (is.null(ns)) get(x) else getFromNamespace(x,ns)
is.function(g) && isdebugged(g)
}
which_debugged <- function(objnames,ns=NULL) {
if (!length(objnames)) return(character(0))
objnames[sapply(objnames,isdebugged_safe,ns=ns)]
}
all_debugged <- function(where=search(), show_empty=FALSE) {
ss <- setNames(lapply(where,function(x) {
which_debugged(ls(x,all.names=TRUE))
}),gsub("package:","",where))
## find attached namespaces
## (is there a better way to test whether a
## namespace exists with a given name??)
ns <- unlist(sapply(gsub("package:","",where),
function(x) {
if (inherits({n <- try(getNamespace(x),silent=TRUE)},
"try-error")) NULL else x
}))
ss_ns <- setNames(lapply(ns,function(x) {
objects <- ls(getNamespace(x),all.names=TRUE)
which_debugged(objects,ns=x)
}),ns)
if (!show_empty) {
ss <- ss[sapply(ss,length)>0]
ss_ns <- ss_ns[sapply(ss_ns,length)>0]
}
## drop overlaps
for (i in names(ss))
ss_ns[[i]] <- setdiff(ss_ns[[i]],ss[[i]])
list(env=ss,ns=ss_ns)
}
undebug_all <- function(where=search()) {
aa <- all_debugged(where)
lapply(aa$env,undebug)
## now debug namespaces
invisible(mapply(function(ns,fun) {
undebug(getFromNamespace(fun,ns))
},names(aa$ns),aa$ns))
}
Run Code Online (Sandbox Code Playgroud)
该代码也发布在http://www.math.mcmaster.ca/bolker/R/misc/undebug_all.R
例:
library(nlme)
debug(lme)
## define functions
source(url("http://www.math.mcmaster.ca/bolker/R/misc/undebug_all.R"))
undebug_all()
fm1 <- lme(distance ~ age, data = Orthodont) # from ?lme
Run Code Online (Sandbox Code Playgroud)
在这种情况下lme运行而不进入调试器.
另一个更难的例子:
library(limma)
source(url("http://www.math.mcmaster.ca/bolker/R/misc/undebug_all.R"))
debug(read.ilmn)
debug(limma:::.read.oneilmnfile)
all_debugged()
undebug_all()
read.ilmn()
read.ilmn("a.txt")
Run Code Online (Sandbox Code Playgroud)
请注意,read.ilmn()并且read.ilmn("a.txt")看起来与调试的观点有所不同(我不明白为什么......)
这是一个选项,假设您正在调试的函数位于工作空间或全局环境中.可以指定任何特定的环境,使其具有适应性,但这不会适用于单个go中所有已加载包中的任何功能.
首先通过全局环境中的几个函数来说明:
> bar <- function() {}
> foo <- function() {}
Run Code Online (Sandbox Code Playgroud)
使用lsf.str()工作区中的返回功能(以备后用,我们unclass()这一点,并把它转换到一个列表):
> funlist <- as.list(unclass(lsf.str()))
> funlist
[[1]]
[1] "bar"
[[2]]
[1] "foo"
Run Code Online (Sandbox Code Playgroud)
接下来,为这些函数生成一个指示器,以确定它们是否已被调试:
> debugged <- sapply(funlist, isdebugged)
> debugged
[1] FALSE FALSE
Run Code Online (Sandbox Code Playgroud)
好的,所以debug()其中一个功能并重新运行:
> debug(bar)
>
> debugged <- sapply(funlist, isdebugged)
> debugged
[1] TRUE FALSE
Run Code Online (Sandbox Code Playgroud)
最后,sapply()在funlist被调试的应用功能,undebug()对他们说:
> sapply(funlist[debugged], undebug)
[[1]]
NULL
Run Code Online (Sandbox Code Playgroud)
这当然可以封装成一个函数
undebugFuns <- function() {
funs <- unclass(lsf.str())
dbg <- sapply(funs, isdebugged)
if(isTRUE(any(dbg))) {
writeLines(paste("Un-debugging:", funs[dbg]))
sapply(funs[dbg], undebug)
} else {
writeLines(paste("Nothing to debug"))
}
invisible()
}
> debug(bar)
> undebugFuns()
Un-debugging: bar
Run Code Online (Sandbox Code Playgroud)
未获取的一种调试isdebugged()是通过debugonce()以下方式实施的:
> debug(bar)
> isdebugged(bar)
[1] TRUE
> undebugFuns()
Un-debugging: bar
> debugonce(bar)
> isdebugged(bar)
[1] FALSE
Run Code Online (Sandbox Code Playgroud)
这只是让Josh再次回答他的答案.
| 归档时间: |
|
| 查看次数: |
2914 次 |
| 最近记录: |