Stop R script without receiving error message "Error while wrapping"

I wrote an R script that writes messages (progress report) to a text file. I changed the error parameter so that when an error occurs, the error message is also written to this 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 occurs:

 Error during wrapup: Execution halted 

So, I think the best way to abort script execution ... or is there?

+5
source share
1 answer

I just found this internal 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 execute. If the stop() call is encountered inside an error handler, R displays an Error during wrapup: message and prevents you from infinite recursion that occurred otherwise.

Do not call stop() from within options$error .

Use q(save="no", status=1, runLast=FALSE) instead to do what the default error handler does for non-interactive use. For more information about error handling, see ?options for the options$error and ?stop values.

+5
source

Source: https://habr.com/ru/post/1202299/


All Articles