Xu Wang Xu Wang - 1 month ago 11
R Question

Why is behavior in R CMD BATCH different for options(error = utils::dump.frames)?

This is a followup to this question: R CMD BATCH or Rscript with stop on error

If in my .Rprofile I have

options(error = utils::dump.frames)


then R CMD BATCH does not stop on errors.

If I run
R CMD BATCH
on a file containing the following:

stop("I really mean stop!")
cat("no, I dont want this printed")


The
cat
command is still executed.

But when I do
source
on that file, R stops. Why is this true and how do I make the behavior consistent?

Answer

1. Regarding the first part of your question:

?dump.frames says:

"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."

That's why R CMD BATCH does not stop on errors.

If you want to stop execution too then use (taken from the help example above!):

## A possible setting for non-interactive sessions
options(error = quote({dump.frames(to.file = TRUE); q(status = 1)}))

2. Regarding the second part of your question (code example with stop):

I suppose your dump.frames error handler is active too since to entered it into your .Rprofile and therefore the code execution continues for the reason explained above.

3 Why does R stop immediately when sourcing the same code?

Sorry, no idea (so my answer reamins incomplete for now)

4. How do I make the behavior consistent?

By using the error handler of 1.) above the code will always stop in case of an error.