Using pdf function in R to save plots AND data frames

3.3k Views Asked by At

I recently discovered the function pdf() in R. I know it can only be used to save plots but I wanna use it to save texts and data frames too. So I am trying to cheat the function by using the grid package and grob objects but I cannot figure out how to do it.
Could somebody input a code showing how to convert a data.frame to something that can be saved by the pdf() function, or any other methods that would provide the same result ? I would appreciate.

2

There are 2 best solutions below

0
On BEST ANSWER

I found the answer somewhere else in stack :
writing data frame to pdf table
Adding text to a grid.table plot
Sorry for not having looked harder before !

0
On

If you're willing to move away from the pdf function, you could use a combination of knitr to produce the table in LaTeX format, and pandoc and rmarkdown to render it as a pdf. Here's a quick example:

sink("trial.Rmd")
```{r, echo = FALSE}
exampleDF <- data.frame(a = 1:5, b = letters[1:5])
knitr::kable(exampleDF)
```
sink()

rmarkdown::render("trial.Rmd", "pdf_document")

Note that this solution, as noted above, depends on having pandoc installed. But it also depends on having LaTeX installed. Hope it's helpful.

EDIT: Sorry, just noticed you mentioned you'd rather not use markdown, but the above should work if you're willing to go that way.