I have a shiny app that renders a DT table that has formatted numeric values. The table contains data that is both percentages and data that represents currency. The data is not tidy in that the table is effectively transposed with each row representing a feature and each column being an observation. Ultimately, my goal is to encode logic that anytime a cell has a negative value (the first character of the string is a '-'), the cell is colored red. Here is my reproducible example:
library(tidyverse)
library(DT)
example_df <- data.frame(
x = c("$1", "-2%", "$3"),
y = c("$10", "10%", "$20")
)
determine_cell_color <- function(x) {
if (str_sub(x, 1, 1) == "-") {
return("red")
}
else {
return("white")
}
}
# how to get an individual cell to be a different color
# if first character in string value is '-'
example_df %>%
datatable() %>%
formatStyle(target = 'row', backgroundColor = styleEqual(~determine_cell_color))
