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

553    Asked by YokoyamaOhno in Devops , Asked on Jul 9, 2021

How do I use the new line character in R?

myStringVariable <- "Very Nice ! I like";
myStringVariabel <- paste(myStringVariable, "n", sep="");

The above code DOESN'T work. How to add new line in r?

P.S There are significant challenges when googling this kind of stuff since the query "R new line character" does seem to confuse google. I really wish R had a different name.

Answered by Bernadette Bond

When you simply print out a vector, the nature of R is such that you're never going to have a newline in a character vector.

> print("hello
world
")
 [1] "hello
world
"
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
world
") hello world
You can also use the writelines() function as follows: 

writeLines("hello
world")
hello
world


Your Answer

Interviews

Parent Categories