创建持久的多行字符串

发布于 2025-01-01 01:39:27 字数 337 浏览 1 评论 0原文

我想将多行字符串分配给 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回心转意 2025-01-08 01:39:27

paste("line 1", "line 2", sep = "\n") 是正确的方法,你会得到你想要的:

> a = paste("line 1", "line 2", sep = "\n")
> cat(a)
line 1
line 2> 

你的困惑可能来自于 print< /code> 对输出进行转义,因此它按照解析器期望的方式打印字符串:

> print(a)
[1] "line 1\nline 2"

请注意字符串周围的引号。 cat 按原样打印输出。在这两种情况下,对象是相同的,只是输出格式不同。

显然,您可以直接创建字符串而无需粘贴:

> a = "line1\nline2"
> cat(a)
line1
line2> 

paste("line 1", "line 2", sep = "\n") is the right way, you get what you intended:

> a = paste("line 1", "line 2", sep = "\n")
> cat(a)
line 1
line 2> 

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:

> print(a)
[1] "line 1\nline 2"

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:

> a = "line1\nline2"
> cat(a)
line1
line2> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文