print()
print(x, ...) print() displays R objects in the console or an output connection. It is a generic function with methods for vectors, data frames, lists, matrices, and many other object types.
Syntax
print(x, ...)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
x | object | , | An object to print |
... | arguments | , | Additional arguments passed to methods |
digits | integer | NULL | Number of significant digits |
quote | logical | FALSE | Whether to quote string output |
print.gap | integer | NULL | Spacing for matrix/array output |
Examples
Basic usage
# Print a vector
x <- c(1, 2, 3)
print(x)
# [1] 1 2 3
# Print a data frame
df <- data.frame(name = c("Alice", "Bob"), age = c(25, 30))
print(df)
# name age
# 1 Alice 25
# 2 Bob 30
Controlling output
The print() function accepts several arguments that change how the output appears in the console. The digits argument controls numeric precision, while quote toggles whether strings are displayed with surrounding quotation marks. These options are especially useful when you want to inspect data at a glance without running a full summary:
# Print with more digits
pi_values <- c(pi, pi^2, pi^3)
print(pi_values, digits = 5)
# [1] 3.1416 9.8696 31.0063
# Quote strings in output
words <- c("hello", "world")
print(words, quote = TRUE)
# [1] "hello" "world"
Common patterns
- Use print() inside functions to debug values
- Set digits for consistent numeric precision
- Use cat() for simpler output without row numbers
print() in practice
print() is R’s primary output function for the interactive console. It dispatches on the class of its argument, calling the appropriate print.method(): print.data.frame() for data frames, print.tbl_df() for tibbles, print.lm() for linear models, etc. This S3 dispatch means printing behavior is fully customizable for any class.
In functions, values are not auto-printed, the implicit auto-print only occurs at the top level of the interactive console. Inside a function, x by itself does not print; you must call print(x) explicitly. This is why print() calls appear in function bodies and loops: for(i in 1:3) print(i) prints 1, 2, 3, while for(i in 1:3) i prints nothing.
print() invisibly returns its argument, so it can be used in pipelines for debugging without affecting the pipeline’s output: x |> print() |> next_function() prints the intermediate value and passes it through unchanged.
cat() is an alternative that formats its argument as a string without using S3 dispatch. It is faster for simple string output and gives you direct control over separators and line endings. Use print() when you want R’s standard formatted output for an object; use cat() when building a specific text output format.
print() is generic, it dispatches to print.data.frame(), print.lm(), etc. depending on the class of x. Typing an object name at the console calls print() implicitly. In a script or function, print() must be called explicitly, just writing the variable name does not trigger output. For formatted output with format strings, use cat() or sprintf() instead.
Debugging pipelines with inline print()
# A data transformation chain with debug prints
library(dplyr)
mtcars |>
filter(cyl == 6) |>
print() |> # inspect after filtering
mutate(hp_per_cyl = hp / cyl) |>
print() |> # verify the new column
summarise(
mean_hp = mean(hp),
mean_ratio = mean(hp_per_cyl)
)
# mpg cyl disp hp drat wt qsec vs am gear carb
# Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
# Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
# Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
# Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
# Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
# Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
# Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
# mean_hp mean_ratio
# 1 122.2857 19.71429
Because print() invisibly returns its input unchanged, you can insert it anywhere in a pipe chain without affecting the data flow. Each print() call shows the intermediate state of the data frame at that point in the pipeline, making it easy to spot where a transformation produced unexpected results. For production code, remove these debug print() calls and replace them with formal logging, but during development they are the fastest way to trace the shape of your data through a sequence of dplyr verbs.