warning
warning(..., call. = TRUE, immediate. = FALSE, noBreaks. = FALSE, domain = NULL) warning() signals a warning condition in R, printing a message without halting execution. It sits between silent success and fatal error — you know something is off, but the computation keeps going. This makes it the right tool for non-fatal problems: unusual values, deprecated patterns, or conditions that warrant attention but don’t justify stopping.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
... | any | — | Zero or more objects coerced to character and pasted together. Can also be a single condition object (see Gotchas). |
call. | logical | TRUE | If TRUE, the call that triggered warning() prints alongside the message. |
immediate. | logical | FALSE | If TRUE, prints the warning immediately even when getOption("warn") <= 0. |
noBreaks. | logical | FALSE | If TRUE, the message avoids line breaks in formatted output. |
domain | character | NULL | For message translation. Pass NULL to skip translation lookup. |
Basic Usage
Give warning() a character message and it prints immediately:
warning("something is not quite right")
# Warning message:
# In warning("something is not quite right") :
# something is not quite right
Multiple arguments paste together without separators, so include spaces explicitly:
x <- c(3.14, NA, 2.71)
warning("Found ", sum(is.na(x)), " missing values in input")
# Warning: Found 1 missing values in input
Controlling Call Display
By default, call. = TRUE and the triggering call prints. In a function context this gets noisy:
check_value <- function(x) {
if (x < 0) warning("negative value received")
x
}
check_value(-5)
# Warning in check_value(-5) : negative value received
Set call. = FALSE to suppress the calling context:
check_value <- function(x) {
if (x < 0) warning("negative value received", call. = FALSE)
x
}
check_value(-5)
# Warning: negative value received
In package code, call. = FALSE is usually cleaner. In interactive exploration, call. = TRUE helps you locate where the warning originated.
The warn Option
R’s warning behavior is controlled globally by getOption("warn"). Understanding the levels helps you debug warning-heavy code:
| Value | Behavior |
|---|---|
-1 | Warnings are ignored completely |
0 (default) | Warnings accumulate in last.warning and print after the top-level function finishes |
1 | Warnings print immediately as they occur |
2 or higher | Warnings are converted to errors and halt execution |
The default (warn = 0) batches warnings — this keeps interactive output clean but can make warnings feel disconnected from their source:
options(warn = 1)
warning("first warning")
# Warning: first warning
options(warn = 2)
warning("this becomes an error")
# Error: (converted from warning) this becomes an error
Reset to default after testing:
options(warn = 0)
Inspecting Stored Warnings
When warn = 0, warnings accumulate in last.warning and print only when control returns to the top level:
options(warn = 0)
warning("first")
warning("second")
warnings()
# Warning messages:
# 1: first
# 2: second
last.warning is a named list of condition objects. You can inspect it directly:
last.warning
# $message
# [1] "first"
#
# $message
# [1] "second"
To clear stored warnings without printing them:
last.warning <- NULL
Suppressing Warnings
Use suppressWarnings() to evaluate an expression with all warnings silenced:
# NaN produced, but we handle it
result <- suppressWarnings(log(-1:1))
result
# [1] NaN 0.000000
suppressWarnings() returns the value of the expression, not the warnings. It’s useful when you know a particular operation produces expected warnings and you want to keep output clean.
Custom Handlers with tryCatch()
For fine-grained control, tryCatch() intercepts warnings with a warning handler. Inside the handler, invokeRestart("muffleWarning") stops the warning from printing:
tryCatch(
{
warning("this gets caught")
42
},
warning = function(w) {
message("Caught warning: ", w$message)
invokeRestart("muffleWarning")
}
)
# Caught warning: this gets caught
# [1] 42
Without invokeRestart("muffleWarning"), the warning handler runs but the warning still prints after.
Gotchas
Condition objects ignore other arguments. When you pass a condition object as the sole argument to warning(), all other parameters are silently ignored:
# Call display is ignored here
warning(simpleWarning("message", quote(sum(x))), call. = FALSE)
Warning truncation. R truncates warning messages to getOption("warning.length") characters (default 1000). Long messages end with [... truncated].
Warnings persist across function calls. With warn = 0, last.warning accumulates across nested calls. If a loop calls code that generates warnings, all of them print when the loop completes. Clear last.warning explicitly if you need a clean slate.
immediate. = TRUE forces immediate printing. Even when warn = 0, immediate. = TRUE causes the warning to print right away. This is useful for debugging inside functions where you want to see a warning without changing global options.
See Also
stop()— signal a fatal error that halts executiontryCatch()— catch and handle warnings, errors, and other conditionswithCallingHandlers()— establish local handlers without muffling- Error handling in R — patterns for managing warnings and errors in practice