Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 623
Jubbles Avatar asked Dec 21 '22 02:12

Jubbles


1 Answers

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> 
like image 130
Simon Urbanek Avatar answered Dec 24 '22 02:12

Simon Urbanek