简体   繁体   English

R shiny:按下按钮时应执行数据表中的多个updateSelectInput,但仅更新第一个

[英]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.我有一个简单的表格,其中一列中的每个单元格都有下拉列表(selectInput)。 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.据我了解,问题在于第一个“updateSelectInput”立即触发了 output 表的渲染。 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 :我不明白发生了什么,但它适用于label = NULL

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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