问题描述
我是闪亮的新手,但有点喜欢它.现在我有一个有趣的问题需要帮助.我有一个数据库可以通过 indexA 和 indexB 查询,但不能同时查询.也就是说,如果我使用 selectInput 从一个索引(例如 indexA)检索数据,我必须将另一个索引(在本例中为 indexB)设置为默认值(B0),反之亦然.输出小部件取决于两个 selectInput.因此,如果我交互一个 selectInput 来查询数据,我需要更新另一个 selectInput,这将导致 selectInput 的反应性将被调用两次.有没有办法在不触发reactive()的情况下执行updateSelectInput?简化代码如下供您参考:
I am new to shiny but kind of like it. Now I have an interesting question in needing of help. I have a database can be queried by either indexA and indexB, but not both. That is if I use selectInput to retrieve data from one index(for example, indexA), I have to set another index(in this case, indexB) to default value(B0), and vise versa. The output widget is depends on both selectInput. Hence, if I interact one selectInput to query data, I need to update another selectInput, which will cause the reactive of selectInput will be called twice. Is there anyway to execute updateSelectInput without triggering reactive()?The simplified code is below for your reference:
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)
推荐答案
这是一种简单的方法,仅当所选值不是默认值时才更新:
Here is a simple way to do it by updating only when selected value is not the default value:
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")
})
}
这篇关于在没有触发反应的情况下更新 SelectInput?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!