r - 调试所有功能

标签 r debugging

考虑我们已经为几个函数调用了debug()以便在它们上创建一个断点。当我们找到并解决该错误时,是否仍然可以通过单个命令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!

您可能会在下面看到接受的答案。任何对此问题无法解决的情况,或更干净的版本,都非常欢迎。

最佳答案

这是我的解决方案...

编辑:修改为处理在命名空间中查找对象。由于我不太真正理解用于操纵/查询 namespace 的方法,而且由于我反复试验,因此代码已经变得有些杂乱。较干净的版本将受到欢迎。几乎肯定还有其他一些失败的案例。

## 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))
}

该代码也发布在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

在这种情况下,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")

请注意,从调试的 Angular 来看,read.ilmn()read.ilmn("a.txt")的行为似乎有所不同(我不明白为什么...)

关于r - 调试所有功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12807237/

相关文章:

r - 找到三个或更多连续的负数并从数据框中删除行

r - 根据键在数据框中汇总值

r - 使用 LaunchControl 运行 .Rnw 脚本时出现 texi2dvi() 错误

r - doWithOneRestart 错误

ruby - binding.pry 和 Pry.start 之间有什么区别?

r - 用R中的数据点名称制作一维图

r - 用 "call by reference"修改对象的内容

c - 编辑 makefile 以允许在 linux 中进行调试

c - 断点如何工作?

java - 如何为 Maven Java 项目设置 Maven 调试配置文件