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.



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


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


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

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.