抑制 R 中的错​​误消息 [英] Suppress error message in R

查看:18
本文介绍了抑制 R 中的错​​误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 R 中运行模拟研究.有时,我的模拟研究会产生错误消息.当我在一个函数中实现我的模拟研究时,当出现此错误消息时,模拟停止.我知道抑制错误是不好的做法,但此时对我来说除了抑制错误然后继续下一个模拟直到我喜欢运行的模拟总数之外别无选择.为此,我必须抑制 R 产生的错误消息.

为此,我尝试了不同的方法:

库(base64)抑制警告抑制消息选项(错误 = 表达式(NULL))

在前两个选项中,只有警告和消息被抑制,所以没有帮助.如果我理解正确,在最后一种情况下,应该避免所有错误消息.但是,这无济于事,该功能仍会停止并显示错误消息.

有人知道为什么这不像我期望的那样工作吗?我在互联网上搜索了解决方案,但只能找到上述方法.在我运行模拟的函数中,部分代码由外部程序 JAGS(Gibbs 采样器)分析,并由此分析产生错误信息.这可能是哪里出错了?

请注意,我不必抑制某个/特定的错误消息,因为没有产生其他错误消息,拥有一个仅抑制所有错误消息的选项就足够好"了.

感谢您的时间和帮助!

解决方案

抑制消息和抑制对错误的响应之间有很大的不同.如果一个函数不能完成它的任务,它必然会返回一个错误(尽管有些函数有一个命令行参数,以便在发生错误时采取其他一些行动).正如 Zoonekynd 建议的那样,您需要的是使用 trytrycatch 来封装"错误,以便您的主程序流即使在函数失败时也能继续.>

I am running a simulation study in R. Occassionally, my simulation study produces an error message. As I implemented my simulation study in a function, the simulation stops when this error message occurs. I know that it is bad practice to suppress errors, but at this moment to me there is no other option than to suppress the error and then go on with the next simulation until the total number of simulations I like to run. To do this, I have to suppress the error message R produces.

To do this, I tried different things:

library(base64)
suppressWarnings
suppressMessages
options(error = expression(NULL))

In the first two options, only warnings and message are suprressed, so that's no help. If I understand it correctly, in the last case, all error messages should be avoided. However, that does not help, the function still stops with an error message.

Has someone any idea why this does not work the way I expect it to work? I searched the internet for solutions, but could only find the above mentioned ways. In the function I am running my simulation, a part of the code is analysed by the external program JAGS (Gibbs sampler) and the error message is produced by this analysis. Might this be where it goes wrong?

Note that I do not have to supress a certain/specific error message, as there are no other error messages produced, it is 'good enough' to have an option that supresses just all error messages.

Thanks for your time and help!

解决方案

There is a big difference between suppressing a message and suppressing the response to an error. If a function cannot complete its task, it will of necessity return an error (although some functions have a command-line argument to take some other action in case of error). What you need, as Zoonekynd suggested, is to use try or trycatch to "encapsulate" the error so that your main program flow can continue even when the function fails.

这篇关于抑制 R 中的错​​误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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