pgm1 <- "#ec2c28"
pgm2 <- "#FFFFFF"
pgm3 <- "#1d4fa3"
pgm4 <- "#EAE824"
pgm5 <- "#458544"
pgm6 <- "#060103"
pgm7 <- "#EFA428"
pgm8 <- "#f9bcc5"
pgm9 <- "#1FB8D7"
pgm10 <- "#9A4687"
pgm11 <- "#C2C2C2"
pgm12 <- "#a9d5b5"
pgmwhite <- "#FFFFFF"
pgmblack <- "000000"
pgmyellow <-"#EAE824"
pgmpink <- "#f9bcc5"

barblue <- "#1893e9"
bshadelow <- "#FBFDFE"
bshadeHi <- "#008FD5"
gshadelow <- "#F0F0F0"
gshadehi <- "#CDCDCD"

pgmcolorb <- function(n) {
  case_when(
  n == 1 ~ pgm1,
  n == 2 ~ pgm2,
  n == 3 ~ pgm3,
  n == 4 ~ pgm4,
  n == 5 ~ pgm5,
  n == 6 ~ pgm6,
  n == 7 ~ pgm7,
  n == 8 ~ pgm8,
  n == 9 ~ pgm9,
  n == 10 ~ pgm10,
  n == 11 ~ pgm11,
  n == 12 ~ pgm12,
  n > 12 ~ pgmwhite
  )
}

pgmcolor <- function(n) {
  case_when(
  n == 1 ~ pgmwhite,
  n == 2 ~ pgmblack,
  n == 3 ~ pgmwhite,
  n == 4 ~ pgmblack,
  n == 5 ~ pgmwhite,
  n == 6 ~ pgmyellow,
  n == 7 ~ pgmblack,
  n == 8 ~ pgmblack,
  n == 9 ~ pgmblack,
  n == 10 ~ pgmwhite,
  n == 11 ~ pgmpink,
  n == 12 ~ pgmblack,
  n > 12 ~ pgmblack
  )
}


getPlacing <- function(n) {
  case_when(
  n == 1 ~ "1st",
  n == 2 ~ "2nd",
  n == 3 ~ "3rd",
  n == 4 ~ "4th",
  n == 5 ~ "5th",
  n == 6 ~ "6th",
  n == 7 ~ "7th",
  n == 8 ~ "8th",
  n == 9 ~ "9th",
  n == 10 ~ "10th",
  n == 11 ~ "11th",
  n == 12 ~ "12th",
  n == 13 ~ "13th",
  n > 13 ~ str_c(toString(n),"th",sep='')
  )
}
df <- data.frame(pgm = c(1,2,3,4,5,6,7,8),
        horse = c("Cigar", "Funny Cide", "Animal Kingdom", "Blame", "Zenyatta", "New Years Day", "Northern Dancer", "Beautiful Pleasure"),
        groundloss = c(55,70,85,42,90,45,53,50),
        distanceRun = c(5050,5070,5085,5045,5090,5045,5053,5050),
        ttl = c(50,70,85,42,90,45,53,50),
        fps = c(52.3,51.8,51.9,52.0,53.6,52.9,53.7,53.1),
        finishTime = c(52.3,51.8,51.9,52.0,53.6,52.9,53.7,53.1),
        finish = c(4,7,1,2,5,6,3,8),
        BL = c(0,1,2,6,2,9,6,8)
        )
dr_formatter <- 
  formatter("span", 
            style = x ~ style(
              font.weight = "bold",
              align = "r",
              color = "black"))

color_bar2 <- function (color = "lightgray", fun = "proportion", ...) 
{
  fun <- match.fun(fun)
  formatter(
    "span", 
    style = function(x) style(
      display = "inline-block",
      `font.weight` = "bold",
      align = "right",
      direction = "rtl", `border-radius` = "0px", `padding-right` = "2px", 
     `background-color` = csscolor(color), color = csscolor(barblue),
      width = percent(fun(as.numeric(x), ...))))
}


#df <- mtcars[1:5, 1:4]

print(df)
##   pgm              horse groundloss distanceRun ttl  fps finishTime finish
## 1   1              Cigar         55        5050  50 52.3       52.3      4
## 2   2         Funny Cide         70        5070  70 51.8       51.8      7
## 3   3     Animal Kingdom         85        5085  85 51.9       51.9      1
## 4   4              Blame         42        5045  42 52.0       52.0      2
## 5   5           Zenyatta         90        5090  90 53.6       53.6      5
## 6   6      New Years Day         45        5045  45 52.9       52.9      6
## 7   7    Northern Dancer         53        5053  53 53.7       53.7      3
## 8   8 Beautiful Pleasure         50        5050  50 53.1       53.1      8
##   BL
## 1  0
## 2  1
## 3  2
## 4  6
## 5  2
## 6  9
## 7  6
## 8  8
df %>%
  mutate(
    pgm = cell_spec(pgm, "html", background = map(pgm,pgmcolorb), color = map(pgm,pgmcolor), align = "center"),
    #horse = cell_spec(horse, "html", background = "white", color = "black", align = "left"),
    horse = dr_formatter(horse),
    groundloss = color_bar2(gshadehi)(groundloss),
    distanceRun = dr_formatter(distanceRun),
    ttl = color_bar2(barblue)(ttl),
    fps = color_tile("lightblue", "pink")(fps),
    finish =  cell_spec(getPlacing(finish), "html", background = "white", color = "black", align = "r")
    ) %>%
  rename(`Ground Loss` = groundloss) %>%
  rename(Horse = horse) %>% 
  rename(`#` = pgm) %>%
  rename(` ` = ttl) %>%
  rename(`FPS*` = fps) %>%
  rename(Time = finishTime) %>% 
  rename(Place = finish) %>% 
  rename(Total = distanceRun) %>% 
  kable("html", escape = F) %>%
  kable_styling("hover", full_width = F) %>% 
  column_spec(5, width = "5cm") %>% 
  add_header_above(c(" ", " ", "Distance (in feet)" = 3, "Results" = 4)) %>% 
  add_footnote(c("FPS = Feet for second displayed in seconds"), notation = "symbol")
Distance (in feet)
Results
# Horse Ground Loss Total FPS* Time Place BL
1 Cigar 55 5050 50 52.3 52.3 4th 0
2 Funny Cide 70 5070 70 51.8 51.8 7th 1
3 Animal Kingdom 85 5085 85 51.9 51.9 1st 2
4 Blame 42 5045 42 52.0 52.0 2nd 6
5 Zenyatta 90 5090 90 53.6 53.6 5th 2
6 New Years Day 45 5045 45 52.9 52.9 6th 9
7 Northern Dancer 53 5053 53 53.7 53.7 3rd 6
8 Beautiful Pleasure 50 5050 50 53.1 53.1 8th 8
* FPS = Feet for second displayed in seconds