The Art of R Programming

(WallPaper) #1
When we callf(), it in turn callsg(), which then callsh(). In the debug-
ging setting, say we are currently about to execute thereturn()withing().
We want to know the values of the local variables of the current function, say
the variablesaa,b, andaab. And while we’re ing(), we also wish to know the
values of the locals inf()at the time of the call tog(), as well as the values of
the global variables. Our functionshowframe()will do all this.
Theshowframe()function has one argument,upn, which is the number of
frames to go up the call stack. A negative value of the argument signals that
we want to view the globals—the top-level variables.
Here’s the code:

# shows the values of the local variables (including arguments) of the
# frame upn frames above the one from which showframe() is called; if
# upn < 0, the globals are shown; function objects are not shown
showframe <- function(upn) {
# determine the proper environment
if (upn < 0) {
env <- .GlobalEnv
} else {
env <- parent.frame(n=upn+1)
}
# get the list of variable names
vars <- ls(envir=env)
# for each variable name, print its value
for (vr in vars) {
vrg <- get(vr,envir=env)
if (!is.function(vrg)) {
cat(vr,":\n",sep="")
print(vrg)
}
}
}

Let’s try it out. Insert some calls intog():

>g
function(aa) {
b<-2
showframe(0)
showframe(1)
aab <- h(aa+b)
return(aab)
}

Now run it:

> f()
aa:

158 Chapter 7

Free download pdf