R provides several libraries to format tabular output. As I have had the problem of finding one that worked well in all occasions and without too much hassle, I would like to compare, from the point of view of aesthetic and ease of use. I will not use any of the options and parameters. Just the basics. The idea for this article started during the redaction of the exploratory analysis for the Capstone project of the Coursera’s Data Science Specialization, where I had problems with most of the packages I have used.
I will take the first 10 rows of the mtcars dataframe and I am going to use several types of tabular output / libraries to display the data in a table output format.
- Simple console output (print)
- knitr/kable output on console (produces better results in Rmd documents)
- htmlTable
- gridExtra
- formattable
The first thing I am going to do is to show how the output of a simple print command renders on a Rstudio console:
1 2 3 4 5 |
# load the data from the datasets package data("mtcars") # Simple Data Frame Print head(mtcars, 10) |
The output produced shall look as follows:
The next option I am going to try is kable, included in the knitr library. I will output in a console as well, but it will render in many formats and it is quite indicated in case of Rmd documents.
1 2 3 4 5 6 7 |
# datasets is already loaded #loading knitr library(knitr) # Here it is just the bare minimal kable(head(mtcars,10)) |
We are in Rstudio Console, let us see how the results look like:
In this case if I had added a caption this would be ignored, but the output looks clearer. Kable outputs nicely in Rmd but it is hard to customize.
Next I am going to look at HtmlTable. This produces a graphical output and it is OK for simple things. However it does not produce consistent output. I had settled on this one, only to be disappointed later.
1 2 3 4 5 6 7 |
# datasets is already loaded #loading htmltable library(htmlTable) # Here I add a caption and vertical bars, and do some alignment htmlTable(head(mtcars,10), caption="Data from 1974 Motor Trend US", align="l|r|r|r|r|r|") |
The output is much better, but still somehow missing something.
One more option, which allows a high degree of customization is the library gridExtra. This seems to work reasonably well in most cases.
1 2 3 4 5 6 7 8 9 |
# datasets is already loaded #loading libraries, not sure if all are needed library(gridExtra) library(grid) library(gtable) # simplest possible use grid.table(head(mtcars,10)) |
The output of this one is rather decent:
The output is nice, the nicest until now and it is graphical. It also allows the production of very sophisticated results. But I found these problems with the library:
- It is slow to draw
- When using it in Rmd and the table is big it gets truncated, this happens also in the plot output view of RStudio when the command is launched from a script or from the console. I had a look at several posts but could not work around it.
The last library I would like to explore is formattable. This allows the production of complex table formats, yet allows the production of reasonable results on the first attempt.
1 2 3 4 5 6 7 |
# datasets is already loaded #loading formattable library(formattable) # simplest possible use formattable(head(mtcars,10)) |
This is the output:
This is really nice. Still you can produce very complex results. Notice that kable in Rmd produces a similar result but its customization options do not scale so well.