R Markdown - Format Text in Code Chunk with New Lines

r markdown - format text in code chunk with new lines

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:

Sample Image

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

Line breaks in R Markdown text (not code blocks)

I tried these tests, it seems to be working:

test.Rmd

---
output: pdf_document
---

# test 1
No spaces used

line1
line2

# test 2
2spaces at the end of line1

line1
line2

# test 3
2spaces at the end of line1, then 2 spaces on next line

line1

line2

Sample Image

sessionInfo()
R version 3.2.0 (2015-04-16)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United Kingdom.1252
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C
[5] LC_TIME=English_United Kingdom.1252

attached base packages:
[1] stats graphics grDevices utils datasets methods base

loaded via a namespace (and not attached):
[1] htmltools_0.2.6 tools_3.2.0 yaml_2.1.13 rmarkdown_0.5.1 digest_0.6.8

Format text inside R code chunk

You are using results='asis', hence, you can simply use print() and formatting markup. If you want your text to be red, simply do:

```{r,results='asis', echo=FALSE}
print("<div class='red2'>")
rd <- sample(x=1e6:1e7, size = 10, replace = FALSE)
cat(rd, sep = "\n")
print("</div>")
```

Hope it helps.

How to add new line in Markdown presentation?

See the original markdown specification (bold mine):

The implication of the “one or more consecutive lines of text” rule is that Markdown supports “hard-wrapped” text paragraphs. This differs significantly from most other text-to-HTML formatters (including Movable Type’s “Convert Line Breaks” option) which translate every line break character in a paragraph into a <br /> tag.

When you do want to insert a <br /> break tag using Markdown, you end a line with two or more spaces, then type return.

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")
}


Related Topics



Leave a reply



Submit