Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add line break in print statement in R [duplicate]

Tags:

r

When using the print function to print to the screen, I would like one line to appear on one line, and the next line to be on a second line.

with this line

print( 
    paste( 
        "hey I want this to be line one", "and this to be line two", "would be great if you could help"
    )
)

I want this to print

[1] "hey I want this to be line one

[2] and this to be line two would be great if you could help"

like image 721
MatthewR Avatar asked Jun 06 '14 00:06

MatthewR


People also ask

How do you print a line break in R?

To use a tab separator, use "\t", and use "\n" to create a line break.

How do I add a line break in printing?

Use "\n" to print a line break <a name="use-"\n""> Insert "\n" at the desired line break position.

How do you break a line in R markdown?

To break a line in R Markdown and have it appear in your output, use two trailing spaces and then hit return .

How do I start a new line in R?

The most commonly used are "\t" for TAB, "\n" for new-line, and "\\" for a (single) backslash character.


1 Answers

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
like image 195
beroe Avatar answered Oct 16 '22 15:10

beroe