> if (!require("DT")) install.packages('DT')
> library(DT)
> library(tibble)
>
> mtcars <- rownames_to_column(mtcars, "carNames")
Background color to ‘cornflowerblue’ when cyl == 6.
> datatable(mtcars) %>%
+ formatStyle('cyl',
+ target='row',
+ backgroundColor = styleEqual('6','cornflowerblue'))
Multiple conditions:
> datatable(mtcars) %>%
+ formatStyle('cyl',
+ target='row',
+ backgroundColor = styleEqual(c('6','4','8'),
+ c('cornflowerblue','cyan','darkgrey')))
Interval conditions:
Color fonts based on conditions on hp to - blue <= 110
110 < yellow <= 175
175 < red <= 325
green > 325
> datatable(mtcars) %>%
+ formatStyle('hp',
+ target='row',
+ color = 'white',
+ backgroundColor = styleInterval(c(110,175,325),
+ c('blue','yellow','red','green')))
Font color to red when carNames == “Valiant”.
> datatable(mtcars) %>%
+ formatStyle('carNames',
+ target='row',
+ color = styleEqual(c('Valiant'), c('red')))
Multiple conditions:
> datatable(mtcars) %>%
+ formatStyle('carNames',
+ target='row',
+ color = styleEqual(c('Valiant','Duster 360'),
+ c('red','blue')))
Interval conditions:
Color fonts based on conditions on hp to - blue <= 110
110 < yellow <= 175
175 < red <= 325
green > 325
> datatable(mtcars) %>%
+ formatStyle('hp',
+ target='row',
+ color = styleInterval(c(110,175,325),
+ c('blue','yellow','red','green')))