2

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

我希望能够从最终 HTML 中的显示中排除一些分析(文本和 R 代码)。如果我想创建两个版本的报告 - 完整/详细版本,以及带有主图和结论的较短版本,这将非常有用。

显然,我可以为每种类型的报告创建单独的 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 
                ```
    }
4

1 回答 1

2

将报告的“详细”部分放在您可以从主文档中选择调用的knitr 子文档中。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)
```

主要文件 - 完整/详细版本

---
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

较短的版本

于 2017-03-23T02:17:31.983 回答