[英]Adding a line break to code blocks in R Markdown
I am using the knitr package with R Markdown to create an HTML report. 我正在使用带有R Markdown的knitr包来创建HTML报告。 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 将返回以下HTML文档
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! 任何帮助解决这个问题将不胜感激!
try chunk option tidy = FALSE
: 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 . 我发现将块的“整洁”设置更改为false的一种方法是添加一个中间命令注释 。 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. 示例:将下面的原始文本复制到Rmd文件中并使用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 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.