24

Question

Within a code chunk in an R Markdown (.Rmd) document how do you parse a string containing new line characters \n, to display the text on new lines?

Data and example

I would like to parse text <- "this is\nsome\ntext" to be displayed as:

this is
some
text 

Here is an example code chunk with a few attempts (that don't produce the desired output):

```{r, echo=FALSE, results='asis'}

text <- "this is\nsome\ntext"  # This is the text I would like displayed

cat(text, sep="\n")     # combines all to one line
print(text)             # ignores everything after the first \n
text                    # same as print

```

Additional Information

The text will come from a user input on a shiny app.

e.g ui.R

tags$textarea(name="txt_comment")      ## comment box for user input

I then have a download button that uses a .Rmd document to render the input:

```{r, echo=FALSE, results='asis'}
input$txt_comment
```

An example of this is here in the R Studio gallery

tospig
  • 7,762
  • 14
  • 40
  • 79
  • What is the source of the text? Manual entry? The result of another function? – A5C1D2H2I1M1N2O1R2T1 Apr 11 '15 at 08:29
  • @AnandaMahto The text comes from an html `textarea` on a Shiny app – tospig Apr 11 '15 at 08:29
  • How do you want it displayed? As a code chunk or just as regular paragraph text? – A5C1D2H2I1M1N2O1R2T1 Apr 11 '15 at 08:41
  • @AnandaMahto as a paragraph of text – tospig Apr 11 '15 at 08:42
  • So i tried this in R: `> cat("hello\nworld\n")` `hello` `world` Do not know the result in Rmarkdown though. – Sigve Karolius Apr 11 '15 at 09:01
  • @SigveKarolius You'll see in the question that I tried that in markdown and it outputs it on one line. I'm after new lines where the `\n` is. – tospig Apr 11 '15 at 09:04
  • 1
    Interesting. This is a question of how Rmarkdown is being parsed I believe. I just started using it recently, but as far as I know the `knit` function is being used to create a markdown file and then it becomes parsed by pandoc or some other markdown parser to produce the final document(? thinking out loud here). Have you tried to look at the markdown file? My point iv view is that if the output (from `cat`) is printed in a regular markdown text field it requires new line and two spaces in order to be parsed as "new line" by markdown. – Sigve Karolius Apr 11 '15 at 09:13
  • [This](http://stackoverflow.com/questions/26819258/format-text-inside-r-code-chunk) question uses native html/latex code to achieve something very similar using the `paste` function. However, in order for it to work in your example you have to process the text to determine when to insert "new line". I haven't tried the regexp capabilities of R, but if it has an equivalent of Matlabs `strsplit` it should be quite possible. – Sigve Karolius Apr 11 '15 at 10:19
  • 1
    @SigveKarolius your comment about requiring two spaces was the solution required, as Floo0 demonstrates in his answer. – tospig Apr 12 '15 at 21:58

1 Answers1

35

The trick is to use two spaces before the "\n" in a row: So replace "\n" by " \n"

Example:

```{r, results='asis'}
text <- "this is\nsome\ntext"

mycat <- function(text){
  cat(gsub(pattern = "\n", replacement = "  \n", x = text))
}

mycat(text)
```

Result:

enter image description here

P.S.: This is the same here on SO (normal markdown behaviour)
If you want just a linebreak use two spaces at the end of the line you want to break

tospig
  • 7,762
  • 14
  • 40
  • 79
Rentrop
  • 20,979
  • 10
  • 72
  • 100
  • thank you for this! I was struggling for like an hour on this. I could get it to double space with \n\n, but it would never recognize \n. The double space prefix works perfectly " \n" though I feel like this is a pretty serious bug or at least should be better documented somewhere. – mattador Feb 15 '21 at 04:55