这是一个很好的问题,因为 R 以许多稍微不同的方式来完成类似任务而臭名昭著。
函数print 用于简单地打印到R 控制台。例如:
# a sample vector
tmp<-c("foo","bar","baz")
tmp
# [1] "foo" "bar" "baz"
print(tmp)
#[1] "foo" "bar" "baz"
另外值得注意的是print可以在加载其他包后获得新的打印方式,例如xtable包。
cat 函数将传递给它的所有条目连接起来,并以更类似于您可能更习惯的命令行或 shell(例如 bash)的方式打印它们。例如:
cat(tmp)
# foo bar baz
像paste 函数一样,它也可以包含分隔符,并允许 R 提取其他技巧:
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}
正如暗示的那样,如果您在环境中使用 R,例如在带有 knitr 的 LaTeX 文档中并想要打印特殊格式的文本,这可能会很方便。
write 函数是 cat 的包装器,它打印到文件并对矩阵进行了一些特殊处理(此处未显示):
write(tmp,file = "output.txt")
file.show("output.txt")
# foo
# bar
# baz
虽然 OP 没有提及它,但我认为提及 paste 函数很重要,包括它的表亲 paste0。 paste 函数还将项目打印到控制台,这些项目在视觉上类似于print,例如:
paste(tmp)
# [1] "foo" "bar" "baz"
但是,存在一些显着差异。您还可以组合来自不同向量的元素,并指定分隔符:
paste(tmp,c("one","two"),sep = "~")
# [1] "foo~one" "bar~two" "baz~one"
# Note how the second element is recycled here!
您还可以collapse 输出将所有内容与另一个指定的分隔符连接在一起:
paste(tmp,c("one","two"),sep = "~",collapse = "|")
# [1] "foo~one|bar~two|baz~one"
如果你只是想要一个快速简单的,你可以使用paste0,它不使用分隔符,但也可以支持collapse。
paste0(tmp,c("one","two"))
# [1] "fooone" "bartwo" "bazone"
重要的是,paste 和 paste0 的输出可以保存为对象或在另一个函数中使用,这与 Richard 的评论中的 cat 不同。
x<-paste(tmp,c("one","two"),sep = "~")
x
# [1] "foo~one" "bar~two" "baz~one"
如果您尝试将print 或cat 的输出保存到一个对象中,这是控制台输出:
> x<-print("yes")
[1] "yes"
> x
[1] "yes"
> x<-cat("yes")
yes
> x
NULL
大多数 R 初学者通常会遇到的另一个症结是在 for 循环中使用 print 和 paste。在for 循环内执行时,paste 的控制台输出不显示,如下所示:
for(i in 1){
paste("This is paste")
print("This is print")
cat("This is cat")
}
# [1] "This is print"
# This is cat
sink 函数只是将 R 控制台输出转移到文件中。
sink("output.txt")
cat("hello")
cat("\n")
cat("world")
print(tmp)
sink()
# hello
# world[1] "foo" "bar" "baz"
请注意,在两个 sink() 命令之间写入控制台的任何内容都将写入文件,这可能是不可取的。
writeLines 函数可以与“连接”类型的对象一起使用,以便将输出发送到其他地方,通常是文件。与sink 不同,它只会将指定的项目写入文件。
fileConn<-file("output.txt")
writeLines(tmp,fileConn)
cat("This is test text")
print("Hello world")
close(fileConn)
file.show("output.txt")
# foo
# bar
# baz
您可以为此提出一些创造性的用途,例如在脚本末尾创建系统会话日志
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!>
有一些很好的解释解释了为什么其中一些函数也能以它们在此博客中的方式工作:http://arrgh.tim-smith.us/atomic.html