undebug all functions

前端 未结 3 1840
暗喜
暗喜 2021-02-03 22:27

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 fu

3条回答
  •  一向
    一向 (楼主)
    2021-02-03 23:06

    Here is one option, assuming that the functions you are debugging are in the workspace or global environment. Any particular environment can be specified so it is adaptable but this isn't going to be something that works for any function in all loaded packages in a single go.

    First illustrate via a couple of functions in the global environment:

    > bar <- function() {}
    > foo <- function() {}
    

    Use lsf.str() to return the functions in the workspace (for use later we unclass() this and convert it to a list):

    > funlist <- as.list(unclass(lsf.str()))
    > funlist
    [[1]]
    [1] "bar"
    
    [[2]]
    [1] "foo"
    

    Next, produce an indicator for these functions as to whether they are debugged:

    > debugged <- sapply(funlist, isdebugged)
    > debugged
    [1] FALSE FALSE
    

    OK, so debug() one of the functions and rerun:

    > debug(bar)
    > 
    > debugged <- sapply(funlist, isdebugged)
    > debugged
    [1]  TRUE FALSE
    

    Finally sapply() over funlist functions that are debugged applying undebug() to them:

    > sapply(funlist[debugged], undebug)
    [[1]]
    NULL
    

    This of course could be encapsulated into a function

    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
    

    One type of debugging not picked up by isdebugged() is that enacted via debugonce():

    > debug(bar)
    > isdebugged(bar)
    [1] TRUE
    > undebugFuns()
    Un-debugging: bar
    > debugonce(bar)
    > isdebugged(bar)
    [1] FALSE
    

    Which just goes to make Josh's point in his Answer again.

提交回复
热议问题