Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用knitr对块的动态调用数_R_Knitr_R Markdown - Fatal编程技术网

使用knitr对块的动态调用数

使用knitr对块的动态调用数,r,knitr,r-markdown,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 它有三个元素,但其长度可以更改(根据此处未显示的数据)。我想在块中显示每个元素 我的第一个想法是在每一步编写一个包含循环调用knit\u ch

我有这样一份清单:

> 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
它有三个元素,但其长度可以更改(根据此处未显示的数据)。我想在块中显示每个元素

我的第一个想法是在每一步编写一个包含循环调用
knit\u child()
的块,但我不知道如何使用
knit\u child()
获得正确的渲染

我已经找到了下面的解决方案,它运行良好,但需要两个
Rmd
文件;第一个调用第二个,第二个递归调用自身:

mainfile.Rmd

```{r, echo=FALSE}
J <- length(lSlopes)
i <- 1
```

```{r child, child="stepfile.Rmd"}
```
Nice!
```{r, echo=FALSE}
lSlopes[[i]]
i <- i+1
```

```{r child, child="stepfile.Rmd", eval= i <= J}
```
`{r,echo=FALSE}

下面的J是使用
knit\u child()
类似的RMarkdown解决方案。作为我的解决方案,它需要两个文件,但要清楚得多

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!
```{r, echo=FALSE}
lSlopes[[i]]
```

顺便说一句,如果你想包含一些内联R代码,这个解决方案会有问题:大量的例子在:020069075。。。学习这些示例后,请随时回答您自己的问题。谢谢@Yihui,效果很好@易慧,我不明白为什么在我的答案中给出的代码中有选项
include
?有点奇怪:事实上,我是从一个闪亮的应用程序中编织这些文件的,代码生成了一个带有数字的意外文件夹。@Yihui啊,好的,我已经删除了
include=FALSE
,我理解这一点。但是文件夹里的数字很奇怪。。。我将尝试隔离问题并将其发布到其他地方。