updateSelectInput 被闪亮应用程序中的 selectInput 覆盖

问题描述 投票:0回答:1

在下面的示例中,操作按钮将更新选择的输入值。但是,第二个选择输入依赖于第一个选择输入,并且当选择操作按钮时,不会更新为“virginica”。

ui <- fluidPage(

        actionButton(inputId = "action", label = "click"),
        uiOutput("select_col"),
        uiOutput("select_species")
    
    )
    
    #
    server <- function(input, output) {
    
    output$select_col <- renderUI({
        selectInput(inputId = "select_col", 
                label = "select_col", 
                choices = colnames(iris))
    })
    
    output$select_species <- renderUI({
        selectInput(inputId = "select_species",
                label = "Species",
                choices = unique(iris[ ,input$select_col]))
    })
    
    observeEvent(input$action, {
        updateSelectInput(inputId = "select_col", selected = "Species")
        updateSelectInput(inputId = "select_species", selected = "virginica")
    })
    
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)

我期望以下结果: 选择输入“select_col”中的“物种”和选择输入“物种”中的 setosa

r shiny selectinput
1个回答
0
投票

这应该有效:

  xxx <- reactiveVal(NULL)
  
  observeEvent(input$action, {
    updateSelectInput(inputId = "select_col", selected = "Species")
    xxx(TRUE)
  })
  
  observeEvent(xxx(), {
    updateSelectInput(inputId = "select_species", selected = "virginica")
    xxx(NULL)
  })

编辑

不,那不行。这是使用

shinyjs
包的 delay 函数的解决方案:

  observeEvent(input$action, {
    updateSelectInput(inputId = "select_col", selected = "Species")
    delay(0, updateSelectInput(inputId = "select_species", selected = "virginica"))
  })

别忘了:

library(shiny)
library(shinyjs)

ui <- fluidPage(
  useShinyjs(), # <- don't forget that
  actionButton(inputId = "action", label = "click"),
  ......
© www.soinside.com 2019 - 2024. All rights reserved.