当“warnings()”出现时中断循环出现在R中

发布于 2024-12-17 18:51:41 字数 148 浏览 0 评论 0原文

我有一个问题: 我正在运行一个循环来处理多个文件。我的矩阵非常巨大,因此如果我不小心,我经常会耗尽内存。

如果创建任何警告,是否有办法打破循环?它只是继续运行循环并报告它在很久以后失败......烦人。聪明的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

把时间冻结 2024-12-24 18:51:41

您可以通过以下方式将警告转化为错误:

options(warn=2)

与警告不同,错误会中断循环。很好的是,R 还会向您报告这些特定错误是从警告转换而来的。

j <- function() {
    for (i in 1:3) {
        cat(i, "\n")
        as.numeric(c("1", "NA"))
}}

# warn = 0 (default) -- warnings as warnings!
j()
# 1 
# 2 
# 3 
# Warning messages:
# 1: NAs introduced by coercion 
# 2: NAs introduced by coercion 
# 3: NAs introduced by coercion 

# warn = 2 -- warnings as errors
options(warn=2)
j()
# 1 
# Error: (converted from warning) NAs introduced by coercion

You can turn warnings into errors with:

options(warn=2)

Unlike warnings, errors will interrupt the loop. Nicely, R will also report to you that these particular errors were converted from warnings.

j <- function() {
    for (i in 1:3) {
        cat(i, "\n")
        as.numeric(c("1", "NA"))
}}

# warn = 0 (default) -- warnings as warnings!
j()
# 1 
# 2 
# 3 
# Warning messages:
# 1: NAs introduced by coercion 
# 2: NAs introduced by coercion 
# 3: NAs introduced by coercion 

# warn = 2 -- warnings as errors
options(warn=2)
j()
# 1 
# Error: (converted from warning) NAs introduced by coercion
神仙妹妹 2024-12-24 18:51:41

R 允许您定义一个条件处理程序,

x <- tryCatch({
    warning("oops")
}, warning=function(w) {
    ## do something about the warning, maybe return 'NA'
    message("handling warning: ", conditionMessage(w))
    NA
})

该处理程序会导致

handling warning: oops
> x
[1] NA

在 tryCatch 之后继续执行;您可以决定通过将警告转换为错误来结束

x <- tryCatch({
    warning("oops")
}, warning=function(w) {
    stop("converted from warning: ", conditionMessage(w))
})

或优雅地处理条件(在警告调用后继续评估),

withCallingHandlers({
    warning("oops")
    1
}, warning=function(w) {
    message("handled warning: ", conditionMessage(w))
    invokeRestart("muffleWarning")
})

打印

handled warning: oops
[1] 1

R allows you to define a condition handler

x <- tryCatch({
    warning("oops")
}, warning=function(w) {
    ## do something about the warning, maybe return 'NA'
    message("handling warning: ", conditionMessage(w))
    NA
})

which results in

handling warning: oops
> x
[1] NA

Execution continues after tryCatch; you could decide to end by converting your warning to an error

x <- tryCatch({
    warning("oops")
}, warning=function(w) {
    stop("converted from warning: ", conditionMessage(w))
})

or handle the condition gracefully (continuing evaluation after the warning call)

withCallingHandlers({
    warning("oops")
    1
}, warning=function(w) {
    message("handled warning: ", conditionMessage(w))
    invokeRestart("muffleWarning")
})

which prints

handled warning: oops
[1] 1
葬シ愛 2024-12-24 18:51:41

设置全局 warn 选项:

options(warn=1)  # print warnings as they occur
options(warn=2)  # treat warnings as errors

请注意,“警告”不是“错误”。循环不会因警告而终止(除非 options(warn=2))。

Set the global warn option:

options(warn=1)  # print warnings as they occur
options(warn=2)  # treat warnings as errors

Note that a "warning" is not an "error". Loops don't terminate for warnings (unless options(warn=2)).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文