0

I'm having the following problem. I want to conditionally format my columns so that values in column 2 that are non-positive are coloured red. I also want this condition to hold afte a cell has been edited. For example:

library(DT)
df <- data.frame(
     col1 = 1:5,
     col2 = c(-1,-1,0,1,1)
   )
datatable(df,editable = T)%>%
formatStyle(
2,
color = styleInterval(cuts = 0,values = c("red","black"))
)

It works when it's displayed in the viewer section of the RStudio IDE. As I edit cell values, the colour is also updated to reflect the new value.

However, when I run it in R Shiny:

ui <- fluidPage(
  dataTableOutput("test")
)
server <- function(input,output,sesion){
  output$test <- DT::renderDataTable({
    df <- data.frame(
      col1 = 1:5,
      col2 = c(-1,-1,0,1,1)
    )
    datatable(df,editable = T)%>%
      formatStyle(
        2,
        color = styleInterval(cuts = 0,values = c("red","black"))
      )
  })
}
shinyApp(ui,server)

the callback becomes completely unresponsive. The cells coloured red at initialisation will remain red no matter how I try to edit the cell value. Can somebody please shed light as to how this phenomenon happens and how I can fix this?

Viet Dang
  • 37
  • 4

1 Answers1

0

This works with the option server = FALSE.

library(shiny)
library(DT)

ui <- fluidPage(
  DTOutput("test")
)
server <- function(input,output,sesion){
  output$test <- renderDT({
    df <- data.frame(
      col1 = 1:5,
      col2 = c(-1,-1,0,1,1)
    )
    datatable(df,editable = T)%>%
      formatStyle(
        2,
        color = styleInterval(cuts = 0,values = c("red","black"))
      )
  }, server = FALSE)
}
shinyApp(ui,server)
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
  • Works like a charm, thank you! If it's not too long a question can you explain what caused this behaviour and if there's another solution (even if that involves javascript code) if we want to retain `server = TRUE`? – Viet Dang May 06 '21 at 09:53