Reputation: 129
I have about 60 .Rdata files in the same directory. The object name in all those .Rdata are same. I want to write some code to load and print all 60 .Rdata file and each file in the new page. For example, if the file name is file_1.rdata
, file_2.rdata
and file_3.rdata
. The object name in all three .Rdata files is table
. The following knitr code showed exactly what I want,
>\```{r,echo=FALSE}
>load("file_1.rdata")
>print(table)
>\```
>\pagebreak
>\```{r,echo=FALSE}
>load("file_2.rdata")
>print(table)
>\```
>\pagebreak
>\```{r,echo=FALSE}
>load("file_3.rdata")
>print(table)
>```
>\pagebreak
But I have more than 60 files, it is really hard to write all the code by hand. I can write for loop in R block, however, how can I make a new page for each .rdata file?
The for loop will be
>\```{r,echo=FALSE}
>names <- c("file_1.rdata","file_2.rdata","file_3.rdata")
>for(i in 1:length(names)){
> current_object <- names[i]
> load(current_object)
> print(table)
>}
>\```
Upvotes: 2
Views: 1425
Reputation: 24955
You can try adding in cat("\n\n\\pagebreak\n")
inside your for loop, and results='asis'
to your chunk call:
```{r,echo=FALSE, results='asis'}
names <- c("file_1.rdata","file_2.rdata","file_3.rdata")
for(i in 1:length(names)){
current_object <- names[i]
load(current_object)
print(table)
cat("\n\n\\pagebreak\n")
}
```
It works for me with mtcars:
---
title: "test"
output: pdf_document
---
```{r, echo=FALSE, results='asis'}
for (i in 1:3) {
print(mtcars)
cat("\n\n\\pagebreak\n")
}
```
NB you might want to look into the function kable
to format your tables more nicely. Or using library(xtable)
:
```{r, echo=FALSE, results='asis'}
for (i in 1:3) {
print(xtable::xtable(mtcars), type = "latex")
cat("\n\n\\pagebreak\n")
}
```
Upvotes: 5