Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Breaking loop when "warnings()" appear in R

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?!

like image 709
mmann1123 Avatar asked Nov 21 '11 20:11

mmann1123


People also ask

How do you break out of a loop in R?

The R Break statement is very useful to exit from any loop such as For, While, and Repeat. While executing these, if R finds the break statement inside them, it will stop executing the code and immediately exit from the loop.

What is warning message in R?

The warning R function generates a warning message. The stop R function generates an error message and stops executing the current R code.

Can you loop in R?

There are three types of loop in R programming: While Loop. Repeat Loop.


3 Answers

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
like image 83
Josh O'Brien Avatar answered Sep 30 '22 15:09

Josh O'Brien


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
like image 39
Martin Morgan Avatar answered Sep 30 '22 16:09

Martin Morgan


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)).

like image 36
Joshua Ulrich Avatar answered Sep 30 '22 17:09

Joshua Ulrich