簡體   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

我有一個簡單的表格,其中一列中的每個單元格都有下拉列表(selectInput)。 每當單擊“開始”按鈕時,都應更新這些下拉列表中的每一個。 不幸的是,只有第一個被更新。 據我了解,問題在於第一個“updateSelectInput”立即觸發了 output 表的渲染。 我必須對代碼進行哪些更改才能使其正常工作? 任何建議都非常感謝!

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)

我不明白發生了什么,但它適用於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