[R] Was confused with options(error = expression(NULL)) in example(stop)

Suharto Anggono Suharto Anggono suharto_anggono at yahoo.com
Wed Feb 6 08:36:08 CET 2013

When I wrote the message earlier, I didn't actually try running example for function 'stop' from R CMD BATCH.

Now, I have run a script with this content from R CMD BATCH.


It turns out that execution of the example still didn't continue after first error. But, there was no "Execution halted" and the time was printed.

Then, I copied the example to a script and ran it from R CMD BATCH. That way, the example was executed until the end. Error messages were printed. So, that was "don't stop on stop(.)".

I get the same effect if I copy the example, then paste it at the R prompt (normal, with options(error=NULL)).

When I tried using source() on a script in an interactive session, the execution also didn't continue after first error.

I am quite surprised that, running script A and script B below from R CMD BATCH give different results. For script A, stop('b') is skipped.

# script A
options(error = expression(NULL))
stop('a'); stop('b')

# script B
options(error = expression(NULL))

In interactive session, running
stop('a'); stop('b')
at the R prompt also skips stop('b') .

I also experimented with
options(error = function() cat("ha\n"))

After experimenting, I see that, if options(error=) is specified to other than NULL, what happens in R on error in interactive mode and in batch mode are not different.

On error, if there is no 'tryCatch' or the like, this happens.
* Error message is printed.
* What is specified by options(error=) is done.
options(error=NULL) means
- "do nothing" in interactive mode
- "quit immediately" in batch mode
* If not quit, prompt is given to accept next command. The rest (if any) of the command that gives error is discarded.

So, in interactive mode, the default (options(error=NULL)) is effectively options(error=expression(NULL)). That's why setting options(error=expression(NULL)) in interactive mode seems to have no effect.

Now, I just have understood a little of this explanation in the documentation of 'stop'.

     The error action is controlled by error handlers established
     within the executing code and by the current default error handler
     set by 'options(error=)'.  The error is first signaled as if using
     'signalCondition()'.  If there are no handlers or if all handlers
     return, then the error message is printed (if
     'options("show.error.messages")' is true) and the default error
     handler is used.  The default behaviour (the 'NULL' error-handler)
     in interactive use is to return to the top level prompt or the top
     level browser, and in non-interactive use to (effectively) call
     'q("no", status=1, runLast=FALSE').

Actually, whatever is set by options(error=), after doing what is specified by options(error=), if not quit, "to return to the top level prompt or the top level browser" always happens. So, I think, it is more appropriate to say "The default behaviour (the 'NULL' error handler) in interactive use is to do nothing".

--- On Fri, 1/2/13, Suharto Anggono Suharto Anggono <suharto_anggono at yahoo.com> wrote:

> From: Suharto Anggono Suharto Anggono <suharto_anggono at yahoo.com>
> Subject: Was confused with options(error = expression(NULL)) in example(stop)
> To: R-help at lists.R-project.org
> Date: Friday, 1 February, 2013, 3:40 PM
> In example for function 'stop' in R,
> there is
> options(error = expression(NULL))
> with comment
> # don't stop on stop(.)  << Use with CARE!
> >>
> I was interested, wanted to know how "don't stop on stop(.)"
> was. So, I tried it.
> Typing
> example(stop)
> at the R prompt and pressing ENTER give this.
> > example(stop)
> stop> options(error = expression(NULL))
> stop> # don't stop on stop(.)  << Use with
> CARE! >>
> stop>
> stop> iter <- 12
> stop> if(iter > 10) stop("too many iterations")
> Error in eval(expr, envir, enclos) : too many iterations
> R still stops on stop(.).
> That confused me. Then, I tried manually. But I still
> couldn't get
> options(error = expression(NULL))
> worked. Everything seemed to be as usual. I gave up.
> Some time later, I saw somewhere in the internet that
> options(error = expression(NULL))
> worked in batch mode. Ah, running from R CMD BATCH. It had
> no effect in interactive session. I hadn't thought of it
> before.
> > sessionInfo()
> R version 2.15.2 (2012-10-26)
> Platform: i386-w64-mingw32/i386 (32-bit)
> locale:
> [1] LC_COLLATE=English_United States.1252
> [2] LC_CTYPE=English_United States.1252
> [3] LC_MONETARY=English_United States.1252
> [5] LC_TIME=English_United States.1252
> attached base packages:
> [1] stats     graphics  grDevices
> utils     datasets 
> methods   base
> loaded via a namespace (and not attached):
> [1] tools_2.15.2

More information about the R-help mailing list