5 Jan 2018

Beautiful HTML tables in R with tableHTML



A couple of years ago while I was developing a football app using shiny, I found it somewhat difficult to build a pretty HTML table exactly the way I wanted. For example, drawing vertical lines every three columns seemed like a difficult task back then even using great packages like xtable. This provided the motivation to team up with Clemens and develop tableHTML.

tableHTML is used to make CSS-ible HTML tables in an intuitive way. Those tables can be used in shiny applications, Rmarkdown documents (like this one!) or any other application that accepts HTML.

Let’s see a few of its main use cases.

A simple HTML table

The main tableHTML function creates a blank HTML table on which CSS can be applied afterwards with the add_css functions. The package uses the pipe operator %>% coming from magrittr to join functions together.

library(tableHTML)
mtcars[1:10, ] %>%
  tableHTML(widths = c(140, rep(50, 11)))
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21 6 160 110 3.9 2.62 16.46 0 1 4 4
Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.32 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.44 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.46 20.22 1 0 3 1
Duster 360 14.3 8 360 245 3.21 3.57 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.19 20 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.15 22.9 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.44 18.3 1 0 4 4

A scientific HTML table

tableHTML provides two themes (rshiny-blue and scientific) that can be used directly with the tableHTML function.

library(tableHTML)
mtcars[1:10, ] %>%
  tableHTML(widths = c(140, rep(50, 11)),
            theme = 'scientific')
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21 6 160 110 3.9 2.62 16.46 0 1 4 4
Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.32 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.44 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.46 20.22 1 0 3 1
Duster 360 14.3 8 360 245 3.21 3.57 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.19 20 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.15 22.9 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.44 18.3 1 0 4 4

Features of tableHTML

tableHTML allows the user to extend the table with:

  • New headers
  • Second headers
  • Row names
  • Row groups
  • A caption
  • A footer

All these can be specified in tableHTML’s arguments.

mtcars[1:15, ] %>%
  tableHTML(widths = c(120, 140, rep(45, 11)),
            second_headers = list(c(2, 2, 4, 5), c('', 'team1', 'team2', 'team3')),
            row_groups = list(c(7, 8), c('Group1', 'Group2')),
            caption = 'This is a caption',
            footer = 'This is a footer')
This is a caption This is a footer
team1 team2 team3
mpg cyl disp hp drat wt qsec vs am gear carb
Group1 Mazda RX4 21 6 160 110 3.9 2.62 16.46 0 1 4 4
Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.32 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.44 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.46 20.22 1 0 3 1
Duster 360 14.3 8 360 245 3.21 3.57 15.84 0 0 3 4
Group2 Merc 240D 24.4 4 146.7 62 3.69 3.19 20 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.15 22.9 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.44 18.3 1 0 4 4
Merc 280C 17.8 6 167.6 123 3.92 3.44 18.9 1 0 4 4
Merc 450SE 16.4 8 275.8 180 3.07 4.07 17.4 0 0 3 3
Merc 450SL 17.3 8 275.8 180 3.07 3.73 17.6 0 0 3 3
Merc 450SLC 15.2 8 275.8 180 3.07 3.78 18 0 0 3 3
Cadillac Fleetwood 10.4 8 472 205 2.93 5.25 17.98 0 0 3 4

Beautify with CSS

Currently the package offers the following add_css functions which add CSS to their respective part of the HTML table:

  • add_css_caption
  • add_css_column
  • add_css_conditional_column
  • add_css_footer
  • add_css_header
  • add_css_row
  • add_css_second_header
  • add_css_table
  • add_css_tbody
  • add_css_thead

This is an example of how some of the add_css functions can be used.

mtcars[1:15, ] %>%
  tableHTML(widths = c(140, rep(45, 11)),
            second_headers = list(c(3, 4, 5), c('team1', 'team2', 'team3')),
            caption = 'Table of Cars',
            footer = 'Figure 1. Stats for famous cars') %>% 
  add_css_second_header(css = list(c('height', 'background-color', 'font-size'), 
                                   c('40px', ' #e6e6e6', '30px')),
                        second_headers = 1:3) %>%
  add_css_header(css = list(c('height', 'background-color'), c('30px', ' #e6e6e6')),
                 headers = 1:12) %>%
  add_css_row(css = list('background-color', '#f2f2f2'),
              rows = even(1:17)) %>%
  add_css_row(css = list('background-color', '#e6f0ff'),
              rows = odd(1:17)) %>%
  add_css_column(css = list('text-align', 'center'), 
                 columns = names(mtcars)) %>%
  add_css_caption(css = list(c('text-align', 'font-size', 'color'), c('center', '20px', 'black'))) %>%
  add_css_footer(css = list(c('text-align', 'color'), c('left', 'black')))
Table of Cars Figure 1. Stats for famous cars
team1 team2 team3
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21 6 160 110 3.9 2.62 16.46 0 1 4 4
Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.32 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.44 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.46 20.22 1 0 3 1
Duster 360 14.3 8 360 245 3.21 3.57 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.19 20 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.15 22.9 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.44 18.3 1 0 4 4
Merc 280C 17.8 6 167.6 123 3.92 3.44 18.9 1 0 4 4
Merc 450SE 16.4 8 275.8 180 3.07 4.07 17.4 0 0 3 3
Merc 450SL 17.3 8 275.8 180 3.07 3.73 17.6 0 0 3 3
Merc 450SLC 15.2 8 275.8 180 3.07 3.78 18 0 0 3 3
Cadillac Fleetwood 10.4 8 472 205 2.93 5.25 17.98 0 0 3 4

Conditional Formatting

Another special feature of the package is conditional formatting. You can see an example below using add_css_conditional_column.

mtcars[1:15, ] %>%
  tableHTML(widths = c(140, rep(45, 11))) %>%
  add_css_conditional_column(conditional = "==",
                             value = 14.3, 
                             css = list('background-color', "steelblue"), 
                             columns = 1) %>%
  add_css_conditional_column(conditional = "!=", 
                             value = 8,
                             css = list('background-color', "mediumvioletred"), 
                             columns = 2) %>%
  add_css_conditional_column(conditional = ">",
                             value = 440,
                             css = list('background-color', "orange"),
                             columns = 3) %>%
  add_css_conditional_column(conditional = ">=", 
                             value = 264,
                             css = list('background-color', "green"),
                             columns = 4) %>%
  add_css_conditional_column(conditional = "<",
                             value = 3, 
                             css = list('background-color', "yellow"),
                             columns = 5) %>%
  add_css_conditional_column(conditional = "<=", 
                             value = 2.20, 
                             css = list('background-color', "lightgray"), 
                             columns = 6)
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21 6 160 110 3.9 2.62 16.46 0 1 4 4
Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.32 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.44 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.46 20.22 1 0 3 1
Duster 360 14.3 8 360 245 3.21 3.57 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.19 20 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.15 22.9 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.44 18.3 1 0 4 4
Merc 280C 17.8 6 167.6 123 3.92 3.44 18.9 1 0 4 4
Merc 450SE 16.4 8 275.8 180 3.07 4.07 17.4 0 0 3 3
Merc 450SL 17.3 8 275.8 180 3.07 3.73 17.6 0 0 3 3
Merc 450SLC 15.2 8 275.8 180 3.07 3.78 18 0 0 3 3
Cadillac Fleetwood 10.4 8 472 205 2.93 5.25 17.98 0 0 3 4

Colour ranking

Various colour rank themes (including custom) are also available.

mtcars[1:15, ] %>%
  tableHTML(widths = c(140, rep(45, 11))) %>%
  add_css_conditional_column(colour_rank_theme = "RAG", columns = 1) %>%
  add_css_conditional_column(colour_rank_theme = "Spectral", columns = 2) %>%
  add_css_conditional_column(colour_rank_theme = "Rainbow", columns = 3) %>%
  add_css_conditional_column(colour_rank_theme = "White-Green", columns = 4) %>%
  add_css_conditional_column(colour_rank_theme = "White-Blue", columns = 5) %>%
  add_css_conditional_column(colour_rank_theme = "White-Red", columns = 6) 
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21 6 160 110 3.9 2.62 16.46 0 1 4 4
Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.32 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.44 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.46 20.22 1 0 3 1
Duster 360 14.3 8 360 245 3.21 3.57 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.19 20 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.15 22.9 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.44 18.3 1 0 4 4
Merc 280C 17.8 6 167.6 123 3.92 3.44 18.9 1 0 4 4
Merc 450SE 16.4 8 275.8 180 3.07 4.07 17.4 0 0 3 3
Merc 450SL 17.3 8 275.8 180 3.07 3.73 17.6 0 0 3 3
Merc 450SLC 15.2 8 275.8 180 3.07 3.78 18 0 0 3 3
Cadillac Fleetwood 10.4 8 472 205 2.93 5.25 17.98 0 0 3 4

More on tableHTML

It would be impossible to fit all the package’s uses on just one post, so I will keep it short here. There are a number of resources / vignettes to have a look at if you would like to find out more about its uses:

Thanks for reading and as always, if you find any bugs please report them on the issues page on github.

 


Tags:
Stats:
0 comments