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)  
>}  
>\```  
4
  • Can you please update your question to include the for loop you've written, with details on which part isn't working? Dec 15, 2015 at 1: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, 2015 at 2:17
  • See the Rendering markdown tables inside of of loop in knitr vignette of the pander package.
    – daroczig
    Dec 15, 2015 at 5: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, 2015 at 19:10

1 Answer 1

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")
}
```
2
  • 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, 2015 at 5:56
  • Check out these two questions
    – jeremycg
    Dec 15, 2015 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.