在R标记中向代码块添加换行符
我使用带有R标记的knitr包来创建HTML报告。在使用“+”时,我很难将代码保持在单独的行上 比如说,在R标记中向代码块添加换行符,r,markdown,knitr,R,Markdown,Knitr,我使用带有R标记的knitr包来创建HTML报告。在使用“+”时,我很难将代码保持在单独的行上 比如说, ```{r} ggplot2(mydata, aes(x, y)) + geom_point() ``` 将在HTML文档中返回以下内容 ggplot2(mydata, aes(x, y)) + geom_point() 通常这很好,但是一旦我开始添加额外的行,问题就出现了,我希望将这些行分开,以使代码更容易理解。运行以下命令: ```{r} ggplot2(mydata, aes
```{r}
ggplot2(mydata, aes(x, y)) +
geom_point()
```
将在HTML文档中返回以下内容
ggplot2(mydata, aes(x, y)) + geom_point()
通常这很好,但是一旦我开始添加额外的行,问题就出现了,我希望将这些行分开,以使代码更容易理解。运行以下命令:
```{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))
```
将导致所有代码在一行中显示,使其更难遵循:
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))
如果您能帮助解决此问题,我们将不胜感激 尝试区块选项
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))
```
我发现将块的“整洁”设置更改为false的一种方法是添加mid命令注释。这似乎使整个块被处理为不整洁的,从而考虑到代码中有(或没有)的换行符。遗憾的是,这不会在as特定位置(针对特定行)添加换行符 示例:将下面的原始文本复制到Rmd文件中,并使用knitr进行处理 整洁(即默认) 输入 输出 不整洁的 输入 输出
有没有一种方法可以对整个文档进行全局设置?是的,IIRC
opts\u chunk$set(tidy=FALSE)
Hrm。如果您想要代码段之间的换行符,比如说,在geom_点和geom_线之间有一个额外的换行符,那么即使使用tidy=FALSE,换行符也会消失。
```{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 )