我一直在使用Knitr和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
下面是在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)
```
答案 0 :(得分:4)
对于任何发现这个问题的人,我终于找到了一种可靠的方式以灵活的方式循环显示降价/乳胶代码,希望它有所帮助。基本上,您可以放置您的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')`
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)
```