从最终 html 输出中排除部分 R markdown (html_notebook)

Excluding part of the R markdown (html_notebook) from the final html output

我正在使用 R Notebook 编写一份相对较长的报告,该报告使用 R markdown 语言,将文本和代码组合在同一文档中并生成 html 输出。

我希望能够在最终 HTML 中显示一些分析(包括文本和 R 代码)。如果我想创建两个版本的报告,这将非常有用 - full/detailed 版本,以及包含主要图表和结论的较短版本。

显然,我可以为每种类型的报告创建单独的 Rmd 文件(或者为较短的版本注释掉需要排除的部分报告),但我想知道是否有更优雅的方法它。

像这样:

     if (Version == "full_text"){

                Full analysis goes here 

                ```{r}
                R code goes here (could be multiple chunks)
                ```
     }
     else {
                The shorter version goes here 
                ```{r}
                R code goes here 
                ```
    }

将报告的 "detailed" 部分放在 knitr child document 中,您可以从主文档中选择调用它。 然后可以通过调用子文档来打开详细内容,也可以通过将变量 child_docs 设置为 NULL 来关闭详细内容。例如,下面是一个主文档和一个子文档。

子文档

将此文档保存在 knitr-child.Rmd

---
title: "knitr child"
output: html_document
---

# Details from the child document
Hi, there. I'm a child with a plot and as many details as necessary.

```{r test-child}
plot(cars)
```

主文档 - full/detailed 版本

---
title: "Report"
output: html_document
---

# Summary 

```{r setup}
child_docs <- c('knitr-child.Rmd')
# child_docs <- NULL
```

```{r test-main, child = child_docs}
```

# Conclusion

主文档较短版本

---
title: "Report"
output: html_document
---

# Summary 

```{r setup}
# child_docs <- c('knitr-child.Rmd')
child_docs <- NULL
```

```{r test-main, child = child_docs}
```

# Conclusion