Search code examples
rshinydatatabledt

How to add checkbox and dropdown list to DT table in shiny app


i am investigating the problem to embed checkbox and dropdown list into separate columns of DT datatable. Now I can populate only one column with checkbox or dropdown list.

Right now, i have managed to find nice examples of checkbox or dropdown list via callback option. But i have not managed to find the example where i can embed both of elements into separate columns of the same DT table.

I have found nice example of dropdown list (see the answer section): Example of dropdown list in DT table.

I would be grateful if someone could make a hint how to add the extra column with checkboxes in above mentioned example (besides dropdown list). I am not skilled in JS.

UPDATED: I have tried this version (added checkbox line) but the string is not generated while selecting the dropdown list as in the example specified above

library(shiny)
library(DT)

ui <- fluidPage(
  title = 'Selectinput column in a table',
  h3("Source:", tags$a("Yihui Xie", href = "https://yihui.shinyapps.io/DT-radio/")),
  DT::dataTableOutput('foo'),
  verbatimTextOutput('sel')
)

server <- function(input, output, session) {
  data <- head(iris, 5)
  
  for (i in 1:nrow(data)) {
    data$species_selector[i] <- as.character(selectInput(paste0("sel", i), "", choices = unique(iris$Species), width = "100px"))
    data$species_checkbox[i] <- as.character(checkboxInput(paste0("sel_checkbox_", i), ""))
  }
   
  output$foo = DT::renderDataTable(
    data, escape = FALSE, selection = 'none', server = FALSE,
    options = list(dom = 't', paging = FALSE, ordering = FALSE),
    callback = JS("table.rows().every(function(i, tab, row) {
                    var $this = $(this.node());
                    $this.attr('id', this.data()[0]);
                    $this.addClass('shiny-input-container');
                  });
                  Shiny.unbindAll(table.table().node());
                  Shiny.bindAll(table.table().node());")
                )
  output$sel = renderPrint({
    str(sapply(1:nrow(data), function(i) input[[paste0("sel", i)]]))
  })
}

shinyApp(ui, server)

Solution

  • library(shiny)
    library(DT)
    
    ui <- fluidPage(
      # Sidebar panel
      sidebarPanel(),
      
      # Main panel with the table
      mainPanel(
        DTOutput("myTable")
      )
    )
    
    dat <- head(iris, 5)
    dat$species_selector <- vapply(1:5, function(i) {
      as.character(
        selectInput(
          paste0("sel", i), label = NULL, choices = unique(iris$Species), selectize = FALSE
        )
      )
    }, character(1))
    dat$species_checkbox <- vapply(1:5, function(i) {
      as.character(
        checkboxInput(
          paste0("sel_checkbox_", i), label = NULL
        )
      )
    }, character(1))
    
    
    server <- function(input, output, session){
    
      output$myTable <- renderDT({
        datatable(
          dat, 
          escape = FALSE,
          select = "none",
          options = list(
            columnDefs = list(
              list(targets = "_all", className = "dt-center")
            ),
            preDrawCallback = 
              JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
            drawCallback = 
              JS('function() { Shiny.bindAll(this.api().table().node()); } ')
          )
        )
      })
      
      observe({
        print(input$sel1)
        print(input$sel_checkbox_1)
      })
    }
    
    shinyApp(ui, server)