News in htmlTable 2.0

A short intro to the new features in htmlTable 2.0. The image is a blend based on a CC image by Ken Xu.

The htmlTable 2.0 package was just released on CRAN! It is my most downloaded package with 160 000+ downloads/month and this update is something that I have been wanting to do for a long time. For those of you that never encountered htmlTable it is a package that takes a matrix/data.frame and outputs a nicely formatted HTML table. When I created the package there weren’t that many alternatives and knitr was this new thing that everyone was excited about, magrittr with its ubiquitous %>% pipe had not even entered the scene. The current update should make it easier to streamline table look, separate layout from content and use tidyverse functionality.

Style and theming

The biggest change in how to use the htmlTable is that you have a separate function for adding style to the table. The change is implemented in a non-breaking fashion, i.e. all the old options should work just as before but the new API is encouraged as it simplifies a lot. The new API changes so that instead of all the css.* arguments we now have a separate function that applies these, addHtmlTableStyle and passes them on as an attribute to the htmlTable:

As we usually want the same layout for the entire table we can accomplish the same effect for all of our tables using setHtmlTableTheme and the style will be applied to all your tables.

There is an option for selecting themes with predefined layouts. In addition to the standard which has the traditional htmlTable look, you also have Google docs and blank. The Google docs is still a work in progress and any help making it as compatible as possible with Google’s Drive document when copy-pasting is much appreciated.

Using tidyverse syntax in tidyHtmlTable

In 2017, Stephen Gragg added the tidyHtmlTable to the package. Since then advances to RStudio has impacted in how we use R and it became obvious that the function should instead of strings as arguments directly accept column names just as defined by tidyselect. The tidyHtmlTable solves one of htmlTable‘s greates weaknesses, the need for calculating the rgroup, tspanner, cgroup arguments and using it with the tidyselect interface is now pure joy:

A more advanced example on how tidyHtmlTable works with tidyverse we can have a look at the example in the vignette("tidyHtmlTable"):

When using tidyHtmlTable you can decouple it from htmlTable and provide any table function by supplying the table_fn function.


In htmlTable 2.0 there are plenty of options that have been added. Most of them should start with the prefix “htmlTable.”, e.g “htmlTable.css.tspanner.sep”. While the prefix is useful for reducing the risk of conflicting options between packages, options such as “table_counter” are unchanged in order to avoid unnecessary breaking changes.

NEWS for 2.0

  • Added theming and styling with addHtmlTableStyle and setHtmlTableTheme to reduce the cognitive burden of finding the right option within the docs. Note: this may impact your current tables and hence the major version (2.0.0).
  • Changed so that css.cell is properly applied to rownames, cell fillers and the actual cells of interest (may impact the final layout!)
  • Breaking change tidyHtmlTable: Moved to a fully tidyverse compatible system with tidyHtmlTable. This is a breaking change to the API as we switch from columns as strings to tidyselect syntax and as gather/spread have been replaced by pivot_longer/pivot_wider the default values have been updated in accordance with their defaults, e.g. rnames = "name" and value = "value".
  • Breaking change tidyHtmlTable: Sorting of rows is skipped as we may have situations with repeating inputs and this can easily be performed pre-function by calling dplyr::arrange. This has furthermore the desirable feature that any custom sorting is retained.
  • Added mso-number-format to help (Issue #63) – thanks Rasmus Hertzum
  • txtRound can now add txtInt when formatting the integer section for easier readability
  • Added htmlTable css options – they should all start with htmlTable.
  • pos.caption now uses match.arg as expected
  • Fixed proper S3 function definition for htmlTable with all the arguments
  • Added htmlTable.css.border style option for allowing to choose border style. Also fixed bug with cgroup empty cells and vertical border.
  • Added htmlTable.pretty_indentation option for skipping the stripping of all the tabs that was required due to old Pandoc bug.
  • Added attr(x, "html") <- TRUE by default and UTF-8 encoding on all outputted strings to mimic the htmltools::HTML function behavior.
  • For simple tibble output the tidyHtmlTable can now be used to choose a column for the rnames argument
  • The print statement now respects the chunk_output_type in Rmd files in RStudio

Flattr this!

This entry was posted in R. Bookmark the permalink.

4 Responses to News in htmlTable 2.0

  1. JOHN BORESTA says:

    I copied the above code but R keeps telling me it can’t find addHtmlTableStyle

  2. Jason Dobkins says:


    I want to let you know that I found a broken link on this page:

    It’s a link to a page on

    I thought you might want to remove or replace it.


Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.