我是新手,有点喜欢它。现在我有一个需要帮助的有趣问题。我有一个数据库可以通过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)
答案 0 :(得分:0)
这是一种简单的方法,只有当所选值不是默认值时才更新:
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")
})
}