This is a good question, since R is rather notorious for having many slightly different ways to accomplish similar tasks.
The function print
is used to simply print to the R console. For example:
# a sample vector
tmp<-c("foo","bar","baz")
tmp
# [1] "foo" "bar" "baz"
print(tmp)
#[1] "foo" "bar" "baz"
It is also notable that print
can gain new printing methods after other packages are loaded, such as the xtable
package.
The cat
function concatenates all the entries passed to it and prints them in a way more akin to the command line or shell (e.g. bash) that you might be more used to. For example:
cat(tmp)
# foo bar baz
Like the paste
function, it can also include separators, and allows R to pull of other tricks:
cat(tmp,sep = "|")
# foo|bar|baz
# handy for creating complex regular expressions
# create some LaTeX markup
FigureFilepath<-"/path/to/figure/i.jpg"
cat("\\includegraphics[width=0.9\\linewidth,height=0.9\\textheight,keepaspectratio]{", FigureFilepath, "}\n", sep="")
# \includegraphics[width=0.9\linewidth,height=0.9\textheight,keepaspectratio]{/path/to/figure/i.jpg}
As implied, this can be handy if you are using R in an environment such as within a LaTeX document with knitr
and want to print specially formatted text.
The write
function is a wrapper for cat which prints to a file and has some special treatment of matrices (not shown here):
write(tmp,file = "output.txt")
file.show("output.txt")
# foo
# bar
# baz
While the OP did not mention it, I think it is important to mention the paste
function, including its cousin paste0
. The paste
function also prints items to the console that can visually appear similar to print
, e.g.:
paste(tmp)
# [1] "foo" "bar" "baz"
However, there are some notable differences. You can also combine elements from different vectors, and specify a separator:
paste(tmp,c("one","two"),sep = "~")
# [1] "foo~one" "bar~two" "baz~one"
# Note how the second element is recycled here!
You can also collapse
the output to concatenate everything together with yet another specified separator:
paste(tmp,c("one","two"),sep = "~",collapse = "|")
# [1] "foo~one|bar~two|baz~one"
If you just want a quick and simple one, you can use paste0
which does not use a separator, but can also support collapse
.
paste0(tmp,c("one","two"))
# [1] "fooone" "bartwo" "bazone"
Importantly, the output of paste
and paste0
can be saved as an object or used within another function, unlike cat
as per Richard's comment.
x<-paste(tmp,c("one","two"),sep = "~")
x
# [1] "foo~one" "bar~two" "baz~one"
Here is the console output if you try to save the output of print
or cat
into an object:
> x<-print("yes")
[1] "yes"
> x
[1] "yes"
> x<-cat("yes")
yes
> x
NULL
Another sticking point that most R beginners will usually run into is the usage of print
and paste
within a for loop. The console output of paste
is not shown when executed within a for
loop, as shown here:
for(i in 1){
paste("This is paste")
print("This is print")
cat("This is cat")
}
# [1] "This is print"
# This is cat
The sink
function simply diverts the R console output, usually to a file.
sink("output.txt")
cat("hello")
cat("\n")
cat("world")
print(tmp)
sink()
# hello
# world[1] "foo" "bar" "baz"
Note that anything written to the console between the two sink()
commands will be written to the file, which might not be desirable.
The writeLines
function can be used with a 'connection' type object in order to send output elsewhere, often to a file. Unlike sink
, it will only write the specified items to the file.
fileConn<-file("output.txt")
writeLines(tmp,fileConn)
cat("This is test text")
print("Hello world")
close(fileConn)
file.show("output.txt")
# foo
# bar
# baz
You can come up with some creative uses for this, such as creating system session logs at the end of your scripts
fileConn<-file("syslog.txt")
writeLines(capture.output(system('uname -srv',intern=T),sessionInfo()), fileConn)
close(fileConn)
# file.show("syslog.txt")
# <too long to fit here!>
There are some nice explanations for the reasoning behind why some of these functions work the way they do in this blog as well: http://arrgh.tim-smith.us/atomic.html