r语言 - 从循环传递参数以在 Rmarkdown 中生成动态报告



我是Rmarkdown的新手,我想创建动态报告,其中每个报告部分都是从模板(子(文档生成的。然后,每个部分将从呈现的 pdf 中的新页面开始。

我的方法目前基于这篇文章,它显示了如何在子级中生成动态文本(有效(,但是我无法将循环的内容转移到 R 代码块中,可能是因为params没有很好地定义我尝试这样做的方式。

这是我的父文档的样子:

---
title: "Dynamic RMarkdown"
output: pdf_document
---

```{r setup, include=FALSE}
library("knitr")
options(knitr.duplicate.label = "allow")
```
# Automate Chunks of Analysis in R Markdown 
Blahblah Blabla
newpage
```{r run-numeric-md, include=FALSE}
out = NULL
for (i in as.character(unique(iris$Species))) {
out = c(out, knit_expand('template.Rmd'))
params <- list(species = i)
}
```
`r paste(knit(text = out), collapse = 'n')`

这就是孩子的样子

---
title: "template"
output: html_document
params:
species: NA
---
# This is the reporting section of Species {{i}}
This is a plot of Sepal length and width based on species {{i}}.
```{r plot2}
paste(params$species)
# plot doesnt work work
# plot(iris$Sepal.Length[iris$Species=={{i}}],
#     iris$Sepal.Width[iris$Species=={{i}}]
#     )
```
newpage

据我了解,实际传递的参数是循环中生成的数据集的最后一species,但我不确定为什么该图不起作用。任何人都可以帮助我解决这个问题吗?

好的,无需通过params.解决方案只是在子文档中的括号和括号之间放置i

父母:

---
title: "Dynamic RMarkdown"
output: pdf_document
---

```{r setup, include=FALSE}
library("knitr")
options(knitr.duplicate.label = "allow")
```
# Automate Chunks of Analysis in R Markdown 
Blahblah Blahblah Main text before individual sections
newpage
```{r run-numeric-md, include=FALSE}
out = NULL
for (i in as.character(unique(iris$Species))) {
out = c(out, knit_expand('template.Rmd'))
}
```
`r paste(knit(text = out), collapse = 'n')`

孩子

---
title: "template"
output: html_document
---
# This is the reporting page of Species {{i}}
This is a plot of Sepal length and width based on species {{i}}.
```{r plot2}
paste("This will be a plot of Sepal Length and Witdh from", '{{i}}')
plot(iris$Sepal.Length[iris$Species=='{{i}}'],
iris$Sepal.Width[iris$Species=='{{i}}']
)
```
newpage

原始解决方案在这里找到。

相关内容

最新更新