循环遍历knitr和rmarkdown中的代码

Tre*_*lof 1 r knitr r-markdown

我一直在使用 Knit 和 rmarkdown 整理报告。我喜欢能够以可重现的 R 格式输出我的 R 代码,但是我不断遇到限制,并且想知道以前谁解决过这个问题。我需要循环遍历两个页面,其中包含列表中每个数据集合的各种内容和编织设置。一旦编写了这两页的代码,我想为列表中的每个集合填写 i (1、2、3、4、5 等)。

对于下面的示例,我有 master_list,其中包含 x1_list 和 x2_list。目前,我在每组代码之前设置了一个 num 变量,并相应地更改数字(1、2、3 等)。这是低效的,因为代码都是相同的,只是粘贴了两次。我怎样才能最好地循环代码。

说得更清楚一点。我想在 master_list 中的项目之前循环遍历代码(现在只需两次),而不是每次都重写代码。

我想重复的部分代码(下面是完整的可重现示例)

## 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
Run Code Online (Sandbox Code Playgroud)

下面是在 Rstudio 中运行的 .Rmd 文件的示例(我单击 Knit PDF 生成 4 页的 PDF 报告)。

---
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)
```       
Run Code Online (Sandbox Code Playgroud)

Tre*_*lof 5

对于任何发现这个问题的人,我终于找到了一种以灵活的方式循环遍历 markdown/latex 代码的可靠方法,希望它有所帮助。基本上,您可以放置​​ rmarkdown 页眉/页脚信息,然后将每个循环项特定代码放入子项中。通过一次循环调用,它将生成所需数量的子项,并且每次都会重用该代码。

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')`
Run Code Online (Sandbox Code Playgroud)

儿童代码.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)
```    
Run Code Online (Sandbox Code Playgroud)