I\'d like to print nicely-formatted data frames to paper, ideally from within a script. (I am trying to collect data using an instrument and automatically process and print
The RStudio IDE gives another nice option to print out a data.table:
View(data_table)
or via the GUIThis works in RStudio V0.98.1103 (and probably newer versions)
Not as fancy, but very utilitarian:
print.data.frame(iris)
If you want to export as a png, you can do like this:
library(gridExtra)
png("test.png", height = 50*nrow(df), width = 200*ncol(df))
grid.table(df)
dev.off()
If you want to export as a pdf, you can do like this:
library(gridExtra)
pdf("test.pdf", height=11, width=10)
grid.table(df)
dev.off()
Here is a quick and easy possibility using grid.table
from the gridExtra package:
library(gridExtra)
pdf("data_output.pdf", height=11, width=8.5)
grid.table(mtcars)
dev.off()
If your data doesn't fit on the page, you can reduce the text size grid.table(mtcars, gp=gpar(fontsize=8))
. This may not be very flexible, nor easy to generalize or automate.
The printr package is a good option for printing data.frames
, help pages, vignette listings, and dataset listings in knitr documents.
From the documentation page:
options(digits = 4)
set.seed(123)
x = matrix(rnorm(40), 5)
dimnames(x) = list(NULL, head(LETTERS, ncol(x)))
knitr::kable(x, digits = 2, caption = "A table produced by printr.")
For long/wide tables you could use pander.
It will automatically split long tables into shorter parts that fit the page, e.g. using knitr insert this chunk into your Rmd file:
pander::pander(mtcars)
If you want something that looks more like Excel tables (even with editing options in html) then use rhandsontable. More info about usage and formatting in the vignette. You will need to knit your Rmd into an html file:
library(rhandsontable)
rhandsontable(mtcars, rowHeaders = NULL)