1 Style One Column Based on Another Column

By default, formatStyle() uses the values of the column(s) specified by the columns argument to style column(s). You can also style a column conditional on the values of a different column using the valueColumns argument.

library(DT)
options(DT.options = list(pageLength = 5))
df = as.data.frame(cbind(matrix(round(rnorm(50), 3), 10), sample(0:1, 10, TRUE)))
# style V6 based on values of V6
datatable(df) %>% formatStyle(
  'V6',
  backgroundColor = styleEqual(c(0, 1), c('gray', 'yellow'))
)
# style V1 base on values of V6
datatable(df) %>% formatStyle(
  'V1', 'V6',
  backgroundColor = styleEqual(c(0, 1), c('gray', 'yellow'))
)
# hide V6
datatable(df, options = list(
    columnDefs = list(list(targets = 6, visible = FALSE))
)) %>% formatStyle(
  'V1', 'V6',
  backgroundColor = styleEqual(c(0, 1), c('gray', 'yellow'))
)

2 Style A Full Row

You can also style the full row instead of a single cell by using the argument target = 'row' in formatStyle(), e.g.,

datatable(df) %>% formatStyle(
  'V6',
  target = 'row',
  backgroundColor = styleEqual(c(0, 1), c('gray', 'yellow'))
)

3 Style A Full Table

You can also style the full table as a type of heat map by using either styleInterval or styleColorBar, e.g.,

# create 19 breaks and 20 rgb color values ranging from white to red
brks <- quantile(df, probs = seq(.05, .95, .05), na.rm = TRUE)
clrs <- round(seq(255, 40, length.out = length(brks) + 1), 0) %>%
  {paste0("rgb(255,", ., ",", ., ")")}
datatable(df) %>% formatStyle(names(df), backgroundColor = styleInterval(brks, clrs))
# using styleColorBar
datatable(df) %>% formatStyle(names(df),
  background = styleColorBar(range(df), 'lightblue'),
  backgroundSize = '98% 88%',
  backgroundRepeat = 'no-repeat',
  backgroundPosition = 'center')