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 <- function() {
x <-1
g(x+1)
}
g <- function(z) {
y = z+2
return(y)
}
I then debug both functions using debug("g")
and debug("f")
. When I end up in g
at the Browser>
, I would like to move back up to f
to examine x.
Thanks
In R terminology, you are wanting to investigate the parent frame of
g()
's evaluation environment (i.e. the environment in whichg
was called). The functions for doing that are documented in the help page for?sys.parent
.Once your browser indicates that you are
'debugging in g(x + 1)'
, you can do the following. (Thanks to Joshua Ulrich for suggestingwhere
to help locate ones position in the call stack .)EDIT: To understand the collection of functions described in
?sys.parent
, it's probably worth noting thatparent.frame()
is (basically) shorthand forsys.frame(sys.parent(1))
. If you find yourself in an evaluation environment farther down a call stack (as revealed bywhere
, for instance), you can reach into environments farther back up the call stack (say two steps up) by eitherparent.frame(2)
orsys.frame(sys.parent(2))
.You can use
recover
(it is often used to debug code after an actual error, viaoptions(error=utils::recover)
, but it can be called directly).