Introducing the {reactablefmtr} Package

data visualization tutorial reactable reactablefmtr

An R package created to make the styling and customization of {reactable} tables easier.

Kyle Cuilla
02-19-2021

Update

The {reactablefmtr} package has undergone many enhancements since the date of this post. For the latest features and customization options available, please visit the package site {reactablefmtr}.

About

The {reactablefmtr} package simplifies and enhances the styling and formatting of tables built with the {reactable} R package. The {reactablefmtr} package provides many conditional formatters that are highly customizable and easy to use.

The {reactablefmtr} package was built using a combination of R, CSS, and HTML in order to allow any level of R user to build highly customizable and stylish tables without having to learn additional programming languages.

For more examples, check out the vignettes. To stay up to date with the latest upgrades to the development version, be sure to follow the package news.

Installation

The {reactablefmtr} package is available from CRAN and can be installed with:

Show code
install.packages("reactablefmtr")

Or install the development version of {reactablefmtr} with:

Show code
remotes::install_github("kcuilla/reactablefmtr")

Examples

Data Bars

Use data_bars() to assign horizontal bars to each row. There are many ways to customize the look of data_bars(), including the alignment of the bars, the position of the text labels, and the option to add icons and images to the bars. See the tutorial for customization examples.

Color Scales

Use color_scales() to assign conditional colors to cells based on their relative values. The color of the text in the cells automatically adjusts based on the shade of the cell color, allowing the use of dark-colored palettes (such as viridis::magma shown below).

Show code
library(reactablefmtr)
library(palmerpenguins)
library(dplyr)
library(viridis)
  
data <- sample_n(penguins, 50) %>% 
  filter(!is.na(bill_length_mm)) %>% 
  select(species, bill_length_mm, bill_depth_mm, flipper_length_mm)

reactable(
  data,
  columns = list(
    bill_length_mm = colDef(style = color_scales(data, colors = viridis::magma(5))),
    bill_depth_mm = colDef(style = color_scales(data, colors = viridis::magma(5))),
    flipper_length_mm = colDef(style = color_scales(data, colors = viridis::magma(5)))
  )
)

By default, colors are conditionally assigned to values within each column, but can also be assigned to row-wise data as shown below. See the tutorial for more examples.

Show code
dimnames <- list(start(nottem)[1]:end(nottem)[1], month.abb)
temps <- matrix(nottem, ncol = 12, byrow = TRUE, dimnames = dimnames)
temps <- as_tibble(temps, rownames = "Year")

reactable(
  temps,
  defaultColDef = colDef(
    style = color_scales(temps, span = TRUE, colors = c("#1e90ff", "#ffffff", "#ff3030")),
    minWidth = 50
  )
)

A similar formatter to color_scales() is color_tiles(). Numbers can be formatted using any formatter from the {scales} package, just like how they are in {ggplot2}. See the tutorial for customization options.

Icon Sets

Use icon_sets() to conditionally assign icons to values from the Font Awesome library based on their relative values. Any number of icons and/or colors can be applied to values within each column. Customization options such as number formatting and positioning of icons are also available. See the tutorial for more options.

Show code
mtcars[1:10,c(1,2,4)] %>% 
reactable(., 
          theme = flatly(),
          defaultColDef = colDef(maxWidth = 150),
          columns = list(
            mpg = colDef(cell = icon_sets(., icons = "gas-pump", colors = c("red","blue","forestgreen"))),
            cyl = colDef(cell = icon_sets(., icons = "car-side", colors = c("red","blue","forestgreen"))),
            hp = colDef(cell = icon_sets(., icons = "horse-head", colors = c("red","blue","forestgreen")))
          )
)

Icon Assign

Use icon_assign() to assign icons to values from the Font Awesome library. Multiple customization options are available, such as bucketing values and the option to show/hide values next to the icons. See the tutorial for more options.

Show code
data <- MASS::Cars93[1:20, c("Make", "Cylinders", "MPG.city", "Price")]

data$Cylinders <- as.numeric(data$Cylinders)

reactable(
  data,
  defaultColDef = colDef(align = "left", maxWidth = 200),
  columns = list(
    Cylinders = colDef(cell = icon_assign(data)),
    MPG.city = colDef(cell = icon_assign(data, icon = "envira", fill_color = "forestgreen", buckets = 5, show_values = "right")),
    Price = colDef(cell = icon_assign(data, icon = "dollar-sign", fill_color = "red", empty_color = "white", buckets = 5, show_values = "right", number_fmt = scales::dollar_format(accuracy = 0.1)))
  )
)

Custom Themes

Within {reactablefmtr}, there are 24+ custom table themes. The themes include bootstrap themes, themes inspired by news/sports sites such as The New York Times, FiveThirtyEight, and ESPN, as well as other custom themes that can only be found within {reactablefmtr}. The themes can be applied easily to tables by simply referencing the theme name. Additional customization options, such as changing the font size, font color, etc. are also available.

Show code
data <- MASS::Cars93[1:20, c("Model", "MPG.city", "MPG.highway")]
        
data %>%
  reactable(.,
    theme = slate(),
    defaultColDef = colDef(
      cell = data_bars(., fill_color = viridis::mako(5), background = "transparent", text_position = "inside-end")
  )
)

Add a Title, Subtitle, and Source

Titles and subtitles can be easily placed above any {reactablefmtr} or {reactable} table with add_title() and add_subtitle(). Also have the option to include a source below a table with add_source(). Additional customization options such as changing the alignment, font size, font family, font style, and font color are available within each formatter.

Show code
reactable(iris[10:29, ]) %>%
  add_title("This is a title") %>% 
  add_subtitle("This is a subtitle") %>% 
  add_source("This is a source")

This is a title

This is a subtitle

This is a source

Save Static or Interactive Tables

{reactablefmtr} or {reactable} tables can be saved directly to a file as a static PNG image or as an interactive HTML file with save_reactable().

Save as a PNG file:

Show code
save_reactable(table_name, "table.png")

Save as an HTML file:

Show code
save_reactable(table_name, "table.html")

If custom CSS styling is applied to the table within an R Markdown document:

Show code
save_reactable("table_name.Rmd", "table.png")

If you prefer to use a pipe:

Show code
table_name %>%
save_reactable("table.png")

Acknowledgments & Contributions

A huge thank you to Greg Lin for creating the amazing {reactable} package! Without Greg, {reactablefmtr} simply would not exist!

Also thank you to June Chao for contributing to the span option in color_scales() and color_tiles()!

Citation

For attribution, please cite this work as

Cuilla (2021, Feb. 19). UNCHARTED DATA: Introducing the {reactablefmtr} Package. Retrieved from https://uncharteddata.netlify.app/posts/2021-03-11-introducing-the-reactablefmtr-package/

BibTeX citation

@misc{cuilla2021introducing,
  author = {Cuilla, Kyle},
  title = {UNCHARTED DATA: Introducing the {reactablefmtr} Package},
  url = {https://uncharteddata.netlify.app/posts/2021-03-11-introducing-the-reactablefmtr-package/},
  year = {2021}
}