我正在try 用R打印多行消息.例如,

print("File not supplied.\nUsage: ./program F=filename",quote=0)

我得到了输出

File not supplied.\nUsage: ./program F=filename

而不是想要的

File not supplied.
Usage: ./program F=filename

推荐答案

cat()的替代方案是writeLines():

> writeLines("File not supplied.\nUsage: ./program F=filename")
File not supplied.
Usage: ./program F=filename
>

一个优点是,您不必记住在传递到cat()的字符串后面附加"\n",就可以在消息后面获得换行符.例如,将上述结果与相同的cat()输出进行比较:

> cat("File not supplied.\nUsage: ./program F=filename")
File not supplied.
Usage: ./program F=filename>

> cat("File not supplied.\nUsage: ./program F=filename","\n")
File not supplied.
Usage: ./program F=filename
>

The reason print() doesn't do what you want is that print() shows you a version of the object from the R level - in this case it is a character string. You need to use other functions like cat()writeLines() to display the string. I say "a version" because precision may be reduced in printed numerics, 和 the printed object may be augmented with extra information, for example.

R相关问答推荐

R中的Fasttext langue_identification返回太多参数-如何与文本匹配?

生成具有受控相关性的x和y

创建计数(带重置)变量

R图中的字体大小和字体样式(带有R底图)

Select 与特定列中最大值对应的数据帧行

如何在弹性表中为类别值的背景上色

高质量地将R格式的图表从Word中输出

编辑文件后编辑RhandsonTable

pickerInput用于显示一条或多条geom_hline,这些线在图中具有不同 colored颜色

如何从当前行上方找到符合特定条件的最接近值?

计算具有奇数日期的运行金额

计算时间段的ECDF(R)

如何从R ggplot图片中获取SVG字符串?

为了网络分析目的,将数据框转换为长格式列联表

使用带有OR条件的grepl过滤字符串

为左表中的所有行使用值Fill滚动左连接

从多面条形图中删除可变部分

当由base::限定时,`[.factor`引发NextMethod错误

注释不会绘制在所有ggplot2面上

在REST API中使用参数R