Quicker knitr kables in RStudio notebook

R-Markdown knitr R-Studio

In this post a simple RStudio hack is presented on how to display tables produced via knitr kable efficiently in the RStudio session.

The setup

The RStudio notebook is a great interactive tool to build a statistical report. Being able to see statistics and graphs right on the fly probably has saved me countless hours, especially when building complex reports.

However, one thing that has always bothered me was the way tables are displayed in the notebook with knitr’s kable function. For example, consider the airquality data set:

head(airquality)
  Ozone Solar.R Wind Temp Month Day
1    41     190  7.4   67     5   1
2    36     118  8.0   72     5   2
3    12     149 12.6   74     5   3
4    18     313 11.5   62     5   4
5    NA      NA 14.3   56     5   5
6    28      NA 14.9   66     5   6

To get a nice table in your report you type

knitr::kable(head(airquality), caption = "New York Air Quality Measurements.")

which shows up nicely formatted in the final output

Table 1: New York Air Quality Measurements.
Ozone Solar.R Wind Temp Month Day
41 190 7.4 67 5 1
36 118 8.0 72 5 2
12 149 12.6 74 5 3
18 313 11.5 62 5 4
NA NA 14.3 56 5 5
28 NA 14.9 66 5 6


The problem

But in the interactive RStudio notebook session the table looks something like the following:

So first of all, the formatting is not that great. Secondly, the table chunk consumes way too much space of the notebook and, at times, can be very cumbersome to scroll. Also for bigger tables (and depending on your hardware) it can take up to a few seconds for the table to be built.

So often when I was using kable, I felt my workflow being disrupted. In the interactive session I want a table being built quickly and in a clean format. Now, using the simple print function you’ll get exactly this

So my initial quick-and-dirty workaround during the interactive session was to comment out the knitr statement and use the print function.

#knitr::kable(head(airquality), caption = "New York Air Quality Measurements.")
print(head(airquality))

Then, only when creating the final report, I would comment out the print function and use kable again. Of course, there is a much more elegant and easier solution to get this without having to switch between functions.

The solution

We define a simple wrapper, which chooses the corresponding function depending on the context:

kable_if <- function(x, ...) if (interactive()) print(x, ...) else knitr::kable(x, ...)

Then you simply call it as you would invoke kable and now you get both, the quick table in the interactive session …

… and a formatted table in the report.

kable_if(head(airquality), caption = "New York Air Quality Measurements.")
Table 2: New York Air Quality Measurements.
Ozone Solar.R Wind Temp Month Day
41 190 7.4 67 5 1
36 118 8.0 72 5 2
12 149 12.6 74 5 3
18 313 11.5 62 5 4
NA NA 14.3 56 5 5
28 NA 14.9 66 5 6

That’s it. Simply put this function definition somewhere in the top of your document and enjoy a quick workflow.

Reuse

Text and figures are licensed under Creative Commons Attribution CC BY-SA 4.0. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".