Suppress Warnings Globally in R (Example)

 

In this R tutorial you’ll learn how to disable warning messages in the global options.

Table of contents:

Let’s dive right into the examples…

 

Example: Disable Warning Messages in R

I’ll illustrate the suppression of warning messages in R based on the pmax function. Consider the following R code:

pmax(1:3, 1:5)                     # pmax function returns warning
# [1] 1 2 3 4 5
# Warning message:
# In pmax(1:3, 1:5) : an argument will be fractionally recycled

As you can see, the pmax function returns a warning message to the RStudio console.

We can suppress this warning message by running the following R syntax:

options(warn = - 1)                # Disable warning messages globally

The previous R code specifies globally that warnings are never returned to the RStudio console.

Let’s illustrate that based on the pmax function. If we run exactly the same R code as before, no warnings are returned:

pmax(1:3, 1:5)                     # pmax function without warnings
# [1] 1 2 3 4 5

 

Set Global Options to Default

In case you want to turn on warning messages again, you can set the global R options for warnings back to default as shown below:

options(warn = 0)                  # Set warning messages to default

Let’s run our exemplifying pmax function once more:

pmax(1:3, 1:5)                     # pmax function returns warning again
# [1] 1 2 3 4 5
# Warning message:
# In pmax(1:3, 1:5) : an argument will be fractionally recycled

The warning message is back!

 

Video & Further Resources

Do you need further explanations on the R programming syntax of this article? Then you may watch the following video tutorial of my YouTube channel. I’m explaining the examples of this article in the video.

 

The YouTube video will be added soon.

 

In addition, I can recommend to read some of the related tutorials of my website.

 

Summary: In this article, I showed how to ignore warnings in the R programming language. Let me know in the comments section, in case you have any additional questions.

 



Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top