在R Markdown中向代码块添加换行符 [英] Adding a line break to code blocks in R Markdown

查看:551
本文介绍了在R Markdown中向代码块添加换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有R Markdown的knitr包来创建HTML报告.使用'+'时,我无法将代码保持在单独的行上.

I am using the knitr package with R Markdown to create an HTML report. I am having some trouble keeping my code on separate lines when using '+'.

例如,

```{r}
ggplot2(mydata, aes(x, y)) +
   geom_point()
```

将返回以下HTML文档

will return the following the the HTML document

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!

推荐答案

尝试块选项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 Markdown中向代码块添加换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆