print()
and cat()
get_help()
docs
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.
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")
# Print the variable with print()
<- "tardigrades"
favorite_animal print(favorite_animal)
## [1] "tardigrades"
# Print the variable with cat()
<- "tardigrades"
favorite_animal cat(favorite_animal)
## tardigrades
# Print the string _and_ variable with cat(), separated by a space
<- "tardigrades"
favorite_animal 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
<- "tardigrades"
first_favorite_animal <- "axolotls"
second_favorite_animal cat(first_favorite_animal, second_favorite_animal, sep = "\n")
## tardigrades
## axolotls