[Rd] access environment in which an error occurred

Jannis bt_jannis at yahoo.de
Tue Apr 29 09:34:31 CEST 2014

Dear R developers,

i have already send the question below to r-help but got no responses. 
Perhaps it is more suitable for r-devel due to its rather technical 
level. It would really help me to find a solution (or to find out that 
there is none).

Is there any way to access/print/save the content of an environment in
which an error occoured?

Imagine the following testcase:

test =  function() {
    b =  3

dump.frames.mod = function() {
     save(list=ls(parent.frame(1)), file='dummy.RData')
options(error = quote({dump.frames.mod()}))


The call to plot() inside test() here would create an error in which 
case I would like
to save the whole environment in which it occurred (in this case only
the object b) to some file for later debugging. In the way I tried to
implement it above, only the content of the global environment is saved
(probably because dump.frames.mod is called from this environment). Is
there any way to save the content of the environment down in the stack
where the error actually occurred?

I know about the dump.frames()
function which somehow does not work this case. I have implemented 
something like:

   dump.frames.mod = function(file.name, file.results)
       file.name.error = 'dummy'
       cat(paste('\nSaving  workspace to file: \n', file.name.error, 
'.rda\n', sep=''))
       dump.frames(dumpto = file.name.error, to.file = TRUE)
       quit(save = 'no', status = 10)
   options(error = quote({dump.frames.mod()}))

This, however, seems to hang my R session in case of an error. I do the 
whole thing to debug Code run remotely and non interactively on a 
cluster. In the logfiles produced I get the message that an error 
occurred (the result of cat(paste('\nSaving  workspace to file: \n', 
file.name.error, '.rda\n', sep=''))) but neither a file is created nor 
the R process is stopped. The cluster process just keeps on running with 
no indication that something actually happens. My impression is that 
this may be due to the huge size of the current R workspace as the 
dump.frames method above usually works smoothly when I run my code with 
much smaller test files.

So the first solution is basically a hack to avoid the dump.frames 
thing. A solution to any of the issues would be great.


More information about the R-devel mailing list