R has several built-in functions that can be used to print or display information, but print
and cat
are the most basic. As R is an interpreted language, you can try these out directly in the R console:
print("Hello World")
#[1] "Hello World"
cat("Hello World\n")
#Hello World
Note the difference in both input and output for the two functions. (Note: there are no quote-characters in the value of x
created with x <- "Hello World"
. They are added by print
at the output stage.)
cat
takes one or more character vectors as arguments and prints them to the console. If the character vector has a length greater than 1, arguments are separated by a space (by default):
cat(c("hello", "world", "\n"))
#hello world
Without the new-line character (\n
) the output would be:
cat("Hello World")
#Hello World>
The prompt for the next command appears immediately after the output. (Some consoles such as RStudio's may automatically append a newline to strings that do not end with a newline.)
print
is an example of a "generic" function, which means the class of the first argument passed is detected and a class-specific method is used to output. For a character vector like "Hello World"
, the result is similar to the output of cat
. However, the character string is quoted and a number [1]
is output to indicate the first element of a character vector (In this case, the first and only element):
print("Hello World")
#[1] "Hello World"
This default print method is also what we see when we simply ask R to print a variable. Note how the output of typing s
is the same as calling print(s)
or print("Hello World")
:
s <- "Hello World"
s
#[1] "Hello World"
Or even without assigning it to anything:
"Hello World"
#[1] "Hello World"
If we add another character string as a second element of the vector (using the c()
function to concatenate the elements together), then the behavior of print()
looks quite a bit different from that of cat
:
print(c("Hello World", "Here I am."))
#[1] "Hello World" "Here I am."
Observe that the c()
function does not do string-concatenation. (One needs to use paste
for that purpose.) R shows that the character vector has two elements by quoting them separately. If we have a vector long enough to span multiple lines, R will print the index of the element starting each line, just as it prints [1]
at the start of the first line.
c("Hello World", "Here I am!", "This next string is really long.")
#[1] "Hello World" "Here I am!"
#[3] "This next string is really long."
The particular behavior of print
depends on the class of the object passed to the function.
If we call print
an object with a different class, such as "numeric" or "logical", the quotes are omitted from the output to indicate we are dealing with an object that is not character class:
print(1)
#[1] 1
print(TRUE)
#[1] TRUE
Factor objects get printed in the same fashion as character variables which often creates ambiguity when console output is used to display objects in SO question bodies. It is rare to use cat
or print
except in an interactive context. Explicitly calling print()
is particularly rare (unless you wanted to suppress the appearance of the quotes or view an object that is returned as invisible
by a function), as entering foo
at the console is a shortcut for print(foo)
. The interactive console of R is known as a REPL, a "read-eval-print-loop". The cat
function is best saved for special purposes (like writing output to an open file connection). Sometimes it is used inside functions (where calls to print()
are suppressed), however using cat()
inside a function to generate output to the console is bad practice. The preferred method is to message()
or warning()
for intermediate messages; they behave similarly to cat
but can be optionally suppressed by the end user. The final result should simply returned so that the user can assign it to store it if necessary.
message("hello world")
#hello world
suppressMessages(message("hello world"))