move up a frame, debug R environment

后端 未结 3 2114
终归单人心
终归单人心 2021-01-31 10:18

When debugging a function, I would like to move up to the parent frame and look at some variables there. How do I do this?

Here is a sample:

f <- fun         


        
3条回答
  •  不知归路
    2021-01-31 11:01

    Just call on.exit(browser()) when browsing f's body and you'll return back to it after you're done with g()

    See output copied from the console below :

    > f <- function() {
    +   x <-1
    +   g(x+1)
    + }
    > g <- function(z) {
    +   y = z+2
    +   return(y)
    + }
    > debug("f")
    > debug("g")
    > f()
    debugging in: f()
    debug at #1: {
        x <- 1
        g(x + 1)
    }
    Browse[2]> on.exit(browser()) # browser() will be run in this environment just before we exit
    Browse[2]> 
    debug at #2: x <- 1
    Browse[2]> 
    debug at #3: g(x + 1)
    Browse[2]> 
    debugging in: g(x + 1)
    debug at #1: {
        y = z + 2
        return(y)
    }
    Browse[3]> 
    debug at #2: y = z + 2
    Browse[3]> 
    debug at #3: return(y)
    Browse[3]> 
    exiting from: g(x + 1)
    Browse[2]> x # we're back to f so we can investigate x
    [1] 1
    Browse[2]> 
    exiting from: f()
    [1] 4
    

提交回复
热议问题