print() and cat()
   get_help() docs


Description

The print() and cat() functions come with R and are part of the Base R {base} package.

We use these functions to explicitly print to the Console. The print() function can only print one thing at a time, but the cat() function can print multiple things together. The output of the printing looks slightly different for more advanced reasons, but both accomplish the goal of printing.

Using printing functions is not strictly necessary when working from the R Console directly, or from RMarkdown chunks. However, explicit printing is crucial when running non-interactive scripts, in which simply typing the variable name will not work to reveal output.

Conceptual Usage

print(thing to print)

cat(thing to print)

cat(first thing to print, 
    second thing to print, 
    (etc. to print more things),
    sep = "how to separate the things you're printing")

Examples


# Print the variable with print()
favorite_animal <- "tardigrades"
print(favorite_animal)
## [1] "tardigrades"


# Print the variable with cat()
favorite_animal <- "tardigrades"
cat(favorite_animal)
## tardigrades


# Print the string _and_ variable with cat(), separated by a space
favorite_animal <- "tardigrades"
cat("My favorite animals are", favorite_animal, sep = " ")
## My favorite animals are tardigrades


# Print the two variables separated by a new line. Use "\n" (backslash and 'n') to represent a "newline" symbol
first_favorite_animal <- "tardigrades"
second_favorite_animal <- "axolotls"
cat(first_favorite_animal, second_favorite_animal, sep = "\n")
## tardigrades
## axolotls