Renkun has created a great R package formattable.

That helps to create nice HTML tables like below.

df <- data.frame(
  id = 1:10,
  name = c("Bob", "Ashley", "James", "David", "Jenny", 
           "Hans", "Leo", "John", "Emily", "Lee"), 
  age = c(28, 27, 30, 28, 29, 29, 27, 27, 31, 30),
  grade = c("C", "A", "A", "C", "B", "B", "B", "A", "C", "C"),
  test1_score = c(8.9, 9.5, 9.6, 8.9, 9.1, 9.3, 9.3, 9.9, 8.5, 8.6),
  test2_score = c(9.1, 9.1, 9.2, 9.1, 8.9, 8.5, 9.2, 9.3, 9.1, 8.8),
  final_score = c(9, 9.3, 9.4, 9, 9, 8.9, 9.25, 9.6, 8.8, 8.7),
  registered = c(TRUE, FALSE, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE),
  stringsAsFactors = FALSE)

library(formattable)

score_colorizer <- formatter("span", 
                             style = function(x) style(
                               display = "block", 
                               color = "white", 
                               "border-radius" = "4px",
                               "padding-right" = "4px",
                               background = rgb(0.2 + 0.8 * rank(-x) / length(x), 0.6, 0)))


formattable(df, list(
  grade = formatter("span",
                    style = function(x) 
                      ifelse(x == "A", style(color = "green", "font-weight" = "bold"), NA)),
  test1_score = score_colorizer,
  test2_score = score_colorizer,
  final_score = formatter("span",
                          style = function(x) style(color = ifelse(rank(-x) <= 3, "green", "gray")),
                          function(x) sprintf("%.2f (ranking %02d)", x, rank(-x))),
  registered = function(x) ifelse(x, "yes", "no")
))
id name age grade test1_score test2_score final_score registered
1 Bob 28 C 8.9 9.1 9.00 (ranking 06) yes
2 Ashley 27 A 9.5 9.1 9.30 (ranking 03) no
3 James 30 A 9.6 9.2 9.40 (ranking 02) yes
4 David 28 C 8.9 9.1 9.00 (ranking 06) no
5 Jenny 29 B 9.1 8.9 9.00 (ranking 06) yes
6 Hans 29 B 9.3 8.5 8.90 (ranking 08) yes
7 Leo 27 B 9.3 9.2 9.25 (ranking 04) yes
8 John 27 A 9.9 9.3 9.60 (ranking 01) no
9 Emily 31 C 8.5 9.1 8.80 (ranking 09) no
10 Lee 30 C 8.6 8.8 8.70 (ranking 10) no

Because I want to create some tables colorized like Excel, I’ve written a code below.

score_colorizer_excel <- formatter("span", 
                             style = function(x) style(
                               display = "block", 
                               color = "black", 
                               "border-radius" = "0px",
                               "padding-right" = "4px",
                               background = rgb(1, 
                                                (max(x) - x)/diff(range(x)) * (239 - 113) / 255 + 113/255, 
                                                (max(x) - x)/diff(range(x)) * (156 - 40) / 255 + 40/255)))

formattable(df, list(
  grade = formatter("span",
                    style = function(x) 
                      ifelse(x == "A", style(color = "green", "font-weight" = "bold"), NA)),
  test1_score = score_colorizer_excel,
  test2_score = score_colorizer_excel,
  final_score = formatter("span",
                          style = function(x) style(color = ifelse(rank(-x) <= 3, "green", "gray")),
                          function(x) sprintf("%.2f (ranking %02d)", x, rank(-x))),
  registered = function(x) ifelse(x, "yes", "no")
))
id name age grade test1_score test2_score final_score registered
1 Bob 28 C 8.9 9.1 9.00 (ranking 06) yes
2 Ashley 27 A 9.5 9.1 9.30 (ranking 03) no
3 James 30 A 9.6 9.2 9.40 (ranking 02) yes
4 David 28 C 8.9 9.1 9.00 (ranking 06) no
5 Jenny 29 B 9.1 8.9 9.00 (ranking 06) yes
6 Hans 29 B 9.3 8.5 8.90 (ranking 08) yes
7 Leo 27 B 9.3 9.2 9.25 (ranking 04) yes
8 John 27 A 9.9 9.3 9.60 (ranking 01) no
9 Emily 31 C 8.5 9.1 8.80 (ranking 09) no
10 Lee 30 C 8.6 8.8 8.70 (ranking 10) no

The real colorized Excel table is below.

The real colorized Excel table

Enjoy!