Adding a line break to code blocks in R Markdown

20,795

Solution 1

try chunk option tidy = FALSE:

```{r tidy=FALSE}
ggplot2(mydata, aes(x, y)) +
  geom_point() +
  geom_line() +
  opts(panel.background = theme_rect(fill = "lightsteelblue2"),
       panel.border = theme_rect(col = "grey"),
       panel.grid.major = theme_line(col = "grey90"),
       axis.ticks = theme_blank(),
       axis.text.x  = theme_text (size = 14, vjust = 0),
       axis.text.y  = theme_text (size = 14, hjust = 1.3))
```

Solution 2

One way I found to change the "tidy" setting of a chunk to false is to add a mid-command comment. This seems to make the whole chunk processed as non-tidy, thereby respecting line breaks you have (or have not) in your code. Unfortunately, this does not add a line break at as specific location (for a specific line).

Example: Copy the raw text below into an Rmd file and process with knitr.

Tidied (i.e. default)

Input

```{r eval=FALSE}
# Line comments do not seem to change tidiness.
list(
    sublist=list( 
        suba=10, subb=20 ),
    a=1,
    b=2 ) # End of line comment does not seem to change tidiness.
    
list(
    sublist=list( 
        suba=10, subb=20 ),
    a=1,
    b=2 )

```

Output

# Line comments do not seem to change tidiness.
list(sublist = list(suba = 10, subb = 20), a = 1, b = 2) # End of line comment does not seem to change tidiness.

list(sublist = list(suba = 10, subb = 20), a = 1, b = 2)

Untidied

Input

```{r eval=FALSE}
list(
    sublist=list( 
        suba=10, subb=20 ),
    a=1, # Mid-command comment seems to "untidy" the chunk.
    b=2 )
    
list(
    sublist=list( 
        suba=10, subb=20 ),
    a=1,
    b=2 )

```

Output

list(
    sublist=list(
        suba=10, subb=20 ),
    a=1, # Mid-command comment seems to "untidy" the chunk.
    b=2 )

list(
    sublist=list(
        suba=10, subb=20 ),
    a=1,
    b=2 )
Share:
20,795
susjoh
Author by

susjoh

R-User (data wrangling, analysis, visualisation).

Updated on July 09, 2022

Comments

  • susjoh
    susjoh almost 2 years

    I am using the knitr package with R Markdown to create an HTML report. I am having some trouble keeping my code on separate lines when using '+'.

    For example,

    ```{r}
    ggplot2(mydata, aes(x, y)) +
       geom_point()
    ```
    

    will return the following the the HTML document

    ggplot2(mydata, aes(x, y)) + geom_point()
    

    Normally this is fine, but the problem arises once I start adding additional lines, which I want to keep separate to make the code easier to follow. Running the following:

    ```{r}
    ggplot2(mydata, aes(x, y)) +
       geom_point() +
       geom_line() +
       opts(panel.background = theme_rect(fill = "lightsteelblue2"),
            panel.border = theme_rect(col = "grey"),
            panel.grid.major = theme_line(col = "grey90"),
            axis.ticks = theme_blank(),
            axis.text.x  = theme_text (size = 14, vjust = 0),
            axis.text.y  = theme_text (size = 14, hjust = 1.3))
    ```
    

    Will result in all the code coming out in one line, making it harder to follow:

    ggplot2(mydata, aes(x, y)) + geom_point() + geom_line() + opts(panel.background = theme_rect(fill = "lightsteelblue2"), panel.border = theme_rect(col = "grey"), panel.grid.major = theme_line(col = "grey90"), axis.ticks = theme_blank(), axis.text.x  = theme_text (size = 14, vjust = 0), axis.text.y  = theme_text (size = 14, hjust = 1.3))
    

    Any help in solving this would be greatly appreciated!

  • wch
    wch almost 12 years
    Is there a way to set this globally for a whole document?
  • kohske
    kohske almost 12 years
    Yes, IIRC opts_chunk$set(tidy = FALSE)
  • Rafael Bento
    Rafael Bento over 11 years
    Hrm. What if you want a line break between sections of code - say, you had an extra line break between geom_point and geom_line - even with tidy=FALSE, that line break goes away.