rshinyselectinput

Strange behavior of selectInput in DT table


The answer must be trivial, and after hours of trying to understand the error, I can't see it. I am embedding a shiny widget into a DT table and trying to get the output of the widget (input$vect). With radioButton the input$vect is correct but with a selectInput, input$vect remains NULL.

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, 1)
  
  for (i in 1:nrow(data)) {
    data$species_selector[i] <- as.character(selectInput("vect", label = "",
                                                          choices = list("Choice 1" = 1, "Choice 2" = 2, "Choice 3" = 3),
                                                          selected = 1))
  }
  
  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({
    input$vect
  })
}

shinyApp(ui, server)

not working while the following is working :

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, 1)
  
  for (i in 1:nrow(data)) {
    data$species_selector[i] <- as.character(radioButtons("vect", label = "",
                                                         choices = list("Choice 1" = 1, "Choice 2" = 2, "Choice 3" = 3),
                                                         selected = 1))
  }
  
  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({
    input$vect
  })
}

shinyApp(ui, server)

Any idea ? Thx in advance !


Solution

  • if you are going to want to use a selectInput, you will need to disable its selectize param (set it to FALSE) , as the selectize requires various additional javascript to be bundled in.