Update Nov. 20, 2016: Note that it is not an R bug (see answer of Martin Maechler). I did not change my answer for reproducibility. The described work around still applies.
Summary
I think dump.frames(to.file = TRUE)
is currently an anti pattern (or probably a bug) in R if you want to debug errors of batch jobs in a new R session.
You should better replace it with
dump.frames()
save.image(file = "last.dump.rda")
or
options(error = quote({dump.frames(); save.image(file = "last.dump.rda")}))
instead of
options(error = dump.frames)
because the global environment (.GlobalEnv
= the user workspace you normally create your objects) is included then in the dump while it is missing when you save the dump directly via dump.frames(to.file = TRUE)
.
Impact analysis
Without the .GlobalEnv
you loose important top level objects (and their current values ;-) to understand the behaviour of your code that led to an error!
Especially in case of errors in "non-interactive" R batch jobs you are lost without .GlobalEnv
since you can debug only in a newly started (empty) interactive workspace where you then can only access the objects in the call stack frames.
Using the code snippet above you can examine the object values that led to the error in a new R workspace as usual via:
load(file = "last.dump.rda")
debugger(last.dump)
Background
The implementation of dump.frames
creates a variable last.dump
in the workspace and fills it with the environments of the call stack (sys.frames()
. Each environment contains the "local variables" of the called function). Then it saves this variable into a file using save()
.
The frame stack (call stack) grows with each call of a function, see ?sys.frames
:
.GlobalEnv is given number 0 in the list of frames. Each subsequent
function evaluation increases the frame stack by 1 and the [...] environment for evaluation of that function are returned by [...] sys.frame with the appropriate index.
Observe that the .GlobalEnv
has the index number 0.
If I now start debugging the dump produced by the code in the question and select the frame 1 (not 0!) I can see a variable parentenv
which points (references) the .GlobalEnv
:
Browse[1]> environmentName(parentenv)
[1] "R_GlobalEnv"
Hence I believe that sys.frames
does not contain the .GlobalEnv
and therefore dump.frames(to.file = TRUE)
neither since it only stores the sys.frames
without all other objects of the .GlobalEnv
.
Maybe I am wrong, but this looks like an unwanted effect or even a bug.
Discussions welcome!
References
https://cran.r-project.org/doc/manuals/R-exts.pdf
Excerpt from section 4.2 Debugging R code (page 96):
Because last.dump can be looked at later or even in another R session,
post-mortem debug- ging is possible even for batch usage of R. We do
need to arrange for the dump to be saved: this can be done either
using the command-line flag
--save to save the workspace at the end of the run, or via a setting such as
options(error = quote({dump.frames(to.file=TRUE); q()}))
ls(parentenv)
orget("a.local.value", env = parentenv)
. – Lonne.GlobalEnv
is not contained indump.frames
. – IllbodingenvironmentName(parentenv)
). You can recognize this at the warning issued: "Warning message: In get(.obj, envir = dump[[.selection]]) : restarting interrupted promise evaluation". I thinkdump.frames
with the parameterto.file = TRUE
is a big anti pattern (I will explain this in an answer later - discussions are welcome) – Illboding