1

I am new to shiny/r and trying to change the background color of a cell (DT table) base on value of another cell. I tried using this example from https://rstudio.github.io/DT/010-style.html

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

But somehow it doesn't seem to work for me

Here's my code :

dt_output = function(title, id) {
        fluidRow(column(
        12, h1(paste0(title)),
        hr(), DTOutput(id)
        ))
        }

 render_dt = function(data, editable = 'cell', server = TRUE, ...) {
    renderDT(data, selection = 'none', server = server, editable = editable, ...)
    }

ui = fluidPage(
downloadButton("mcp_csv", "Download in CSV", class="but"),
    
dt_output('Report', 'x9'),
)

server = function(input, output, session) {
d1 = readRDS("cmp.rds")
d9 = d1

output$x9 = render_dt(d9, 'cell', rownames = FALSE, extensions = 'Buttons', options = list(dom = 'Bfrtip', buttons = I('colvis')))
    
observeEvent(input$x9_cell_edit, {
d9 <<- editData(d9, input$x9_cell_edit, 'x9', rownames = FALSE)
saveRDS(d9, 'cmp.rds', version = 2)
})

datatable(d9) %>% formatStyle(
'R/Y/G', 'Y', #'R/Y/G' is the column cell I'm trying to change values for based on column 'Y'
backgroundColor = styleEqual(c(0, 1), c('red', 'yellow'))
)

I'm not sure what I'm doing wrong. Maybe it's in the wrong place, I don't know. Also if I needed to change color of column 'R/Y/G' based on three different columns (R, Y, G), based on dynamic input (not hardcoded like 0 and 1) =, how would I implement that? Thanks

P.S. If I add this code

dt_d9=datatable(d9) %>% formatStyle(
'R/Y/G', 'Y',
backgroundColor = styleEqual(c(0, 1), c('red', 'yellow'))
)

and replace

output$x9 = render_dt(d9, 'cell', rownames = FALSE, extensions = 'Buttons', options = list(dom = 'Bfrtip', buttons = I('colvis')))

with

output$x9 = render_dt(dt_d9, 'cell', rownames = FALSE, extensions = 'Buttons', options = list(dom = 'Bfrtip', buttons = I('colvis')))

I do get the colors on the R/Y/G column, but the edit cell function stops working. The edit cell function can be found here : https://yihui.shinyapps.io/DT-edit/

retrx22
  • 61
  • 1
  • 9

1 Answers1

1

Your code gives the following error:

renderDataTable ignores ... arguments when expr yields a datatable object;

Since you now return a DT::datatable object to renderDT and not a dataframe. This is a bit similar to this Q/A. So now you have to move all arguments to the DT::datatable constructor:

render_dt = function(data) {
 renderDT(data)
}

and

  dt_d9 <- datatable(d9, editable = 'cell', rownames = FALSE, extensions = 'Buttons', options = list(dom = 'Bfrtip', buttons = I('colvis'))) %>% formatStyle(
'R/Y/G', 'Y', #'R/Y/G' is the column cell I'm trying to change values for based on column 'Y'
backgroundColor = styleEqual(c(0, 1), c('red', 'yellow'))
 )

 output$x9 = render_dt(dt_d9)
julien.leroux5
  • 969
  • 7
  • 17
  • I'm trying to change cell color of column 'R/Y/G' as soon as values of column 'R', 'Y' or 'G' is changed.The color changes, but only when I edit a cell value and close the app and reopen it again. But it doesn't change as soon as I edit the cell value. Here's what I'm doing : – retrx22 Aug 19 '21 at 17:31
  • d9$tcolor <- ifelse(d9$R > 2500000, 2, ifelse(d9$Y > 2000000 & d9$Y <= 2500000, 0, ifelse(d9$G <= 2000000, 1))) dt_d9=datatable(d9, editable = 'cell', rownames = FALSE, extensions = 'Buttons', options = list(dom = 'Bfrtip', buttons = I('colvis'))) %>% formatStyle( 'R/Y/G', 'tcolor', backgroundColor = styleEqual(c(0,1,2), c('yellow', 'green', 'red')),fontWeight = 'bold' ) output$x9 = render_dt(dt_d9) – retrx22 Aug 19 '21 at 17:31
  • 1
    See my [answer to this question you asked in a specific post](https://stackoverflow.com/a/68868445/1892433) – julien.leroux5 Aug 20 '21 at 21:59
  • It worked, thanks. Had to remove this d9$tcolor <- NA. Otherwise the colors don't show up when I run the app the first time and reflect once I make any edits. But thanks a lot. – retrx22 Aug 21 '21 at 13:55
  • 'R/Y/G' column has three colors, red , green and yellow. How do I set up an email alert as soon as the color of the cell changes from green to yellow or yellow to red? – retrx22 Aug 24 '21 at 06:38
  • Use the [mailR package](https://www.rdocumentation.org/packages/mailR/versions/0.4.1) conditionnally with a new `observeEvent` observing values of the `tcolor` column. – julien.leroux5 Aug 24 '21 at 15:43
  • tried using this code to send mail via outlook but it didn't work send.mail(from = "me@companydomain.com", to = c("coworker@companydomain.com"), subject = "Test Email", body = "testing", authenticate = TRUE, smtp = list(host.name = "smtp.office365.com", port = 587, user.name = "me@companydomain.com", passwd = "Password", sls = TRUE, tls = TRUE) – retrx22 Aug 31 '21 at 06:29
  • Please take a look at this as well https://stackoverflow.com/questions/69051085/column-created-in-r-lose-their-values-as-soon-as-query-is-rerun-r-shiny – retrx22 Sep 03 '21 at 22:29