2

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)  
>}  
>\```  
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
David Lee
  • 129
  • 1
  • 9
  • Can you please update your question to include the `for` loop you've written, with details on which part isn't working? – Simon MᶜKenzie Dec 15 '15 at 01:58
  • Thank you for your advice. I have already wrote the for loop. The part I do not how to deal with is how to make a new page for each .rdata file. – David Lee Dec 15 '15 at 02:17
  • See the [Rendering `markdown` tables inside of of loop in `knitr`](https://cran.r-project.org/web/packages/pander/vignettes/knitr.html#rendering-markdown-inside-loopvectorized-function) vignette of the `pander` package. – daroczig Dec 15 '15 at 05:34
  • Note there are ways to explore the directory of files using R commands as well. If your directory contains *only* the files you want to load, then you could do something like: `names <- dir()` or `names <- list.files()`. (Those commands support regular expression filename patter matching as well; see the help files.) – Kalin Dec 15 '15 at 19:10

1 Answers1

5

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")
}
```
jeremycg
  • 24,657
  • 5
  • 63
  • 74
  • Thanks. It works. Can I ask one more question? How should I deal with if row in the table is wider than the page? Even some reference will also help me a lot, thanks. – David Lee Dec 15 '15 at 05:56
  • Check out these [two](https://stackoverflow.com/questions/16507191/automatically-adjust-latex-table-width-to-fit-pdf-using-knitr-and-rstudio) [questions](https://stackoverflow.com/questions/30417073/resizing-a-stargazer-table-in-knitr) – jeremycg Dec 15 '15 at 13:28