Introducing the {reactablefmtr} Package

data visualization tutorial reactable reactablefmtr

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

Author

Affiliation

Kyle Cuilla

 

Published

Feb. 19, 2021

Citation

Cuilla, 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)))
  )
)
species
bill_length_mm
bill_depth_mm
flipper_length_mm
Adelie
39
18.7
185
Gentoo
47.5
14.2
209
Gentoo
43.3
14
208
Gentoo
42.8
14.2
209
Chinstrap
52.2
18.8
197
Adelie
41.6
18
192
Chinstrap
50.6
19.4
193
Adelie
41.3
21.1
195
Gentoo
46.4
15
216
Adelie
43.1
19.2
197
1–10 of 50 rows

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
  )
)
Year
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
Nov
Dec
1920
40.6
40.8
44.4
46.7
54.1
58.5
57.7
56.4
54.3
50.5
42.9
39.8
1921
44.2
39.8
45.1
47
54.1
58.7
66.3
59.9
57
54.2
39.7
42.8
1922
37.5
38.7
39.5
42.1
55.7
57.8
56.8
54.3
54.3
47.1
41.8
41.7
1923
41.8
40.1
42.9
45.8
49.2
52.7
64.2
59.6
54.4
49.2
36.3
37.6
1924
39.3
37.5
38.3
45.5
53.2
57.7
60.8
58.2
56.4
49.8
44.4
43.6
1925
40
40.5
40.8
45.1
53.8
59.4
63.5
61
53
50
38.1
36.3
1926
39.2
43.4
43.4
48.9
50.6
56.8
62.5
62
57.5
46.7
41.6
39.8
1927
39.4
38.5
45.3
47.1
51.7
55
60.4
60.5
54.7
50.3
42.3
35.2
1928
40.8
41.1
42.8
47.3
50.9
56.4
62.2
60.5
55.4
50.2
43
37.3
1929
34.8
31.3
41
43.9
53.1
56.9
62.5
60.3
59.8
49.2
42.9
41.9
1–10 of 20 rows

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")))
          )
)
mpg
cyl
hp
Mazda RX4
21
6
110
Mazda RX4 Wag
21
6
110
Datsun 710
22.8
4
93
Hornet 4 Drive
21.4
6
110
Hornet Sportabout
18.7
8
175
Valiant
18.1
6
105
Duster 360
14.3
8
245
Merc 240D
24.4
4
62
Merc 230
22.8
4
95
Merc 280
19.2
6
123

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)))
  )
)
Make
Cylinders
MPG.city
Price
Acura Integra
25
$15.9
Acura Legend
18
$33.9
Audi 90
20
$29.1
Audi 100
19
$37.7
BMW 535i
22
$30.0
Buick Century
22
$15.7
Buick LeSabre
19
$20.8
Buick Roadmaster
16
$23.7
Buick Riviera
19
$26.3
Cadillac DeVille
16
$34.7
1–10 of 20 rows

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")
  )
)
Model
MPG.city
MPG.highway
Integra
25
31
Legend
18
25
90
20
26
100
19
26
535i
22
30
Century
22
31
LeSabre
19
28
Roadmaster
16
25
Riviera
19
27
DeVille
16
25
1–10 of 20 rows

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

Sepal.Length
Sepal.Width
Petal.Length
Petal.Width
Species
4.9
3.1
1.5
0.1
setosa
5.4
3.7
1.5
0.2
setosa
4.8
3.4
1.6
0.2
setosa
4.8
3
1.4
0.1
setosa
4.3
3
1.1
0.1
setosa
5.8
4
1.2
0.2
setosa
5.7
4.4
1.5
0.4
setosa
5.4
3.9
1.3
0.4
setosa
5.1
3.5
1.4
0.3
setosa
5.7
3.8
1.7
0.3
setosa
1–10 of 20 rows

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()!

Footnotes

    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}
    }