Search code examples
rshinydtshinyjs

can't make ShinyJS disable a checkbox in Shiny DT


I am rendering the full column with checkboxes, each checkbox has its individual id so I can play with the different options and catch the relative states.

in the example below as you can see all the checkboxes will be named chkbx_ the rownumber, e.g. for row1 in the column newvar I'll have a checkbox with id chkbx_1.

now let's say that I want the first checkbox to be disabled, this should be easily achievable as far as I know with ShinyJS (I am doing that for some buttons outside from the DT and it works).

So I am calling the disable function on id chkbx_1, but for some reasons the checkbox is still selectable.

Any idea on how to proceed?

here below the sample code to quickly replicate the issue.

library("DT")
library("shinyjs")

ui <- basicPage(
  h2("The mtcars data"),
  useShinyjs(),
  DT::dataTableOutput("mytable")
)

server <- function(input, output) {
  
  shinyInput <- function(FUN, len, id, ...) {
    inputs <- as.character(FUN(paste0(id,'_', len), ...))
    inputs
  }
  
  mtcarsx <- data.frame(mtcars)
  
  
  for(myrow in 1:nrow(mtcarsx)){
    mtcarsx[myrow,"newvar"]=toString(shinyInput(checkboxInput,toString(myrow),"chkbx",label="",value=FALSE,width=NULL))
  }

  shinyjs::disable("chkbx_1")
  
  
  output$mytable = DT::renderDataTable({
    DT::datatable(mtcarsx, 
                  escape = FALSE, 
                  selection = 'none', 
                  rownames = TRUE, 
                  extensions = c('FixedColumns'),
                  options = list(searching = FALSE, 
                                 ordering  = FALSE,
                                 autoWidth = TRUE,
                                 scrollX = TRUE,
                                 FixedColumns = list(leftColumns = c(2))
                                 
                  ))
  })
  
}

shinyApp(ui, server)

Solution

  • Try this

    library("DT")
    library("shinyjs")
    
    ui <- basicPage(
      h2("The mtcars data"),
      useShinyjs(),
      DT::dataTableOutput("mytable")
    )
    
    server <- function(input, output) {
      
      shinyInput <- function(FUN, len, id, ...) {
        inputs <- as.character(FUN(paste0(id,'_', len), ...))
        inputs
      }
      
      mtcarsx <- data.frame(mtcars)
      
      
      for(myrow in 1:nrow(mtcarsx)){
        mtcarsx[myrow,"newvar"]=toString(shinyInput(checkboxInput,toString(myrow),"chkbx",label="",value=FALSE,width=NULL))
      }
      
      observe({
        shinyjs::disable("chkbx_1")
        print(input$chkbx_2)
      })
      
      output$mytable = DT::renderDataTable({
        DT::datatable(mtcarsx, 
                      escape = FALSE, 
                      selection = 'none', 
                      rownames = TRUE, 
                      extensions = c('FixedColumns'),
                      options = list(searching = FALSE, 
                                     ordering  = FALSE,
                                     autoWidth = TRUE,
                                     scrollX = TRUE,
                                     FixedColumns = list(leftColumns = c(2)),
                                     preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
                                     drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } ')
                                     
                      ))
      })
      
    }
    
    shinyApp(ui, server)