gpt4 book ai didi

r - 在 R Markdown 中为代码块添加换行符

转载 作者:行者123 更新时间:2023-12-03 14:03:19 64 4
gpt4 key购买 nike

我正在使用带有 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))

任何解决此问题的帮助将不胜感激!

最佳答案

尝试 block 选项 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))
```

关于r - 在 R Markdown 中为代码块添加换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11306745/

64 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com