R解除所有功能 [英] R undebug all functions

查看:127
本文介绍了R解除所有功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑到我们已经为几个函数调用了 debug()来为它们创建一个断点。当我们发现并解决这个错误时,还有没有一个 undebug()已经由 debug()一个命令?



这是一个很好的基准,看看你提出的方法是否真的很完美:

 >图书馆(limma)#bioconductor 
> debug(read.ilmn)
> read.ilmn(a.txt)#如果此文件不存在,则无问题
浏览[2]> debug(.read.oneilmnfile)#这是read.ilmn()的调试浏览器
浏览[2]> Q#退出调试浏览器
> undebug.all()#这里运行你提出的函数来解除一切!
> read.ilmn(a.txt)
#现在如果调试浏览器没有启动,你很幸运通过这个测试!

您可能会在下面看到接受的答案。任何情况下,这个答案不起作用,或更清洁的版本是非常受欢迎的。

解决方案

这是我的解决方案...



编辑:修改为处理在命名空间中查找对象。代码已经变得有些了不起,因为我并没有真正理解操作/查询命名空间的方法,而且因为我是通过试用和错误的工作。清洁版本是受欢迎的。几乎肯定会有其他角落的情况会失败。

  ##返回对象的名称(从

g <= if(is.null(ns)),它们是函数,并且具有调试标志设置
isdebugged_safe< - (x,ns = )get(x)else getFromNamespace(x,ns)
is.function(g)&& (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:, ))
##查找附加的命名空间
##(有没有更好的方法来测试一个
##命名空间是否存在一个给定的名称?)
ns< - unlist(sapply(gsub(package:,,where),
function(x){
if(inherits({n< - try(getNamespace(x),silent = },
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重叠
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)
##现在调试命名空间
invisible(mapply(function(ns,fun)){
undebug (getFromNamespace(fun,ns))
},名称(aa $ ns),aa $ ns))
}

代码也发布在 http:// www。 math.mcmaster.ca/bolker/R/misc/undebug_all.R



示例:

 库(nlme)
调试(lme)
##定义函数
源(url(http://www.math.mcmaster.ca /bolker/R/misc/undebug_all.R))
undebug_all()
fm1 < - lm e(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)

注意 read.ilmn() read.ilmn(a.txt)似乎与调试的立场不同不明白为什么...)


Consider we have called debug() for several functions to make a breakpoint on them. When we find and solve the bug, is there anyway to undebug() all functions already marked by debug() by a single command?

Here is a good benchmark to see if your proposed method really works perfect:

> 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!

You may see the accepted answer below. Any case that this answer does not work, or cleaner versions are more than welcome.

解决方案

This was my solution ...

edit: revised to deal with finding objects in namespaces. The code is already getting a little bit crufty, since I don't really understand the methods for manipulating/querying namespaces all that well, and since I was working by trial and error. Cleaner versions would be welcome. There are almost certainly other corner cases that will fail.

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

The code is also posted at http://www.math.mcmaster.ca/bolker/R/misc/undebug_all.R

Example:

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

In this case lme runs without entering the debugger.

Another, harder example:

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")

Note that read.ilmn() and read.ilmn("a.txt") appear to behave differently from a debugging standpoint (I don't understand why ...)

这篇关于R解除所有功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆