Something like this might work, leveraging RColorBrewer
color_tile3 <- function(fun = "comma", digits = 0, palette = 'RdBu', n = 9) {
fun <- match.fun(fun)
stopifnot(n >= 5)
return_cut <- function(y)
cut(y, breaks = quantile(y, probs = 0:n/n, na.rm = T),
labels = 1:n, ordered_result = T, include.lowest = T)
return_col <- function(y)
RColorBrewer::brewer.pal(n, palette)[as.integer(return_cut(y))]
formatter("span", x ~ fun(x, digits = digits),
style = function(y) style(
display = "block",
padding = "0 4px",
"border-radius" = "4px",
"color" = ifelse( return_cut(y) %in% c(1, 2, n-1, n),
csscolor("white"), csscolor("black")),
"background-color" = return_col(y)
)
)
}
Use case:
library(tidyverse)
library(RColorBrewer)
mtcars[, 1:5] %>%
corrr::correlate() %>%
formattable(., list(
`rowname` = formatter("span", style = ~ style(color = "grey",
font.weight = "bold")),
area(col = 2:6) ~ color_tile3(digits = 2)))
Output:
Here's a link to the output mtcars_color3 which looks like this:
