Is it possible to use more than 2 colors in the color_tile function?

前端 未结 5 939
青春惊慌失措
青春惊慌失措 2020-12-12 01:29

I have a dataframe column that I\'m currently formatting using the formattable::color_tile function (below):

color_tile( \"red\", \"springgreen\"

5条回答
  •  渐次进展
    2020-12-12 01:59

    It doesn't look like the function is designed to handle more than two colors, but you can make your own building on that template.

    color_tile2 <- function (...) {
      formatter("span", style = function(x) {
        style(display = "block",
              padding = "0 4px", 
              `border-radius` = "4px", 
              `background-color` = csscolor(matrix(as.integer(colorRamp(...)(normalize(as.numeric(x)))), 
                                                   byrow=TRUE, dimnames=list(c("red","green","blue"), NULL), nrow=3)))
      })}
    

    which can be used like

    formattable(mtcars, list(mpg = color_tile2(c("white", "pink"))))
    formattable(mtcars, list(mpg = color_tile2(c("blue", "green", "pink"))))
    

提交回复
热议问题