是否有可能有条件地评价一个代码块和,使用R降价和其相关的标题knitr?例如,如果eval_cell是TRUE包括块和它的标题,但无论是如果不包括eval_cell的FALSE.
```{r}
eval_cell = TRUE
```
# Heading (would like to eval only if eval_cell is TRUE)
```{r eval = eval_cell}
summary(cars)
```
Run Code Online (Sandbox Code Playgroud)
您可以将标题放在内联R表达式中:
```{r}
eval_cell = TRUE
```
`r if (eval_cell) '# Heading (would like to eval only if eval_cell is TRUE)'`
```{r eval = eval_cell}
summary(cars)
```
Run Code Online (Sandbox Code Playgroud)
如果您需要有条件地包含大块文本/代码,这将变得很麻烦,在这种情况下,建议您将它们放在单独的子文档中,例如child.Rmd:
# Heading (would like to eval only if eval_cell is TRUE)
```{r}
summary(cars)
```
Run Code Online (Sandbox Code Playgroud)
然后在原始(父)文档中,您只需要
```{r}
eval_cell = TRUE
```
```{r child='child.Rmd', eval=eval_cell}
```
Run Code Online (Sandbox Code Playgroud)