如何评估 Rmarkdown 中的所有块

How to evaluate all chunks in Rmarkdown

如何评估 Rmd 文档中的所有块,而不在每个块处设置 eval=TRUE?按照我下面的方式,只评估第一个块。

```{r,eval=TRUE}
1+1
```

Some text

```
2+2
```

编辑:

我正在尝试 knit/compile 到 HTML。

```
require(knitr)
opts_chunk$set(eval = TRUE, tidy = TRUE, cache = FALSE, echo = FALSE, include = FALSE,
               fig.path = 'figures/', dev = c("pdf"),
               fig.width = 7, fig.height = 7)
```
some text

```
1+1
```
more text
```
2+2
```

在您的第一个块中,您可以全局设置 knitr 选项。

opts_chunk$set(tidy = TRUE, cache = FALSE, echo = FALSE, include = FALSE,
    fig.path = 'figures/', dev = c("pdf"),
    fig.width = 7, fig.height = 7)

在任何后续块中,您可以通过通常的方式更改它们,但它们仅适用于该块。

编辑。这是来自 K 的更完整的示例。 Broman

```{r global_options, include=FALSE}
knitr::opts_chunk$set(fig.width=12, fig.height=8, fig.path='Figs/',
                      echo=FALSE, warning=FALSE, message=FALSE)
```

eval=TRUE 是 .Rmd 块的默认行为,因此您不需要将其显式添加到块的选项中。

但是,您 do 需要在打开 fences 之后包含 {r} 以便将块识别为 R 代码并进行相应评估。不使用 ```{r} 打开的块将不会是 运行,因此您看到的问题。

一个可行的例子可能是:

```{r}
1+1
```
Some text

```{r}
2+2
```

要插入一个新的、带有适当围栏和 {r} 的空块,您可以按 Ctrl + Alt+ i 在 Windows 上,或 + Option + i 在 Mac 上,或者单击 RStudio 源窗格右上角的这个图标(根据记忆,旧版本的 RStudio 在该一般区域有一个 'Insert' drop-down):