创建持久的多行字符串
我想将多行字符串分配给 R 中的变量,以便稍后可以调用该变量。 当我尝试 paste("line 1", "line 2", sep = "\n")
时,我得到 "line 1\nline 2"
。 当我尝试 cat("line 1", "line 2", sep = "\n")
时,我得到了所需的输出,但该输出不是持久的 (cat()
返回一个 None
类型的对象)。我尝试使用多行字符串的原因是我需要通过邮件正文中的 SMTP 服务器(以及包 sendmailR
)发送查询结果(而不是作为附件)。
I would like to assign a multi-line string to a variable in R so that I can call the variable later.
When I try paste("line 1", "line 2", sep = "\n")
I get "line 1\nline 2"
.
When I try cat("line 1", "line 2", sep = "\n")
, I get the desired output, but this is output is not persistent (cat()
returns an object of type None
). The reason that I'm trying to use a multi-line string is that I need to send query results via a SMTP server (and the package sendmailR
) in the message body (not as an attachment).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
paste("line 1", "line 2", sep = "\n")
是正确的方法,你会得到你想要的:你的困惑可能来自于
print< /code> 对输出进行转义,因此它按照解析器期望的方式打印字符串:
请注意字符串周围的引号。
cat
按原样打印输出。在这两种情况下,对象是相同的,只是输出格式不同。显然,您可以直接创建字符串而无需粘贴:
paste("line 1", "line 2", sep = "\n")
is the right way, you get what you intended:Your confusion probably comes from the fact that
print
escapes the output, so it is printing the string the way it would be expected by the parser:Note the quotes around the string.
cat
prints the output as-is. In both cases the object is the same, it's only the output format that differs.Obviously, you could create the string directly without
paste
: