在R中设置调试标志的列表函数 [英] Listing functions with debug flag set in R

查看:102
本文介绍了在R中设置调试标志的列表函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在R中找到一个全局对象 isdebugged()。我的场景是我有一些函数调用其他函数,我已经编写了,并且在调试过程中为 debug()打开和关闭了不同的函数。但是,我可能会忽略哪些功能被设置为调试。当我忘记并开始一个循环时,我可能会得到更多的输出(令人讨厌,但并不可怕),或者当需要某些输出时(坏),我可能得不到输出。



我目前的方法是使用类似于下面的函数,我可以用 listDebugged(ls())来调用它,或者列出加载库中的项目(例子下面)。这可能就足够了,但它需要我用工作空间中的每个函数的列表或在加载的包中调用它。我可以包装另一个获得这些功能的函数。看来应该有一种更简单的方法来直接询问调试功能,或者查询一些隐藏的部分环境,在该环境中存储设置了调试标志的函数列表。



因此,有两个部分的问题:


  1. 是否存在一个简单的调用,用于通过调试标志查询函数设置?

  2. 如果没有,那么是否有任何我忽略的技巧?例如,如果一个包中的某个函数掩盖了另一个包,那么我怀疑我可能会返回一个令人误解的结果。 我知道还有另一个包我可以尝试的方法是在函数中包装 debug undebug ,这些函数还维护调试函数名称的隐藏列表。我还没有确信这是一件安全的事情。



    更新(8/5/11):我搜索过,没有发现早期的问题。然而,SO的相关问题列表显示,类似的早期问题,尽管该问题的答案中的函数比@cbeleites提供的函数更详细和更慢。旧的问题也没有提供任何代码,而我做到了。 :)



    代码:

      listDebugged<  -  function项目){
    isFunction< - vector(length = length(items))
    isDebugged< - vector(length = length(items))

    for(ix in seq_along (items)){
    isFunction [ix]< - is.function(eval(parse(text = items [ix])))
    }

    for(ix in (isFunction == 1)){
    isDebugged [ix]< - isdebugged(eval(parse(text = items [ix])))
    }
    names(isDebugged) - 项目
    返回(isDebugged)


    示例用法
    listDebugged(ls())
    库(MASS)
    debug( write.matrix)
    listDebugged(ls(package:MASS))


    解决


    $ b

      ls.deb<  -  function(items = search()){
    .ls.deb< - function(i){
    f< -ls(i)
    f< -mget(f,as.environment(i), mode =functio ()函数()NULL)


    ##返回一个未调试的函数if(length(f)== 0)
    return(NULL)

    f< -f [sapply(f,isdebugged)]
    f< - names(f)$ (f,function(f)isdebugged(get(f)))

    $ b ##现在检查被调试的函数是否被一个未被调试的
    掩码< - !sapply b
    ##生成漂亮的输出格式:
    ##package :: function和(package :: function)用于掩码调试函数
    if(length(f)> 0){
    if(grepl('^ package:',i)){
    i < - gsub('^ package:','',i)
    f< - paste i,f,sep =::)
    }

    f [masked]< - paste((,f [masked],),sep =)

    f
    } else {
    NULL
    }
    }


    函数< - lapply(items, .ls.deb)
    unlist(functions)
    }




    • 我选择了一个不同的名称,因为输出格式只是被调试的函数(否则我很容易得到数千个函数)。
    • 输出格式为 package :: function (或者更确切地说是 namespace :: function ,但包很快就会有命名空间)。如果被调试的函数被屏蔽,输出是(package :: function)

    • li>缺省值正在查看整个搜索路径


    I am trying to find a global counterpart to isdebugged() in R. My scenario is that I have functions that make calls to other functions, all of which I've written, and I am turning debug() on and off for different functions during my debugging. However, I may lose track of which functions are set to be debugged. When I forget and start a loop, I may get a lot more output (nuisance, but not terrible) or I may get no output when some is desired (bad).

    My current approach is to use a function similar to the one below, and I can call it with listDebugged(ls()) or list the items in a loaded library (examples below). This could suffice, but it requires that I call it with the list of every function in the workspace or in the packages that are loaded. I can wrap another function that obtains these. It seems like there should be an easier way to just directly "ask" the debug function or to query some obscure part of the environment where it is stashing the list of functions with the debug flag set.

    So, a two part question:

    1. Is there a simpler call that exists to query the functions with the debug flag set?
    2. If not, then is there any trickery that I've overlooked? For instance, if a function in one package masks another, I suspect I may return a misleading result.

    I realize that there is another method I could try and that is to wrap debug and undebug within functions that also maintain a hidden list of debugged function names. I'm not yet convinced that's a safe thing to do.

    UPDATE (8/5/11): I searched SO, and didn't find earlier questions. However, SO's "related questions" list has shown that an earlier question that is similar, though the function in the answer for that question is both more verbose and slower than the function offered by @cbeleites. The older question also doesn't provide any code, while I did. :)

    The code:

    listDebugged    <- function(items){
        isFunction  <- vector(length = length(items))
        isDebugged  <- vector(length = length(items))
    
        for(ix in seq_along(items)){
            isFunction[ix]  <- is.function(eval(parse(text = items[ix])))
        }
    
        for(ix in which(isFunction == 1)){
            isDebugged[ix]  <- isdebugged(eval(parse(text = items[ix])))
        }
        names(isDebugged)   <- items
        return(isDebugged)
    }
    
    # Example usage
    listDebugged(ls())
    library(MASS)
    debug(write.matrix)
    listDebugged(ls("package:MASS"))
    

    解决方案

    Here's my throw at the listDebugged function:

    ls.deb  <- function(items = search ()){
      .ls.deb <-  function (i){
        f <- ls (i)
        f <- mget (f, as.environment (i), mode = "function",
    
                   ## return a function that is not debugged
                   ifnotfound = list (function (x) function () NULL)
                   )
    
        if (length (f) == 0)
          return (NULL)
    
        f <- f [sapply (f, isdebugged)]
        f <- names (f)
    
        ## now check whether the debugged function is masked by a not debugged one
        masked <- !sapply (f, function (f) isdebugged (get (f)))
    
        ## generate pretty output format:
        ## "package::function"  and "(package::function)" for masked debugged functions
        if (length (f) > 0) {
          if (grepl ('^package:', i)) {
            i <- gsub ('^package:', '', i)
            f <- paste (i, f, sep = "::")
          }
    
          f [masked] <- paste ("(", f [masked], ")", sep = "")
    
          f
        } else {
          NULL
        }
      }
    
    
      functions <- lapply (items, .ls.deb)
      unlist (functions)
    }
    

    • I chose a different name, as the output format are only the debugged functions (otherwise I easily get thousands of functions)
    • the output has the form package::function (or rather namespace::function but packages will have namespaces pretty soon anyways).
    • if the debugged function is masked, output is "(package::function)"
    • the default is looking throught the whole search path

    这篇关于在R中设置调试标志的列表函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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