当前位置: 动力学知识库 > 问答 > 编程问答 >

r - Create persistent multi-line string

问题描述:

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).

网友答案:

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> 
分享给朋友:
您可能感兴趣的文章:
随机阅读: