1

在我的 ui.RI 中有一个 uiOutput 和一个按钮,允许我在单击时随意添加 selectInput。这些 selectInput 的可能选择是相同的,因此当您有两个 selectInput 时,一个的更新会影响另一个的选择。我遇到的问题是,在对 selectInput 进行更新后,我无法更新 selectInput 上的选项。这是代码。

在 Ui.R

div(id="inference_species_group_div",
  box(h5(strong(uiInferenceSpeciesGroup)), width = "100%",
    fluidRow(
      column(width = 6, id = "inference_species_group_inputs_col",
        uiOutput("inf_par_sp_group_output_1")
      )
    ),
    br(),
    fluidRow(
      column(width = 12, id = "inference_species_group_add_input_col",
             actionButton("inference_species_group_add_input_btn", uiInferenceSpeciesGroupAdd, icon = icon("plus"))
      )
    )
  )
)

在服务器.R

observeEvent(input$inference_species_group_add_input_btn, {
  addInfParSpGroupAdd()
})

addInfParSpGroupAdd <- function(){
  InfParSpGpInd <<- InfParSpGpInd + 1
  output[[paste0("inf_par_sp_group_output_", InfParSpGpInd)]] <- renderUI({
    list(
      fluidRow(
        column(width=8, 
               selectInput(paste0("inf_par_sp_group_select_", InfParSpGpInd), 
                  label=paste0(uiInferenceSpeciesGroupInputLabel, ""),
                  multiple=T, choices = InfParSpGpListSp, selected = NULL
               )
        ),
        column(width=1, id=paste0("inf_par_sp_group_rmv_col_", InfParSpGpInd),
               actionButton(paste0("inf_par_sp_group_rmv_btn_", InfParSpGpInd), "", icon = icon("times"),
                  onclick = paste0("Shiny.onInputChange('removeGroupSpecies', ", InfParSpGpInd, ")")
               )
        ),
        tags$style(type='text/css', paste0("#inf_par_sp_group_rmv_col_", InfParSpGpInd, " {padding-top:4%}"))
      ),
      uiOutput(paste0("inf_par_sp_group_output_", (InfParSpGpInd + 1)))
    )
  })
  observeEvent(input[[paste0("inf_par_sp_group_select_", InfParSpGpInd)]], {
    selectedInInput = input[[paste0("inf_par_sp_group_select_", InfParSpGpInd)]]
    InfParSpGpListSp <<- InfParSpGpListSp [! InfParSpGpListSp %in% selectedInInput]
    rebindGroupSpeciesInputs()
  })
}

observeEvent(input$removeGroupSpecies, {
  i = input$removeGroupSpecies
  selectedInInput = input[[paste0("inf_par_sp_group_select_", i)]]
  output[[paste0("inf_par_sp_group_output_", i)]] <- renderUI({
    uiOutput(paste0("inf_par_sp_group_output_", (i + 1)))
  })
  if (!is.null(selectedInInput) && length(selectedInInput) > 0){
    InfParSpGpListSp <<- c(InfParSpGpListSp, selectedInInput)
    rebindGroupSpeciesInputs()
  }
})

rebindGroupSpeciesInputs <- function(){
  for (i in 1:InfParSpGpInd){
    if (!is.null(input[[paste0("inf_par_sp_group_select_", i)]])){
      updateSelectInput(session, input[[paste0("inf_par_sp_group_select_", i)]], choices = InfParSpGpListSp)
    }
  }
}

在这一行中,

updateSelectInput(session, input[[paste0("inf_par_sp_group_select_", i)]], choices = InfParSpGpListSp)

的值InfParSpGpListSp应该是,但 selectInput/selectizeInput 不会更新选项列表。

4

0 回答 0