Printing Newlines with Print() in R

R - do I need to add explicit new line character with print()?

The nature of R means that you're never going to have a newline in a character vector when you simply print it out.

> print("hello\nworld\n")
[1] "hello\nworld\n"

That is, the newlines are in the string, they just don't get printed as new lines. However, you can use other functions if you want to print them, such as cat:

> cat("hello\nworld\n")
hello
world

Printing newlines with print() in R

An alternative to cat() is writeLines():

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

An advantage is that you don't have to remember to append a "\n" to the string passed to cat() to get a newline after your message. E.g. compare the above to the same cat() output:

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

and

> 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() and writeLines() to display the string. I say "a version" because precision may be reduced in printed numerics, and the printed object may be augmented with extra information, for example.

Add line break in print statement in R

I assume your sample output should actually be three lines instead of two... You should use cat instead of print, and add sep="\n" to the paste statement:

 cat(paste("hey I want this to be line one", 
"and this to be line two",
"would be great if you could help" ,sep="\n"))

Output:

hey I want this to be line one
and this to be line two
would be great if you could help

How to output text in the R console without creating new lines?

Use cat() instead of print():

cat("0%")
cat("..10%")

Outputs:

0%..10%

R - Insert newline in a variable (not for printing purposes)

as you mentioned cat() is used for printing puposes. It interpretes "\n" as a linebreak, but does not return you a concenated character. Therefore to concenate the character you could use paste() and then call cat() to print it:

method = "method"
canonical_uri = "canonical_uri"
canonical_querystring = "querystring"
out <- paste(method, "\n", canonical_uri, "\n", canonical_querystring,sep = "")
cat(out)

Printing from a function in Rmarkdown that allows newlines and escaping Markdown and Latex commands

I figured out a way to do what I wanted using paste():

stackOverflow <- function() {
paste(c(
"This is the **first** line printed from a JSON file",
"\\vspace{1em}",
"\n",
"This is the \\textbf{second} line printed from a JSON file",
"\n",
"\\vspace{1em}"
), collapse = "\n")
}

Putting the multiline output in a list nested in paste() using collapse = "\n" results in the following when executed:

[1] "This is the **first** line printed from a JSON file\n\\vspace{1em}\nThis is the \textbf{second} line printed from a JSON file\n\\vspace{1em}"

Which gets rendered correctly:

Rendered output

It's not perfect as Rmarkdown, for example, requires two spaces at the end or a blank line between text to put it in a new line hence why I added the extra "\n" characters. For now it works, however, so I'm marking it as the correct answer in the hopes that it will help others. If someone can come up with a better solution I'll happily mark theirs as correct.

Edit:

The extra line/two spaces required by markdown can, in most cases, be solved by collapsing with two line break characters like so: "\n\n"

This changes the above code to:

stackOverflow <- function() {
paste(c(
"This is the **first** line printed from a JSON file",
"\\vspace{1em}",
"This is the \\textbf{second} line printed from a JSON file",
"\\vspace{1em}"
), collapse = "\n\n")
}

Print multiple lines without printing the print()

This will do what you are looking for cat("Line 1 \nLine 2")

 >cat("Line 1 \nLine 2")
Line 1
Line 2

See R - do I need to add explicit new line character with print()?



Related Topics



Leave a reply



Submit