问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/25778838/stopping-an-r-script-without-getting-error-during-wrapup-message