2017-04-11 69 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

@沙利文如果這是正確的答案,你應該檢查它是接受的。 –