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.
Asked
Active
Viewed 3,214 times
1

jeandut
- 2,471
- 4
- 29
- 56
-
there are at least a few packages that let you output data to pdf tables, like `xtable` – Rorschach Jun 10 '15 at 21:02
-
Thanks a lot this seeems to convert it to a nice LateX object !!! But how can I use it to display with the pdf function ? This code does not return anything : `pdf("report.pdf",width=7,height=7)` `p<-xtable(dataframe)` `dev.off()` – jeandut Jun 10 '15 at 21:12
-
do you need to specifically use the `pdf` function? or are you just trying to output data to pdf? – Rorschach Jun 10 '15 at 21:14
-
I would rather use pdf function but if you have something as simple (not the markdown) and that can also work with plots that could interest me a great deal ! – jeandut Jun 10 '15 at 21:20
2 Answers
0
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.

Daniel Anderson
- 2,394
- 13
- 26
0
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 !