停止R脚本而不会收到“包装时出错"消息.信息 [英] Stopping an R script without getting "Error during wrapup" message

查看:65
本文介绍了停止R脚本而不会收到“包装时出错"消息.信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

它可以工作,但是当确实发生错误时,我会在控制台/终端窗口中收到此消息:

 包装过程中的错误:执行停止 

所以我在想有一种更好的方法来中断脚本的执行...还是存在?

解决方案

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

  if(inError){/*递归错误的故障安全处理程序*/if(inError == 3){/* REprintf可以生成错误吗?如果是这样,我们应该为此留心*/REprintf(_(包装时出错:"));/*这不会尝试打印呼叫,因为那样可以导致一系列错误调用*/Rvsnprintf(errbuf,sizeof(errbuf),format,ap);REprintf(%s \ n",errbuf);} 

stop()导致执行错误处理程序.如果在错误处理程序中发生 stop()调用,R会在wrapup:消息中显示 Error,并防止您进行否则会发生的无限递归.

请勿从 options $ error 内部调用 stop().

请改用 q(save ="no",status = 1,runLast = FALSE),它应该完全执行默认错误处理程序用于非交互使用的操作.有关错误处理的详细信息,请参见?options 以了解 options $ error 的含义,以及?stop .

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?

解决方案

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.

这篇关于停止R脚本而不会收到“包装时出错"消息.信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆