当“warnings()”出现时中断循环出现在R中
我有一个问题: 我正在运行一个循环来处理多个文件。我的矩阵非常巨大,因此如果我不小心,我经常会耗尽内存。
如果创建任何警告,是否有办法打破循环?它只是继续运行循环并报告它在很久以后失败......烦人。聪明的 stackoverflow-ers 有什么想法吗?!
I am having an issue:
I am running a loop to process multiple files. My matrices are enormous and therefore I often run out of memory if I am not careful.
Is there a way to break out of a loop if any warnings are created? It just keeps running the loop and reports that it failed much later... annoying. Any ideas oh wise stackoverflow-ers?!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过以下方式将警告转化为错误:
与警告不同,错误会中断循环。很好的是,R 还会向您报告这些特定错误是从警告转换而来的。
You can turn warnings into errors with:
Unlike warnings, errors will interrupt the loop. Nicely, R will also report to you that these particular errors were converted from warnings.
R 允许您定义一个条件处理程序,
该处理程序会导致
在 tryCatch 之后继续执行;您可以决定通过将警告转换为错误来结束
或优雅地处理条件(在警告调用后继续评估),
打印
R allows you to define a condition handler
which results in
Execution continues after tryCatch; you could decide to end by converting your warning to an error
or handle the condition gracefully (continuing evaluation after the warning call)
which prints
设置全局
warn
选项:请注意,“警告”不是“错误”。循环不会因警告而终止(除非
options(warn=2)
)。Set the global
warn
option:Note that a "warning" is not an "error". Loops don't terminate for warnings (unless
options(warn=2)
).