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!
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.
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))
```
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.
```{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 )
```
# 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)
```{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 )
```
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 )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With