Adding a line break to code blocks in R Markdown

2019-02-08 06:52发布

问题:

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!

回答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))
```


回答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 )


标签: r markdown knitr