Tabular output in R

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.

The first thing I am going to do is to show how the output of a simple print command renders on a Rstudio console:

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.

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.

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.

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:

  1. It is slow to draw
  2. 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.

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.