Updated March 12, 2022 with info on the reactablefmtr and gtextras packages.
It doesn’t matter how good your data analysis is if you don’t effectively communicate your results. In most reports, communication of results happens through a combination of data visualization and tables. Along with narrative text, these make up the bulk of content used to communicate results.
Clear writing is a topic for another venue. Data visualization in R is a huge topic (and one covered expertly in Kieran Healy’s Data Visualization: A Practical Introduction and Claus Wilke’s Fundamentals of Data Visualization). But what about tables? Fortunately for R users, there are many ways to create beautiful tables that effectively communicate your results.
If you are using RMarkdown (and, if you’re not, you should really consider it), any data frame that you call in a code chunk is displayed using the data frame printing method set in your YAML. As Yihui Xie puts it, “printing objects in R code chunks basically emulates the R console.” This default output leaves much to be desired. “Table” output with fixed-width fonts like this never inspired anyone to take action.
It is quite easy to improve this output. You can change the default data frame print method in the YAML to one of the following options.
Of these, the one most likely to improve your table output is , which creates paged tables that work well when you have particularly long tables and want to give users the ability to look at pieces of the output:
There are also many packages that provide functions to produce nicely formatted tables. Here are some of the packages I have used that produce tables that look good and effectively communicate results.
gt is a new(ish) package. It “is designed to be both straightforward yet powerful … [with] an emphasis is on simple functions for the everyday display table needs.” The package has functions designed to do things like:
The gt package provides a general philosophy of tables (similar to the grammar of graphics theory that underlies the package) that enables it to be easy to use (with some practice) and extremely flexible.
There’s also a package called that provides add-ons for the package. This package, by Thomas Mock, provides some nice themes like this modeled on New York Times tables.
The package also lets you add visualizations into tables, including sparklines and bar plots.
The kableExtra package builds on the kable output from the knitr package. As author Hao Zhu puts it:
Sharla Gelfand used the kableExtra package in a report she did for the College of Nurses of Ontario and loved it.
Kun Ren’s formattable package “is designed for applying formatting on vectors and data frames to make data presentation easier, richer, more flexible and hopefully convey more information.”
One of the most unique features is the ability to add colors, background shading, bars, and other features that move tables beyond hulking blocks of text.
If you’re looking for an example of the formattable package in use, Laura Ellis has a really nice walkthrough.
The main appeal of the DT package is its ability to add filtering, pagination, and sorting to HTML tables. Users can slice, scroll through, and arrange tables in ways that help them to make sense of your results.
I had not heard of this package until Mara Averick tweeted about it recently. Though I haven’t used it, the docs look incredible. Like the DT package, it offers the ability to sort and filter data. It’s also much more visually appealing than the DT package. One of the examples (below) looks exactly like the gorgeous tables made by FiveThirtyEight.
The package can be a bit more challenging to work with. To simplify the process of creating tables in , Kyle Cuilla developed the package. As Kyle puts it:
Among other things, the package makes it easier to conditionally add colors to tables, add interactive sparklines, use custom themes, embed images in tables, and save tables in PNG and HTML format.
The above packages are designed to produce HTML tables. If you are knitting to Word, your best for tables is the flextable package (though the tables this package produces can also be embedded within HTML and PDF outputs). Designed by David Gohel, the flextable package provides a straightforward syntax to shape the content and format of your tables.
The above packages are ones that I’ve used and had good success with. When I put the call out on Twitter for other packages to make tables in R, I got some promising-looking responses!
Like the DT package, the rhandsontable package gives users the ability to manipulate data in tables. It comes with “powerful features like data validation, sorting, grouping, data binding, formula support or column ordering.” Working with tables feels like viewing data in Excel, which can be helpful for users used to working with Microsoft’s ubiquitous spreadsheet program. Users can even make changes to data in tables made with this package.
The fantastically-named pixedust package is designed to produce a specific type of table: model output that has been tidied using the broom package. Using pixiedust is a three-step process:
The functions enable you to do things like removing excess decimal places, convert p-values to not use scientific notation, and more.
If you want to ensure that end users read and understand the results of your complex statistical analyses, this looks like a great package!
There are surely more table packages that I’ve missed (in wrapping up this post, I came across this list from the RStudio RMarkdown tutorials), but this should give you a good place to start in making your tables more beautiful and more effective.