2017-04-11 68 views
0

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

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

这里有一个简单的方法通过更新做到这一点,只有当选定的值不是默认值:

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") 
    }) 
} 
+0

感谢。有用。 – Sullivan

+0

@沙利文如果这是正确的答案,你应该检查它是接受的。 –