How to print (on paper) a well-formatted data frame

I would like to print well formatted data frames on paper, ideally, from a script. (I'm trying to collect data using a tool and automatically process and print it using an R script).

Right now I can write a data frame to a text file using write.table() , but this has two problems:

  • The resulting text file is poorly formatted (columns do not necessarily match the headers) and
  • I do not know how to print a text file from R.

I am looking for more general strategies than for specific code (although the code will be great too!). Will Sweave be the most convenient solution? Basically, I can use socketConnection() to print to the printer - and if so, where can I find out how to use it (I did not find the documentation very useful).

+69
formatting r dataframe
May 14 '12 at 16:46
source share
10 answers

Here is a quick and easy way to use grid.table from the gridExtra package:

 library(gridExtra) pdf("data_output.pdf", height=11, width=8.5) grid.table(mtcars) dev.off() 

enter image description here

If your data does not fit on the page, you can reduce the size of the text grid.table(mtcars, gp=gpar(fontsize=8)) . It may not be very flexible, easy to generalize or automate.

+94
May 14 '12 at 17:31
source share

I suggest xtable in conjunction with LaTeX docs. Take a look at the examples in this pdf:

You can also directly combine this with Sweave or knitr .

+16
May 14 '12 at 16:55
source share

The grid.table solution will indeed be the fastest way to create a PDF, but it may not be the optimal solution if you have a fairly long table. RStudio + knitr + longtable makes it easy to create well-formatted PDF files. You will need something like:

 \documentclass{article} \usepackage{longtable} \begin{document} <<results='asis'>>= library(xtable) df = data.frame(matrix(rnorm(400), nrow=100)) xt = xtable(df) print(xt, tabular.environment = "longtable", floating = FALSE ) @ \end{document} 

See more details.

+6
Jan 02 '15 at 19:47
source share

Not as a fantasy, but very utilitarian:

 print.data.frame(iris) 
+4
Sep 20 '13 at 18:09
source share

The printr package is a good option for printing data.frames , help pages, vignette lists, and dataset lists in knitr .

On the 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.") 
+4
Jul 04 '15 at 13:45
source share

Surprised, no one mentioned the stargazer package for pleasantly printing data.

You can output a beautiful text file:

 stargazer(mtcars, type = 'text', out = 'out.txt') ============================================ Statistic N Mean St. Dev. Min Max -------------------------------------------- mpg 32 20.091 6.027 10.400 33.900 cyl 32 6.188 1.786 4 8 disp 32 230.722 123.939 71.100 472.000 hp 32 146.688 68.563 52 335 drat 32 3.597 0.535 2.760 4.930 wt 32 3.217 0.978 1.513 5.424 qsec 32 17.849 1.787 14.500 22.900 vs 32 0.438 0.504 0 1 am 32 0.406 0.499 0 1 gear 32 3.688 0.738 3 5 carb 32 2.812 1.615 1 8 -------------------------------------------- 

Or even HTML:

 stargazer(mtcars, type = 'html', out = 'out.html') 

 <table style="text-align:center"><tr><td colspan="6" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left">Statistic</td><td>N</td><td>Mean</td><td>St. Dev.</td><td>Min</td><td>Max</td></tr> <tr><td colspan="6" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left">mpg</td><td>32</td><td>20.091</td><td>6.027</td><td>10.400</td><td>33.900</td></tr> <tr><td style="text-align:left">cyl</td><td>32</td><td>6.188</td><td>1.786</td><td>4</td><td>8</td></tr> <tr><td style="text-align:left">disp</td><td>32</td><td>230.722</td><td>123.939</td><td>71.100</td><td>472.000</td></tr> <tr><td style="text-align:left">hp</td><td>32</td><td>146.688</td><td>68.563</td><td>52</td><td>335</td></tr> <tr><td style="text-align:left">drat</td><td>32</td><td>3.597</td><td>0.535</td><td>2.760</td><td>4.930</td></tr> <tr><td style="text-align:left">wt</td><td>32</td><td>3.217</td><td>0.978</td><td>1.513</td><td>5.424</td></tr> <tr><td style="text-align:left">qsec</td><td>32</td><td>17.849</td><td>1.787</td><td>14.500</td><td>22.900</td></tr> <tr><td style="text-align:left">vs</td><td>32</td><td>0.438</td><td>0.504</td><td>0</td><td>1</td></tr> <tr><td style="text-align:left">am</td><td>32</td><td>0.406</td><td>0.499</td><td>0</td><td>1</td></tr> <tr><td style="text-align:left">gear</td><td>32</td><td>3.688</td><td>0.738</td><td>3</td><td>5</td></tr> <tr><td style="text-align:left">carb</td><td>32</td><td>2.812</td><td>1.615</td><td>1</td><td>8</td></tr> <tr><td colspan="6" style="border-bottom: 1px solid black"></td></tr></table> 
+3
Oct 24 '16 at 17:26
source share

The RStudio IDE provides another good option for printing a data table.

  • Open the data in a viewer, for example. View(data_table) or via the graphical interface
  • Open the view in a separate window (icon in the upper left corner: "Show in new window")
  • A separate dialog box now supports the print dialog (including print preview).

This works in RStudio V0.98.1103 (and possibly later versions)

+2
Aug 12 '15 at 14:37
source share

I came across this question when I wanted to do something similar. I found a mention of the shell command elsewhere in stackoverflow, which was useful in this context:

 sink('myfile.txt') print(mytable,right=F) sink() 
+1
Feb 27 '16 at 21:50
source share

In long / wide tables you can use pander .

It automatically splits long tables into shorter parts that correspond to the page, for example. using knitr paste this snippet into your Rmd file:

 pander::pander(mtcars) 

enter image description here

If you want something more than Excel spreadsheets (even with editing options in html), use rhandsontable . Learn more about using and formatting in vignette . You need to link Rmd in the html file:

 library(rhandsontable) rhandsontable(mtcars, rowHeaders = NULL) 

enter image description here

+1
Jan 03 '17 at 16:34 on
source share

If you want to export as 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 in pdf format, you can do it as follows:

 library(gridExtra) pdf("test.pdf", height=11, width=10) grid.table(df) dev.off() 
0
Jul 27 '17 at 2:04 on
source share



All Articles