stopifnot() &
Assertions in R
Learn how to write defensive code that catches bugs early using stopifnot() and assert-style condition checks in R.
What Is an Assertion?
An assertion is a statement in your code that declares something must be TRUE at a specific point. If the condition is false, the program immediately stops and tells you what went wrong.
Why Assertions Matter
Bugs caused by bad inputs are some of the hardest to find. Without assertions, a function might silently return a wrong answer. Assertions catch problems at the source rather than letting errors propagate through your program.
Introducing stopifnot()
stopifnot() is R's built-in assertion function. You pass it one or more logical conditions. If any condition evaluates to FALSE, R throws an error and stops execution.
The Assertion Rule
Named vs. Unnamed Conditions
Since R 4.0, you can give conditions names to produce clearer error messages. Compare these two styles:
stopifnot() vs. if/stop()
R offers two main ways to halt on bad conditions. Here is how they compare:
| stopifnot() | if (...) stop() | |
|---|---|---|
| Lines of code | 1 line | 2-3 lines |
| Custom message | Named args (R 4.0+) | Full control |
| Multiple checks | Comma-separated | Separate blocks |
| Vector handling | All elements must be TRUE | Must use all() manually |
| Best for | Quick guard clauses | Complex conditions |
How to Apply Assertions
Example 1: Basic Guard
A function that computes the square root should only accept non-negative numbers:
Example 2: Vector Assertions
When the condition produces a vector of TRUE/FALSE values, stopifnot() requires every element to be TRUE:
Example 3: Data Frame Validation
Use assertions to validate a data frame before running an analysis:
Where to Place Assertions
What Can You Check?
is.character(s)
is.logical(flag)
length(v) > 0
nrow(df) >= 10
\( x \geq 0 \land x \leq 1 \)
is.finite(x)
ncol(m) == 3
is.data.frame(df)
Using if/stop() for Complex Messages
When you need to include variable values in the error message, if (...) stop() gives you more control:
Common Mistakes
Summary Reference
| Feature | stopifnot() | if/stop() | warning() |
|---|---|---|---|
| Halts execution | Yes | Yes | No |
| Custom message | Named args | Full control | Full control |
| Multiple conditions | Built-in | Manual | Manual |
| Vector-aware | Yes | Need all() | Need all() |
| Use case | Guard clauses | Complex errors | Non-fatal alerts |
Question 1 of 5
What happens when all conditions inside stopifnot() evaluate to TRUE?
Question 2 of 5
Which of the following is the correct syntax for a named assertion in stopifnot()?
Question 3 of 5
Given \( x \leftarrow c(3, -1, 5) \), what does stopifnot(\( x > 0 \)) do?
Question 4 of 5
Where is the best place to put stopifnot() inside a function?
Question 5 of 5
Consider the following function:
compute_bmi \( \leftarrow \) function(weight_kg, height_m) {
stopifnot(
"weight must be positive" = \( \text{weight_kg} > 0 \),
"height must be positive" = \( \text{height_m} > 0 \)
)
\( \text{weight_kg} / \text{height_m}^2 \)
}
A student calls: compute_bmi(70, "1.75")
What error will the student see?