[英]Adding a line break to code blocks in R Markdown
我正在使用帶有R Markdown的knitr包來創建HTML報告。 使用'+'時,我在將代碼保持在單獨的行上時遇到了一些麻煩。
例如,
```{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))
任何幫助解決這個問題將不勝感激!
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))
```
我發現將塊的“整潔”設置更改為false的一種方法是添加一個中間命令注釋 。 這似乎使整個塊處理為非整潔,從而尊重您在代碼中擁有(或沒有)的換行符。 不幸的是,這不會在特定位置(對於特定行)添加換行符。
示例:將下面的原始文本復制到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.