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

标签 r markdown knitr

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

相关文章:

R 传单 : addPolygons by group

r - 函数 : nested conditions 中的 dplyr NSE 模式

r - 仅从 Rmd 文档中提取文本

r - inline::cxxfunction 在 knitr 中显示不佳

r - spplot() - 使 color.key 看起来不错

r - 在整个数据框中查找多个字符串

visual-studio - 如何使用 visual studio code 将数学公式插入到 markdown 文件中?

windows - 如何使用 UTF-8 编码编织 2html?

markdown - pandoc:如何链接到另一个 Markdown 文件中的部分?

regex - 在 Pandoc Markdown 输出中生成内联而不是列表样式的脚注?