TExcelTAD
Home › R › R Print Output

R Print Output

R gives you several ways to show results. Choosing the right one makes output easier to read.

Function What it does
auto-printing a visible value on its own line is printed like print() would
print(x) prints an object in its standard format (with [1], quotes, …)
cat(...) writes plain text, no quotes or indices, no automatic newline
message(...) writes a diagnostic message (to stderr) with a newline
format() / sprintf() build nicely formatted strings

Auto-printing and print()

At the top level of a script, writing a value is enough. Inside loops and functions, however, auto-printing does not happen, so you must call print() explicitly:

r · try it yourself
"Top level is printed"
for (i in 1:3) {
  i * 10          # not printed: inside a loop
}
for (i in 1:3) {
  print(i * 10)   # printed
}
[1] "Top level is printed"
[1] 10
[1] 20
[1] 30

print() also returns its argument (invisibly), so you can use it in the middle of a pipeline without changing the value.

cat() for clean text

cat() concatenates its arguments with a space between them and writes them as plain text. Use sep to change the separator and remember the "\n":

r · try it yourself
city <- "Lisbon"
temp <- 23.5
cat("Weather in", city, "\n")
cat("Temperature:", temp, "degrees\n")
cat("a", "b", "c", sep = "-")
cat("\n")
cat("Scores:", c(8, 9, 10), "\n")
Weather in Lisbon
Temperature: 23.5 degrees
a-b-c
Scores: 8 9 10

When you give cat() a vector, it writes every element separated by sep.

Building strings with paste()

paste() glues values into a string with a space; paste0() uses no separator. They return a value, so you can store the result or pass it on:

r · try it yourself
first <- "Grace"
last <- "Hopper"
full <- paste(first, last)
print(full)
paste0("ID-", 1:3)
cat(paste("Hello", full), "\n")
[1] "Grace Hopper"
[1] "ID-1" "ID-2" "ID-3"
Hello Grace Hopper

Formatting numbers

round() controls decimals, format() pads and adds separators, and sprintf() uses C-style placeholders (%d integer, %f decimal, %s string):

r · try it yourself
pi_value <- 3.14159265
round(pi_value, 2)
format(1234567.891, big.mark = ",")
sprintf("Pi is about %.3f", pi_value)
sprintf("%-8s|%5d", "apples", 42L)
cat(sprintf("Item %d costs $%.2f\n", 1:3, c(2.5, 10, 7.25)), sep = "")
[1] 3.14
[1] "1,234,568"
[1] "Pi is about 3.142"
[1] "apples  |   42"
Item 1 costs $2.50
Item 2 costs $10.00
Item 3 costs $7.25

sprintf() is vectorised: given vectors, it returns one string per element.

Messages

message() is meant for progress notes and diagnostics rather than results. In a script it shows up in the output just like cat(), but it goes to the error stream and can be silenced with suppressMessages():

r · try it yourself
message("Loading data...")
x <- c(5, 3, 8)
message("Loaded ", length(x), " values")
suppressMessages(message("you will not see this"))
sum(x)
Loading data...
Loaded 3 values
[1] 16

Test yourself

Inside a for loop, what do you need to show a value?

  1. Nothing, it auto-prints
  2. print() or cat()
  3. return()
  4. echo()

What does paste0("x", 1:2) return?

  1. "x1 x2"
  2. c("x1", "x2")
  3. "x 1" "x 2"
  4. "x12"

Which sprintf format prints 2.5 as 2.50?

  1. %d
  2. %s
  3. %.2f
  4. %2d