0

I am writing a Shiny app that has a data table. I am looking to color code rows based on the values of two columns. Even there are values in the 30 Day column that are greater than the Overall column, all values are black. I have tried a for loop as well, but this did not work either. Is there a way to compare two columns in a data table and make a style change based on the results of the comparison?

setDT(a)
a[,group_i := as.Date(DATE) >= Sys.Date()-30]
output$tab<-DT::renderDataTable({
  dt<-datatable(a[, .(
  "Overall Percent" = round(sum(OPERATION_STATUS == "FAIL") / .N * 100, 2),
  "30 Day Percent" = round(sum(OPERATION_STATUS[group_i] == "FAIL") / sum(group_i) * 100, 2)
   ),
  keyby = .("Area" = CRIT_CODE)], rownames = FALSE) %>% 
  formatStyle("Area", color=ifelse(("Overall Percent" < "30 Day Percent"), "red", "black"), target="row)
  return(dt)
Lost
  • 331
  • 1
  • 12
  • You can add a calculated column to base your style on and [hide it](https://stackoverflow.com/questions/36815622/how-to-hide-a-column-using-the-dt-package-columndefs-parameter-doesnt-work). I hope this helps. If not, please add some sample data and create a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Florian Jun 22 '18 at 13:49
  • @Florian that worked. I needed to set up the datatable first then do the options part within renderDataTable. I could not do both in the same statement. I still am having trouble with the two different colors – Lost Jun 22 '18 at 17:14

0 Answers0