Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stopping an R script without getting "Error during wrapup" message

I wrote an R script which writes messages (progress report) to a text file. I modified the error option so that when an error occurs, the error message is also written to that file:

options(error = function() {
 cat(geterrmessage(),file = normalizePath("logs/messages.txt"),append = TRUE)
 stop()
})

It works, but I get this message in the console/terminal window when an error does occur:

Error during wrapup:
Execution halted

So I'm thinking there's a better way to interrupt the execution of the script... or is there?

like image 941
Dominic Comtois Avatar asked Sep 11 '14 03:09

Dominic Comtois


People also ask

How do you exit a script in R?

Both quit() and stop(message) will quit your script. If you are sourcing your script from the R command prompt, then quit() will exit from R as well.

How do I stop an execution of a program in R?

The shortcut to interrupt a running process in R depends on the R software and the operating system you are using. However, if you are using RStudio on a Windows computer, you can usually use Esc to stop a currently executing R script. Then, we can press Esc to interrupt the loop.


1 Answers

I just found this inside R source code:

if (inError) {
    /* fail-safe handler for recursive errors */
    if(inError == 3) {
         /* Can REprintf generate an error? If so we should guard for it */
        REprintf(_("Error during wrapup: "));
        /* this does NOT try to print the call since that could
           cause a cascade of error calls */
        Rvsnprintf(errbuf, sizeof(errbuf), format, ap);
        REprintf("%s\n", errbuf);
    }

stop() causes the error handler to be executed. If the stop() call occurs within the error handler, R displays the Error during wrapup: message and prevents you from the infinite recursion that would occur otherwise.

Do not call stop() from inside your options$error.

Use q(save="no", status=1, runLast=FALSE) instead, that should do exactly what the default error handler does for non-interactive use. See ?options for the meaning of options$error and ?stop for details about error handling.

like image 92
Roman Zenka Avatar answered Nov 14 '22 23:11

Roman Zenka