Written and maintained by CASRAI Editorial Board
Published · Last updated
On this page
The print function in R, print(x), writes the value of x to the console and returns x invisibly, so you can still assign or pipe the result. It is a generic function: the same call formats a number, a character vector, a factor, a table or a data frame with the method for that class, and its arguments (digits, quote, na.print, right, max, width) control how the output looks. This guide covers the syntax, a worked example with research output, how to read what R prints, and when cat(), message() or format() is the better tool.
What the print function in R is used for in research
In analysis scripts, print() is how you make a result visible when R would not show it automatically: inside a for loop, inside a function body, or in a script run non-interactively. R’s language definition says the value of a loop statement is always NULL and is returned invisibly, so a loop shows nothing on its own; calling print() on the object inside the loop is how each iteration’s result reaches the console.
Researchers use print() for three recurring jobs:
- Checking intermediate objects while a pipeline runs (a cleaned data frame, a fitted model summary, a vector of p-values).
- Controlling displayed precision with the digits argument, so a table of estimates shows a consistent number of significant digits.
- Writing a readable log of a long script, where each step announces what it produced.
The function does not change the stored value. The documentation states that print “prints its argument and returns it invisibly (via invisible(x))”, which is why y = print(x) prints once and stores the same object in y. If you are writing your own functions, the guide to defining a function in R and the overview of functions in R programming cover where a function’s return value goes and why a bare value inside it is not shown.
How to install or access R to use print
print() is part of the base package, so there is nothing to install beyond R itself, and no library() call is needed. R is distributed through CRAN, the Comprehensive R Archive Network at cran.r-project.org. On the CRAN Windows base page the download link reads “Download R-4.6.2 for Windows”; that build requires UCRT, which is included in Windows 10, Windows Server 2016 and later.
Once R is installed, open the R console (or run Rscript yourfile.R from a terminal) and type the examples below. Any editor that runs R sends the same commands to the same print() function.
How to use the print function in R step by step
Step 1: print a single value or vector
- Create an object:
x = c(12.3456, 9.8765, 10.5) - Print it:
print(x). R shows the elements with a bracketed index, for example[1] 12.3456 9.8765 10.5000. The elements share one format, so shorter numbers are padded with zeros to the same number of decimals. - Check the return value:
y = print(x)prints once, and y now holds the same vector.
Step 2: control the digits shown
- Call
print(pi, digits = 3). R prints[1] 3.14. - For a vector, remember what the documentation says: digits is the minimum number of significant digits, and at least one entry of the vector shows that many.
print(c(12.3456, 9.8765), digits = 3)therefore prints[1] 12.35 9.88: the smaller element needs two decimals to show three significant digits, and the common format gives the other element two decimals too. - Only whole numbers from 1 to 22 are accepted for digits. If you leave it as NULL, R uses
getOption("digits").
Step 3: print text without quotation marks
print("p = 0.049")shows the string inside quotes, because quote = TRUE is the default.print("p = 0.049", quote = FALSE)removes the quotes. The related functionnoquote()does the same by class, and the documentation’s own example isnoquote(format(x, scientific = FALSE)).
Step 4: print inside a loop or function
- Write
for (i in 1:3) print(i * 2). Each pass prints a line; a barei * 2in the loop body would not. - In a function, end with the object you want to return and call print() on anything you want displayed along the way, as the functions guide describes.
- To return a value without it echoing at the console, wrap it in
invisible(). The R documentation’s example is a functionf2 = function(x) invisible(x)whose call prints nothing, whilef1 = function(x) xprints its result.
Step 5: a worked research example
This is an illustrative composite, not data from a real study. Suppose a script has computed group means and a p-value:
results = data.frame(group = c("A", "B"), mean = c(12.3456, 9.8765))
p_value = 0.0123456
print(results, digits = 3)
print(p_value, digits = 2)
cat("Group difference, p =", format(p_value, digits = 2), "\n")
The first call prints the data frame with the mean column formatted so that each value shows at least three significant digits (12.35 and 9.88). The second prints [1] 0.012. The third uses cat(), covered below, to build a log line, with format() fixing the p-value to two significant digits. A single call would not do all three jobs: print() formats objects, while cat() and format() assemble a text line.
How to read the output of print in R
The bracketed index
The [1] at the start of a printed vector is the position of the first element on that line, not part of your data. Long vectors wrap across lines, each line starting with the index of its first element. Line width follows the width argument, which defaults to getOption("width").
Quotes and NA
Strings print with surrounding quotes unless quote = FALSE. A missing value prints as NA; a character NA prints as NA wrapped in angle brackets only when quote = FALSE. The na.print argument replaces how NA is shown, and the documentation notes it has no interaction with quote, so print(c(1.5, NA), na.print = "-") shows a dash in place of NA.
Spacing and alignment
print.gap sets the spacing between columns of a matrix or data frame (a non-negative integer up to 1024; NULL means 1). Strings are left-aligned by default; right = TRUE right-aligns them.
Truncation of long output
The max argument sets the approximate maximum number of entries to print. NULL means getOption("max.print"), so a very large object may stop early with a message; raise the limit with print(x, max = 5000) or change the option for the session.
Class-specific methods
Because print() is generic, some classes add their own arguments. The documentation lists the factor method (quote = FALSE, max.levels, width), the table method (digits, quote = FALSE, na.print = “”, zero.print = “0”, right, justify = “none”) and the function method (useSource = TRUE). The documentation’s table example is print(table(t1, t2), zero.print = "."), which shows a dot for empty cells and makes sparse cross-tabulations easier to scan. When results are stored in lists, the guide to lists in R shows how those structures are built.
Troubleshooting the print function in R
Nothing prints from a loop or function
A value that is not auto-printed needs print(). At top level, a bare expression such as x is evaluated and printed; inside a loop or a function body only calls such as print() or cat() write to the console. A function whose last line is invisible(x) also prints nothing when called.
The digits value is rejected or seems ignored
digits only accepts 1 to 22. Because it is a minimum for the whole vector, trailing zeros can still appear or extra decimals can appear on larger values; that is R choosing one shared format, not a rounding error. To round the stored values, use round() and then print.
Factors print as numbers inside cat()
cat() prints a factor as its integer codes. The cat documentation says to convert with as.character() or format() first. print() does not have this problem because it uses the factor method.
Output shows quotes you do not want
Use quote = FALSE in print(), wrap the text in noquote(), or switch to cat(), which outputs strings as they are with no escaping.
Text and numbers will not combine in print()
print() formats one object; extra arguments are options such as digits, not more text, so print("Mean is", 3) does not join the two. Build a single string with paste() or paste0(), or use sprintf for formatted strings, then print that. The paste documentation says arguments are converted with as.character and that vector arguments are recycled; paste() has a default separator of a single space, while paste0() has none, and a collapse string joins the results into one. The guide to collapse in R covers that argument.
Free alternatives to the print function in R
R itself is free, and so is every function below, because all are in the base package. They differ in where output goes, how much conversion they apply and what they return.
| Function | What it does | Where output goes and what it returns |
|---|---|---|
| print() | Formats an object with the method for its class, using the arguments above | Console; returns its argument invisibly |
| cat() | Concatenates and writes atomic vectors with a separator (default a space); strings are output as they are, without escaping | Console by default (file = “”), or a file; with append = TRUE it adds to an existing file; takes atomic vectors, not data frames |
| message() | Pastes its arguments together and sends them as a diagnostic message | Standard error by default; suppressMessages() hides it |
| format() | Returns character representations of the elements in a common format (nsmall, digits, width, big.mark, scientific) | Returns a character vector; prints nothing by itself |
| paste() and paste0() | Joins arguments converted to character, with sep and collapse | Returns a character vector; prints nothing by itself |
A practical rule that follows from this table: use print() to look at an object, cat() or paste() plus print() to produce a line of text, message() for progress or warnings that should not pollute saved output, and format() when the number of decimals or a thousands separator matters. The cat documentation says it “performs much less conversion than print”, so it suits simple strings and numbers but not objects with structure. Data frames and factors should go through print() or be converted first.
The data table below lists the print.default arguments with their defaults and effects, compiled from the R manual, so you can check a setting without opening the help page.
Sources
- The R Core Team: print (R-devel manual, base package), checked 2026-11-17
- The R Core Team: print.default (R-devel manual, base package), checked 2026-11-17
- The R Core Team: cat (R-devel manual, base package), checked 2026-11-17
- The R Core Team: message (R-devel manual, base package), checked 2026-11-17
- The R Core Team: format (R-devel manual, base package), checked 2026-11-17
- The R Core Team: paste (R-devel manual, base package), checked 2026-11-17
- The R Core Team: invisible (R-devel manual, base package), checked 2026-11-17
- The R Core Team: R Language Definition, CRAN, checked 2026-11-17
- CRAN: Download R for Windows, checked 2026-11-17
Data: Arguments of print.default in R and what each controls
Compiled from the R-devel manual page for print.default (base package): each argument of the default print method, its documented default and the effect it has on console output. Checked 2026-11-17. Use it to look up a setting before changing how a result prints.
| Argument | Documented default | Effect on printed output |
|---|---|---|
| digits | NULL (uses getOption("digits")) | Minimum number of significant digits; only 1 to 22 accepted; at least one entry shows that many |
| quote | TRUE | Strings print with surrounding quotes; FALSE removes them |
| na.print | NULL | Replaces how NA is shown; no interaction with quote |
| print.gap | NULL (spacing of 1) | Spacing between columns; non-negative integer up to 1024 |
| right | FALSE | FALSE left-aligns strings; TRUE right-aligns them |
| max | NULL (uses getOption("max.print")) | Approximate maximum number of entries printed |
| width | NULL (uses getOption("width")) | Line width used when wrapping output |
| useSource | TRUE | Printing a function shows its original source when available |
Sources
- The R Core Team: print.default (R-devel manual) (accessed 2026-11-17)
- The R Core Team: print (R-devel manual) (accessed 2026-11-17)
Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.







