knitr templates and child documents in a loop

一世执手 提交于 2019-12-03 12:37:09
Mateusz Kobos

The answer given by Sean didn't work for me. The code that worked for me was:

# Master document

```{r echo=FALSE, include=FALSE}
library(knitr)

out = NULL
for (i in 1:3) {
  # lets create three different uniform data sets.
  v1 <- sort(rnorm(2))
  uvec <- runif(10, v1[[1]], v1[[2]])
  out <- c(out, knit_child('child.Rmd'))
}
```

```{r, echo=FALSE, results="asis"}
cat(paste(out, collapse = '\n'))
```

One answer is to use knit_expand() instead of knit_child() and to knit(, quiet=TRUE) later - here is a revised master document.

# Master document
```{r master1, comment='', echo=FALSE, message=FALSE, warning=FALSE, results="asis", fig.width= 4., fig.height= 4, fig.cap= ""}
out = NULL
for (i in 1:3) {
  # lets create three different uniform data sets.
  v1 <- sort(rnorm(2))
  uvec <- runif(10, v1[[1]], v1[[2]])
  out <- c(out, knit_expand('child.Rmd'))
}
cat(knit(text=unlist(paste(out, collapse = '\n')), quiet=TRUE))
```

I ran into the same problem with Latex instead of markdown and the solution for me was to use the \Sexpr instead of a code block

In addition, knit_child('child.file', quiet=TRUE) works as well

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!