formattable icon indicating copy to clipboard operation
formattable copied to clipboard

Custom Scale for min max on color_tile

Open mastoltejr opened this issue 6 years ago • 2 comments

Hello, I have a column of numbers c(5474, 6284, 6585, 3877, etc) and I want to use the color_tile function to color them proportionate between the range of c(3000, 7000) rather than the default which is used c(min(x) = 3877, max(x) = 6585. Is there any way to do this? Thanks

mastoltejr avatar Dec 18 '18 16:12 mastoltejr

You can do something like this:

library(formattable)

customRange = c(-50, 35) # custom min / max values
colors      = csscolor(gradient(as.numeric(c(customRange, mtcars$mpg)), "white", "pink"))
colors      = colors[-(1:2)] ## remove colors for min/max

fmt    = formatter("span", 
                   style = function(x){
                     style(display            = "block",
                           padding            = "0 4px",
                           `border-radius`    = "4px",
                           `background-color` = colors
                           )})

formattable(mtcars, list(mpg = fmt))

Atrebas avatar Dec 27 '18 09:12 Atrebas

@mastoltejr - does @Atrebas solution work for your use case?

monkeywithacupcake avatar Oct 14 '21 23:10 monkeywithacupcake