R has several built-in functions that can be used to print or display information, but
cat are the most basic. As R is an interpreted language, you can try these out directly in the R console:
print("Hello World") # "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
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.)
"Hello World", the result is similar to the output of
cat. However, the character string is quoted and a number
 is output to indicate the first element of a character vector (In this case, the first and only element):
print("Hello World") # "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
s <- "Hello World" s # "Hello World"
Or even without assigning it to anything:
"Hello World" # "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
print(c("Hello World", "Here I am.")) # "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
 at the start of the first line.
c("Hello World", "Here I am!", "This next string is really long.") # "Hello World" "Here I am!" # "This next string is really long."
The particular behavior of
If we call
print(1) # 1 print(TRUE) # 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
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
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"))