Stop for-Loop when Warnings Appear in R (Example)
In this article, I’ll explain how to break a for-loop whenever a warning message occurs in R.
Table of contents:
- Setting Up an Example for-Loop
- Example: Breaking for-Loop after Warning Message
- Video, Further Resources & Summary
So without further additions, let’s dive right into the examples:
Setting Up an Example for-Loop
Let’s assume that we want to run a for-loop in R, which looks as follows:
for(i in 1:3) { # Start for-loop pmax(1:3, 1:4) # Apply function with warning } # Warning: # 1: In pmax(1:3, 1:4) : an argument is partially recycled # 2: In pmax(1:3, 1:4) : an argument is partially recycled # 3: In pmax(1:3, 1:4) : an argument is partially recycled |
for(i in 1:3) { # Start for-loop pmax(1:3, 1:4) # Apply function with warning } # Warning: # 1: In pmax(1:3, 1:4) : an argument is partially recycled # 2: In pmax(1:3, 1:4) : an argument is partially recycled # 3: In pmax(1:3, 1:4) : an argument is partially recycled
Within our example loop, we applied the pmax function to vectors with different length, leading to a warning message in each run of the loop.
Now, let’s assume that we want to stop our loop, in case such a warning message appears…
Example: Breaking for-Loop after Warning Message
If we want to stop a for-loop after the appearance of a warning message, we can simply change the global options of the R programming language so that warnings are treated as errors. The reason why this works is that R automatically stops for-loops after the occurrence of errors.
Let’s do this in practice! With the following R code, we change the global options for warnings:
options(warn = 2) # Modify global options |
options(warn = 2) # Modify global options
Now, let’s apply exactly the same R syntax as before:
for(i in 1:3) { # Start for-loop pmax(1:3, 1:4) # Apply function with warning } # Error in pmax(1:3, 1:4) : # (converted from warning) an argument is partially recycled |
for(i in 1:3) { # Start for-loop pmax(1:3, 1:4) # Apply function with warning } # Error in pmax(1:3, 1:4) : # (converted from warning) an argument is partially recycled
As you can see based on the output of the RStudio console, our for-loop was finished after the first warning message. Looks good!
Note that you would have to change the global options back to the default, in case you want to continue with default specifications after running your for-loop:
options(warn = 0) # Set global options to default |
options(warn = 0) # Set global options to default
Also note that the previous R code could also be applied to other types of loops, such as while-loops.
Video, Further Resources & Summary
I have recently released a video on my YouTube channel, which explains the examples of this article. You can find the video below:
The YouTube video will be added soon.
In addition, you may want to read the other tutorials of this website:
- break & next Functions in for-Loops
- message, warning & stop Functions in R
- for-Loop in R
- Loops in R
- The R Programming Language
This article explained how to cancel for- and while-loops in the R programming language after warnings() appear. In case you have further questions, don’t hesitate to let me know in the comments.