问题描述
在我的数据集中,我想使用R Markdown将60个组放入HTML报告中进行分析.因为我想将相同的分析应用于每个组,所以我希望有一种方法可以动态生成代码块/分析.
In my dataset, I have 60 groups that I want to analyze in put into an HTML report using R Markdown. Because I want to apply the same analysis to each group, I am hoping that there is a way I can dynamically generate the code blocks/analysis.
简而言之,我想避免重复60次.
Simply, I want to avoid replicating the block 60 times.
我遇到了这个这个问题,该问题在knitr
中使用了子级.我试图用虹膜数据集复制它.在下面的示例中,我要做的就是生成三个H4标题,每个物种一个.
I came across this this question which uses children in knitr
. I have attempted to replicate this with the iris dataset. In my example below, all I wanted to do was generate three H4 titles, one for each species.
值得注意的是,我还没有嫁给这种方法,它似乎与我要做的事情有关.
It's worth noting that I am not married to this approach, it just appears to be related to what I am looking to do.
这是我使用的文件:
parent.RMD
文件.这将是我的主"报告.
parent.RMD
file. This would be my "master" report.
Automate Chunks of Analysis in R Markdown
========================================================
```{r setup, echo=FALSE}
library(knitr)
```
```{r run-numeric-md, include=FALSE}
out = NULL
for (i in as.character(unique(iris$Species))) {
out = c(out, knit_child('child.Rmd'))
}
```
这是child.Rmd
.
#### Species = `r [i]`
推荐答案
尝试knit_expand()
:
Automate Chunks of Analysis in R Markdown
========================================================
```{r setup, echo=FALSE}
library(knitr)
```
```{r run-numeric-md, include=FALSE}
out = NULL
for (i in as.character(unique(iris$Species))) {
out = c(out, knit_expand(text='#### Species = {{i}}'))
}
```
`r paste(knit(text = out), collapse = '\n')`
您还可以创建一个像'child.rmd'
这样的模板文件,并将其放在for循环中,这样就不必在引号中进行复杂的分析:
You can also create a template file like 'child.rmd'
and put this in your for loop so you don't have to put a complicated analysis in quotes:
out = c(out, knit_expand('template.rmd'))
然后将您的'template.rmd'
是:
#### Species = {{i}}
这篇关于生成动态R降价块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!