Home » R Programming Tutorial » Print Function in R – Cat Function in R

Print Function in R – Cat Function in R


()

Simply enter the variable name or expression at the command prompt, R will print its value. Use the print function for generic printing of any object. Use the cat function for producing custom formatted output. Here is the demonstration.

pi

Try It

Print Function

Another way to print pi using Print function as shown below

print(pi)

Try It

This is useful because you can always view your data: just print it. You needn’t write special printing logic, even for complicated data structures.

The print function has a significant limitation, however: it prints only one object at a time. Trying to print multiple items gives this mind-numbing error message:

Error in print.default(“The zero occurs at”, pi, “radians.”) : 
invalid ‘quote’ argument
Calls: print -> print.default
Execution halted

print("The zero occurs at", pi, "radians.")

Try It

Cat Function

The cat function is an alternative to print that lets you combine multiple items into a continuous output:

cat("The zero occurs at", 2*pi, "radians.", "\n")

Try It

Using cat gives you more control over your output, which makes it especially useful in R scripts. A serious limitation, however, is that it cannot print compound data structures such as matrices and lists.

Error in cat(list(…), file, sep, fill, labels, append) : 
argument 1 (type ‘list’) cannot be handled by ‘cat’
Execution halted

cat(list("a","b","c"))

Try It

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote Count

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Array Interview Questions
Graph Interview Questions
LinkedList Interview Questions
String Interview Questions
Tree Interview Questions