Errors, Warnings
& Messages in R
Learn to read, interpret, and respond to the three types of console output R gives you: when code breaks, when something looks suspicious, and when R just wants to keep you informed.
What Is Console Output?
Every time you run R code, the console may print text back at you. Not all of this text means the same thing. R uses three distinct categories of console output, each with a different purpose and severity level.
Why Reading Output Matters
Many beginners panic at any red text in the console. But not all red text means your code failed. Understanding the difference between errors, warnings, and messages saves you from unnecessary debugging and helps you catch real problems faster.
The Severity Hierarchy
Understanding Errors
An error is R's way of saying: "I cannot do what you asked." The code halts immediately, and no result is returned. Errors always begin with the word Error in the console.
Understanding Warnings
A warning means R completed your request but encountered something unusual. Warnings begin with Warning message: and the result is still returned, but may not be what you expected.
Understanding Messages
Messages are purely informational. They tell you about package loading, default settings, or progress updates. They do not indicate any problem with your code.
Key Distinction: Error vs. Warning
| Error | Warning | |
|---|---|---|
| Starts with | Error in... | Warning message: |
| Code execution | Stops immediately | Runs to completion |
| Result returned? | No | Yes, but check it |
| R function | stop() | warning() |
| Action required | Must fix code | Investigate result |
R Functions Behind the Scenes
R provides three functions that package authors and you can use to generate each type of output. Understanding these helps you read other people's code and write your own.
How to Interpret Console Output
Example 1: Syntax Error
Example 2: Coercion Warning
Example 3: Package Message
Tricky Case: Object Not Found
This is one of the most common errors in R. It means the variable my_data does not exist in your current environment. Common causes:
Common Mistakes
Summary Comparison
| Feature | Error | Warning | Message |
|---|---|---|---|
| Keyword | Error | Warning | (varies) |
| Execution | Halts | Continues | Continues |
| Result? | None | Possibly incorrect | Correct |
| R function | stop() | warning() | message() |
| Your response | Fix the code | Investigate | Read and move on |
| Suppress with | tryCatch() | suppressWarnings() | suppressMessages() |
Question 1 of 5
You run log(-5) in R and get a result of NaN along with text that starts with "Warning message:". What type of output is this?
Question 2 of 5
You try to run mean(my_vector) and R says: "Error in mean(my_vector) : object 'my_vector' not found". What should you do first?
Question 3 of 5
When you run library(dplyr), R prints several lines about "masked" objects. What type of output is this?
Question 4 of 5
Which R function would a package developer use to halt execution and display an error when invalid input is detected?
Question 5 of 5
You run as.numeric(c("10", "abc", "30")) and get [1] 10 NA 30 plus a warning. What does the NA in the result tell you?