简体   繁体   中英

R shiny: Multiple updateSelectInput in datatable should be executed when a button is pressed, but only the first one is updated

I have a simple table with dropdowns (selectInput) for each cell in one of the columns. Whenever a the button "Go" is clicked each of these dropdowns should be updated. Unfortunately only the first one is updated. As far as I understood, the issue is that the first "updateSelectInput" immediately triggers the rendering of the output table. What do I have to change in the code to make it work? Any suggestions are highly appreciated!

library(shiny)
library(DT)

ui <- fluidPage(
  title = 'Updateable Selectinput column in a table',
  h3("Source:", tags$a("Yihui Xie", href = "https://yihui.shinyapps.io/DT-radio/")),
  shiny::actionButton(inputId = "btn", label = "Go"),
  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"))
  }

 
  observeEvent(input[["btn"]], {
    for (i in 1:nrow(data)){
      updateSelectInput(session = session, inputId = paste0("sel", i), label = "", choices = 1:nrow(data))
    }
  })

  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[["btn"]]
    str(sapply(1:nrow(data), function(i) input[[paste0("sel", i)]]))
  })
  }

shinyApp(ui, server)

I don't understand what's going on, but it works with label = NULL :

updateSelectInput(session = session, inputId = paste0("sel", i), label = NULL, choices = 1:nrow(data))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM