I think this should do the trick:

test <- function() {
   b <- 3
   stop('error here')
}

df3 <- function() {
  saveRDS(sys.frame(1), file = "dump.rds")
}

options(error = df3)
test()
# Error in test() : error here


# Read in the file and examine the environment
frame <- readRDS("dump.rds")
ls(frame)
# [1] "b"
frame$b
# [1] 3


-Winston



On Tue, Apr 29, 2014 at 2:34 AM, Jannis <bt_jan...@yahoo.de> wrote:

> 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
>    plot(notavailable)
> }
>
>
> dump.frames.mod = function() {
>     save(list=ls(parent.frame(1)), file='dummy.RData')
> }
> options(error = quote({dump.frames.mod()}))
>
> test()
>
> 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.
>
>
> Thanks
> Jannis
>
> ______________________________________________
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

        [[alternative HTML version deleted]]

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to