1

I'm trying to change cell color of column 'R/Y/G' as soon as values of column 'R', 'Y' or 'G' is changed. I have made the datatable editable for that reason. 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 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 as CSV", class="but"),
  
  dt_output('Report', 'x9')
)

server = function(input, output, session) {
  d1 = readRDS("cmp.rds")
  d9 = d1
  
  observeEvent(input$x9_cell_edit, {
    d9 <<- editData(d9, input$x9_cell_edit, 'x9', rownames = FALSE)
    saveRDS(d9, 'cmp.rds', version = 2)
  })
  
  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
  • 61
  • 1
  • 9
  • Try [that](https://stackoverflow.com/a/67028667/1100107). Does it help? – Stéphane Laurent Aug 18 '21 at 14:59
  • I'll give it a go, thanks. Any way I can do the same without js? – retrx22 Aug 18 '21 at 15:04
  • The `formatXXX` functions run some JavaScript, anyway. And I don't know another way than the one in my link. – Stéphane Laurent Aug 18 '21 at 15:23
  • I don't have a lot of JS experience, but I'll try and incorporate it. Thanks – retrx22 Aug 18 '21 at 15:24
  • It doesn't work as expected. It colors a cell that I edit. What I want is to color column 'R/Y/G' based on column 'R', 'Y' or 'G' and 'R/Y/G' should not have any values, just colors – retrx22 Aug 19 '21 at 05:56
  • It's hard to understand because your code is not reproducible. – Stéphane Laurent Aug 19 '21 at 12:50
  • It'll be easier to assume a table with just 4 columns : 'R/Y/G' , 'R', 'Y' and 'G'. Column 'R/Y/G' will just have colors (red , yellow and green) based on the values entered in 'R', 'Y' or 'G' – retrx22 Aug 19 '21 at 13:33
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/236195/discussion-between-retrx22-and-stephane-laurent). – retrx22 Aug 19 '21 at 13:33

1 Answers1

1

Based on ideas found here, here's the server part, storing the dataframe in a reactiveValues():

server = function(input, output, session) {
  d1 = readRDS("cmp.RDS")
  d9 = d1
 
  d9$tcolor <- NA
  rv <- reactiveValues()
  observe({
    rv$d9 <- d9
  })
  
  dt_d9=datatable(isolate(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)

  proxy = dataTableProxy('x9')
  observe({
    DT::replaceData(proxy, rv$d9, rownames = FALSE, resetPaging = FALSE)
  })
  
  observeEvent(input$x9_cell_edit, {
    rv$d9 <<- editData(rv$d9, input$x9_cell_edit, 'x9', rownames = FALSE)
    d9 <- rv$d9
    d9$tcolor <- ifelse(d9$R > 2500000, 2,
                        ifelse(d9$Y > 2000000 & d9$Y <= 2500000, 0,
                               ifelse(d9$G <= 2000000, 1)))
    rv$d9 <<- d9
    saveRDS(d9, 'cmp.rds', version = 2)
    
  })
  
}
julien.leroux5
  • 969
  • 7
  • 17
  • 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