Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a line break to code blocks in R Markdown

Tags:

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!

like image 840
susjoh Avatar asked Jul 03 '12 08:07

susjoh


People also ask

How do you break a line in code blocks?

Type out the new number such as 18. Then make sure you have one space after the period. Then press the Enter key once. Add 4 spaces to the beginning of the line that you are presently on.


2 Answers

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))
```
like image 197
kohske Avatar answered Sep 18 '22 16:09

kohske


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 )
like image 27
Kalin Avatar answered Sep 18 '22 16:09

Kalin