01 / 21
! ! i

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.

R Programming

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.

Errors
Code stops running. Something is fundamentally broken.
Warnings
Code finishes, but R suspects something may be wrong.
Messages
Informational only. Code ran perfectly fine.

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

Error Execution stops Warning Execution continues Message Informational only HIGH MEDIUM LOW

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.

> log("hello") Error in log("hello") : non-numeric argument to mathematical function

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.

> log(-1) [1] NaN Warning message: In log(-1) : NaNs produced

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.

> library(ggplot2) ── Attaching packages ──────────────── tidyverse 2.0.0 ── ✓ ggplot2 3.4.4

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.

stop()
Halts execution and produces an error message.
warning()
Continues execution but flags a warning.
message()
Prints informational text. No impact on execution.
# You can generate each type yourself: stop("This is an error") warning("This is a warning") message("This is a message")

How to Interpret Console Output

1
Read the first word. Does it say "Error", "Warning message", or something else?
2
Identify the function. R usually tells you which function caused the issue (e.g., "In log(-1)").
3
Read the description. The text after the colon explains what went wrong in plain English.
4
Check for a result. If a value was returned, it is a warning. If nothing was returned, it is an error.
5
Decide your response. Fix errors, investigate warnings, and safely ignore most messages.

Example 1: Syntax Error

> x <- c(1, 2, 3 Error: unexpected end of input
# Fix: close the parenthesis > x <- c(1, 2, 3) # No output → assignment worked silently ✓

Example 2: Coercion Warning

> as.numeric(c("1", "two", "3")) [1] 1 NA 3 Warning message: NAs introduced by coercion

Example 3: Package Message

> library(dplyr) Attaching package: 'dplyr' The following objects are masked from 'package:stats': filter, lag The following objects are masked from 'package:base': intersect, setdiff, setequal, union

Tricky Case: Object Not Found

> mean(my_data) Error in mean(my_data) : object 'my_data' 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:

1
Typo in the name: you defined mydata but typed my_data
2
Forgot to run earlier code: the line that creates my_data was never executed
3
Scope issue: the variable was created inside a function and is not accessible outside it

Common Mistakes

✕ Panicking at messages
Loading a package prints messages. These are not errors. Read the first word before reacting.
✕ Ignoring warnings
A warning means R produced a result, but it might be wrong. Always check the output after a warning.
✕ Not reading the full message
Error messages tell you which function and what went wrong. Read the whole thing, not just "Error".

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?