我print用来输出R中的函数,例如:
print("blah blah blah")
Run Code Online (Sandbox Code Playgroud)
这输出
[1] "blah blah blah"
Run Code Online (Sandbox Code Playgroud)
到控制台.我该如何避免[1]和报价?
Ale*_*lds 24
使用cat("Your string")(键入?cat以查看帮助页面)将连接对象输出到标准输出.
Das*_*son 15
message可能是替代print您的需求的最佳功能. cat也是一个很好的功能,但message也会为你打印一个新的系列.使用起来也更好,因为它比抑制输出更容易message抑制输出cat.
如果您只想删除引号但不介意[1]打印,那么您可以使用quote=FALSE选项print.
编辑:如注释中所述,message 与调用不同,print因为它将输出发送到不同的连接.使用cat将按照其他人的注意事项执行您想要的操作,但您可能希望在消息后添加新行.
例
cat("This is a message\n") # You'll want to add \n at the end
Run Code Online (Sandbox Code Playgroud)
如果你想去掉唯一的[1],但保留换行符:
string_vector <- c('some', 'words', 'in', 'a', 'vector')
paste(string_vector, collapse = '\n') %>% cat()
Run Code Online (Sandbox Code Playgroud)
产生:
some
words
in
a
vector
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10426 次 |
| 最近记录: |