Color cells in DT::dataTable in Shiny

So.. I can't run your code because it uses variables that aren't defined. But my guess is that the formatStyles are in the wrong place, applying to the list of options rather than the datatable. Check your parentheses.

Check out the DT docs for examples on how to conditionally style table cells:

Here's one example I've lifted straight from the docs. For a value x, cell color is

  • red if x <= 3.4
  • white if 3.4 < x <= 3.8
  • green if x > 3.8
library(DT)

datatable(iris) %>% formatStyle(
  'Sepal.Width',
  backgroundColor = styleInterval(c(3.4, 3.8), c('red', 'white', 'green')),
  fontWeight = 'bold'
)

and btw, you can format code like this by wrapping it in backticks. Triple backticks for a code block.

1 Like