Looping through code in knitr and rmarkdown

。_饼干妹妹 提交于 2019-12-18 18:20:35

问题


I have been putting together reports using Knitr and rmarkdown. I love being able to output my R code in a reproducible R format, however I am continuously running into a limitation and am wondering who has tackled this before. I need to loop through say two pages with various content and knitr settings for each collection of data in a list. Once the code for those two pages is written I would like to fill in i for each collection in a list (1, 2, 3, 4, 5, etc).

For the example below I have master_list, which has x1_list & x2_list in it. Currently I set up a num variable before each sets of code and change the number accordingly (1, 2, 3, etc). This is inefficient since the code is all the same, just pasted twice. How can I best loop through the code.

To be clearer. I want to loop through the code before as many times as their are items in master_list (right now just twice), without rewriting the code each time.

Part of code I want to repeat (full reproducible example below)

## Name `r num`: First Table

\vspace*{0.5in}

```{r echo = F, results = 'asis', message = F, warning = F, fig.width = 8, fig.height = 8, size = 'Large'}
library(xtable)
print(xtable(master_list[[num]][[1]]),type='latex',comment = FALSE,floating=FALSE)

```

\newpage

## Manager `r num`: Second Table

```{r echo = F, results = 'asis', message = F, warning = F, fig.align = 'center', fig.width = 9, fig.height = 7, fig.show = 'hold', size = 'Large'}

print(xtable(master_list[[num]][[2]]),type='latex',comment = FALSE,floating=FALSE)
```       

\newpage

Below is an example of a .Rmd file running in Rstudio (I click Knit PDF to generate the PDF report with 4 pages).

---
output: 
   pdf_document:
      includes:

classoption: landscape
geometry: margin=1.75cm
---

`r  x1_list <- list(x1_front <- data.frame("col1" = rnorm(10), "col2" = rnorm(10)), x1_back <- data.frame("col1" = rnorm(10), "col2" = rnorm(10))) `
`r  x2_list <- list(x2_front <- data.frame("col1" = rnorm(10), "col2" = rnorm(10)), x2_back <- data.frame("col1" = rnorm(10), "col2" = rnorm(10))) `
`r master_list <- list(x1_list, x2_list) `

`r num <- 1` 

## Name `r num`: First Table

\vspace*{0.5in}

```{r echo = F, results = 'asis', message = F, warning = F, fig.width = 8, fig.height = 8, size = 'Large'}
library(xtable)
print(xtable(master_list[[num]][[1]]),type='latex',comment = FALSE,floating=FALSE)

```

\newpage

## Manager `r num`: Second Table

```{r echo = F, results = 'asis', message = F, warning = F, fig.align = 'center', fig.width = 9, fig.height = 7, fig.show = 'hold', size = 'Large'}

print(xtable(master_list[[num]][[2]]),type='latex',comment = FALSE,floating=FALSE)
```       

\newpage

`r num <- 2` 

## Name `r num`: First Table

\vspace*{0.5in}

```{r echo = F, results = 'asis', message = F, warning = F, fig.width = 8, fig.height = 8, size = 'Large'}

print(xtable(master_list[[num]][[1]]),type='latex',comment = FALSE,floating=FALSE)

```

\newpage

## Manager `r num`: Second Table

```{r echo = F, results = 'asis', message = F, warning = F, fig.align = 'center', fig.width = 9, fig.height = 7, fig.show = 'hold', size = 'Large'}

print(xtable(master_list[[num]][[2]]),type='latex',comment = FALSE,floating=FALSE)
```       

回答1:


For anyone finding this question, I finally found a reliable way to loop through markdown/latex code in a flexible manner, hope it helps. Basically you can put your rmarkdown header/footer information and then put each loop item specific code into the child. With one loop call it will generate as many child items as you want, reusing that code each time.

Code for main.Rmd

---
  output: 
  pdf_document:
  includes:

  classoption: landscape
geometry: margin=1.75cm
---

  `r  x1_list <- list(x1_front <- data.frame("col1" = rnorm(10), "col2" = rnorm(10)), x1_back <- data.frame("col1" = rnorm(10), "col2" = rnorm(10))) `
`r  x2_list <- list(x2_front <- data.frame("col1" = rnorm(10), "col2" = rnorm(10)), x2_back <- data.frame("col1" = rnorm(10), "col2" = rnorm(10))) `
`r master_list <- list(x1_list, x2_list) `


```{r include = FALSE}
out = NULL
for (i in 1:2) {
  num <- i
  out <- c(out,knit_child('child.Rmd'))
}
```

`r paste(out, collapse='\n')`

Code for child.Rmd

\newpage

## Name `r num`: First Table

\vspace*{0.5in}

```{r echo = F, results = 'asis', message = F, warning = F, fig.width = 8, fig.height = 8, size = 'Large'}
library(xtable)
print(xtable(master_list[[num]][[1]]),type='latex',comment = FALSE,floating=FALSE)

```

\newpage

## Manager `r num`: Second Table

```{r echo = F, results = 'asis', message = F, warning = F, fig.align = 'center', fig.width = 9, fig.height = 7, fig.show = 'hold', size = 'Large'}

print(xtable(master_list[[num]][[2]]),type='latex',comment = FALSE,floating=FALSE)
```    


来源:https://stackoverflow.com/questions/30893358/looping-through-code-in-knitr-and-rmarkdown

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