reactablefmtr icon indicating copy to clipboard operation
reactablefmtr copied to clipboard

conditional formatting color in color_tiles

Open mguller opened this issue 2 years ago • 3 comments

Hi,

Is it possible to define color based on condition in color_tiles? For example, I would like to apply color_tiles only if the value is lower than 0 I created a function "f_stylefunc" to assign color, it is just returning a color if it is lower than 1.5, and "white" if it is higher than 1.5. But I could not find how to assign value inside function. Thank you in advance for your help

` data <- iris[10:29, ]

f_colorpal <- function(value){ if (value > 1.5) { color <- "#FFFFFF" } else { color <- "#E75400" } } reactable(data, columns = list( Petal.Length = colDef(cell= color_tiles(data, colors = f_colorpal)) ) ) `

mguller avatar Nov 03 '21 13:11 mguller

Yes, you can utilize conditional colors by creating the condition in another column and then referencing that column with color_ref.

data <- iris[10:29, ]

data <- data %>% mutate(f_colorpal = case_when( Petal.Length > 1.5 ~ "#FFFFFF", TRUE ~ "#E75400" ))

reactable(data, columns = list( f_colorpal = colDef(show=FALSE), Petal.Length = colDef(cell= color_tiles(data, color_ref = "f_colorpal")) ) )

image

kcuilla avatar Nov 03 '21 14:11 kcuilla

Hi, Thank you for the answer. If I need to do only for one column, then this solution helps. But I have 25 column which I need to apply same condition. If value less than 0, I want to use color, otherwise keep cell with white color. I guess it would not be a proper solution to create additional column for each column to define color I already found a solution in reactable with "background color", but visuaization with color_tiles function is more attractive

Thank you again for this great package and maybe there is a solution for my issue.

mguller avatar Nov 04 '21 07:11 mguller