在没有触发反应的情况下更新 SelectInput?
Update SelectInput without trigger reactive?
我是 shiny 的新手,但有点喜欢它。现在我有一个有趣的问题需要帮助。我有一个数据库可以通过 indexA 和 indexB 查询,但不能同时通过两者查询。也就是说,如果我使用 selectInput 从一个索引(例如 indexA)中检索数据,我必须将另一个索引(在本例中为 indexB)设置为默认值(B0),反之亦然。输出小部件取决于两个 selectInput。因此,如果我交互一个 selectInput 来查询数据,我需要更新另一个 selectInput,这将导致 selectInput 的反应被调用两次。有没有办法在不触发 reactive() 的情况下执行 updateSelectInput?
简化代码如下,供您参考:
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)
这是一种简单的方法,仅当所选值不是默认值时才更新:
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")
})
}
我是 shiny 的新手,但有点喜欢它。现在我有一个有趣的问题需要帮助。我有一个数据库可以通过 indexA 和 indexB 查询,但不能同时通过两者查询。也就是说,如果我使用 selectInput 从一个索引(例如 indexA)中检索数据,我必须将另一个索引(在本例中为 indexB)设置为默认值(B0),反之亦然。输出小部件取决于两个 selectInput。因此,如果我交互一个 selectInput 来查询数据,我需要更新另一个 selectInput,这将导致 selectInput 的反应被调用两次。有没有办法在不触发 reactive() 的情况下执行 updateSelectInput? 简化代码如下,供您参考:
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)
这是一种简单的方法,仅当所选值不是默认值时才更新:
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")
})
}