【问题标题】:Update SelectInput without trigger reactive?更新 SelectInput 而不触发反应?
【发布时间】:2017-09-06 04:34:24
【问题描述】:

我是闪亮的新手,但有点喜欢它。现在我有一个有趣的问题需要帮助。我有一个数据库可以通过 indexA 和 indexB 查询,但不能同时查询。也就是说,如果我使用 selectInput 从一个索引(例如 indexA)检索数据,我必须将另一个索引(在本例中为 indexB)设置为默认值(B0),反之亦然。输出小部件取决于两个 selectInput。因此,如果我与一个 selectInput 交互来查询数据,我需要更新另一个 selectInput,这将导致 selectInput 的响应式将被调用两次。无论如何执行updateSelectInput而不触发reactive()? 简化代码如下供您参考:

library(shiny)

indexA = c('A0', 'A1', 'A2', 'A3', 'A4', 'A5')
indexB = c('B0', 'B1', 'B2', 'B3', 'B4', 'B5')

ui <- fluidPage(
    selectInput('SelA', 'IndexA', choices = indexA, selected = NULL),
    selectInput('SelB', 'IndexB', choices = indexB, selected = NULL), 
    verbatimTextOutput('textout')
)

server <- function(input, output, session) {
    GetIndexA <- reactive({
        updateSelectInput(session, "SelB", choices = indexB, selected = NULL)
        ta <- input$SelA
    })

    GetIndexB <- reactive({
        updateSelectInput(session, "SelA", choices = indexA, selected = NULL)
        tb <- input$SelB
    })

    output$textout <- renderText({
        textA = GetIndexA()
        textB = GetIndexB()
        paste("IndexA=", textA, " IndexB=", textB, "\n")
    })
}

shinyApp(ui, server) 

【问题讨论】:

    标签: shiny reactive


    【解决方案1】:

    这是一种简单的方法,仅当所选值不是默认值时才更新:

    server <- function(input, output, session) {
      GetIndexA <- reactive({
        ta <- input$SelA
        if(ta!=indexA[1])
          updateSelectInput(session, "SelB", choices = indexB, selected = NULL)
        ta
      })
    
      GetIndexB <- reactive({
        tb <- input$SelB
        if(tb!=indexB[1])
          updateSelectInput(session, "SelA", choices = indexA, selected = NULL)
        tb
      })
    
      output$textout <- renderText({
        textA = GetIndexA()
        textB = GetIndexB()
        paste("IndexA=", textA, " IndexB=", textB, "\n")
      })
    }
    

    【讨论】:

    • @Sullivan 如果这是正确答案,您应该将其作为已接受的答案进行检查。
    猜你喜欢
    • 2019-06-30
    • 1970-01-01
    • 1970-01-01
    • 2019-12-08
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    • 2012-08-29
    • 2020-08-07
    相关资源
    最近更新 更多