Sté*_*ent 6 r knitr r-markdown
我有这样一个清单:
> lSlopes
$A
Estimate 2.5 % 97.5 %
1 2.12 -0.56 4.80
$B
Estimate 2.5 % 97.5 %
1 2.21 -0.68 5.10
$C
Estimate 2.5 % 97.5 %
1 2.22 -2.21 6.65
Run Code Online (Sandbox Code Playgroud)
它有三个元素,但其长度可以改变(根据此处未显示的数据).我想在一个块中显示每个元素.
我的第一个想法是knit_child()在每一步编写一个包含循环调用的块,但我不知道如何获得正确的渲染knit_child().
我发现以下解决方案效果很好,但需要两个Rmd文件; 第一个调用第二个,第二个递归调用自身:
mainfile.Rmd:
```{r, echo=FALSE}
J <- length(lSlopes)
i <- 1
```
```{r child, child="stepfile.Rmd"}
```
Nice!
Run Code Online (Sandbox Code Playgroud)
stepfile.Rmd:
```{r, echo=FALSE}
lSlopes[[i]]
i <- i+1
```
```{r child, child="stepfile.Rmd", eval= i <= J}
```
Run Code Online (Sandbox Code Playgroud)
这准确地生成了我想要的渲染:
我喜欢这个棘手的解决方案,但我想知道是否存在非递归解决方案?
下面是RMarkdown解决方案类似于https://github.com/yihui/knitr-examples/blob/master/020-for-loop.Rnw使用knit_child().作为我的解决方案,它需要两个文件,但它更清晰.
mainfile.Rmd:
```{r, echo=FALSE}
J <- length(lSlopes)
```
```{r runall, include=FALSE}
out <- NULL
for (i in 1:J) {
out <- c(out, knit_child('stepfile.Rmd'))
}
```
`r paste(out, collapse = '\n')`
Nice!
Run Code Online (Sandbox Code Playgroud)
stepfile.Rmd:
```{r, echo=FALSE}
lSlopes[[i]]
```
Run Code Online (Sandbox Code Playgroud)