停止R脚本而不获取;wrapup过程中出错“;消息

停止R脚本而不获取;wrapup过程中出错“;消息,r,error-handling,R,Error Handling,我编写了一个R脚本,它将消息(进度报告)写入文本文件。我修改了error选项,以便在发生错误时,错误消息也会写入该文件: options(error = function() { cat(geterrmessage(),file = normalizePath("logs/messages.txt"),append = TRUE) stop() }) 它可以工作,但当发生错误时,我会在控制台/终端窗口中收到以下消息: Error during wrapup: Execution halte

我编写了一个R脚本,它将消息(进度报告)写入文本文件。我修改了
error
选项,以便在发生错误时,错误消息也会写入该文件:

options(error = function() {
 cat(geterrmessage(),file = normalizePath("logs/messages.txt"),append = TRUE)
 stop()
})
它可以工作,但当发生错误时,我会在控制台/终端窗口中收到以下消息:

Error during wrapup:
Execution halted

所以我想有更好的方法来中断脚本的执行。。。还是有?

我刚刚在R源代码中找到了这个:

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()
导致执行错误处理程序。如果
stop()
调用发生在错误处理程序中,R将在wrapup:期间显示
错误消息,并阻止您执行否则可能发生的无限递归

不要从
options$error
内部调用
stop()


使用
q(save=“no”,status=1,runLast=FALSE)
,这应该与默认错误处理程序在非交互式使用时所做的完全相同。有关错误处理的详细信息,请参见
?options
,了解
options$error
stop
的含义。

我怀疑您必须从中删除
stop()
。然后发生的情况是,如果我使用Rscript.exe myscript.R运行脚本,会报告错误,但脚本会继续运行而不停止。请尝试使用q()而不是停止()。使用stop()函数调用错误处理程序,因此从stop中停止。。。听起来像是递归。这可能就是问题所在。