render dropdown for single column in DT shiny

前端 未结 1 1805
感动是毒
感动是毒 2020-12-10 18:57

I\'m not proficient in Javascript and would like to replicate a dropdown function as is available in the rhandsontable package but for the DT package.

How could this

相关标签:
1条回答
  • 2020-12-10 19:24

    I blatantly stole the idea from Yihui's app for including radioButtons in DT.

    Code:

    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"))
      }
    
      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)
    

    Output:

    0 讨论(0)
提交回复
热议问题