When a function I have stop()s, I'd like it to return its evaluation
frame, but not halt execution of the script. In experimenting with this,
I became confused with dump.frames. From ?dump.frames:
If ?dump.frames? is installed as the error handler, execution will
continue even in non-interactive sessions. See the examples for
how to dump and then quit.
Suppose I save the following script to "dump-test.R":
options(error=dump.frames)
cat("interactive:", interactive(), "\n")
f <- function() {
stop("dump-test-error")
cat("execution continues within f\n")
}
f()
cat("execution continues outside of f\n")
if(exists("last.dump"))
cat("last.dump is available\n")
From an interactive R prompt, execution is halted at 'stop':
R> source('dump-test.R')
interactive: TRUE
Error in f() : dump-test-error
Using Rscript, execution continues depending on whether you source() the
file with the -e flag, or pass the file as an argument.
matt at pal ~$ Rscript dump-test.R
interactive: FALSE
Error in f() : dump-test-error
execution continues outside of f
last.dump is available
matt at pal ~$ Rscript -e "source('dump-test.R')"
interactive: FALSE
Error in f() : dump-test-error
Calls: source -> eval.with.vis -> eval.with.vis -> f
It seems that interactiveness (as tested by interactive()) doesn't come
into play, yet execution does *not* always continue. What am I missing?
Alternative solutions are also welcome.
-Matt
P.S. There is a typo in the help file: "The dumped object contain the
call stack..." should read "The dumped object contains the call
stack...".
> sessionInfo()
R version 2.13.0 alpha (2011-03-18 r54865)
Platform: x86_64-unknown-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=C LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] tools_2.13.0
--
Matthew S Shotwell Assistant Professor School of Medicine
Department of Biostatistics Vanderbilt University