2014-10-08 78 views
1

我一直試圖擴展Ramnath發佈的第二個解決方案(使用updateSelectInput的那個),最初發布在R shiny passing reactive to selectInput choices處。我希望允許用戶從分層的一系列輸入中選擇輸入,其中每個選擇中的選擇更新下一個選擇中的可能值。作爲一個例子,在下面我通過簡單地添加第三個輸入來修改Ramnath的代碼,第三個輸入列出了第二個輸入中所選變量的值。此示例基於mtcarsiris數據集,並在R v 3.1.1和RStudio v 0.98.1062上運行。它不會拋出錯誤,但您會看到我被困在如何添加第二個reactive(),其輸入爲input$datasetinput$column的組合。R Shiny:傳遞多個成功的反應選擇輸入

library(shiny) 

runApp(list(

    ui = bootstrapPage(
    selectInput('dataset', 'Choose data set', c('mtcars', 'iris')), 
    selectInput('columns', 'Choose variable', ""), 
    selectInput('values', 'Show values', "") 
), 

    server = function(input, output, session){ 

    # updates variable names based on selected dataset 
    outVar = reactive({ 
     names(get(input$dataset)) 
    }) 

    # i want this to update the values of the selected variable 
    outVar2 = reactive({ 
     sort(unique(get(input$dataset)[, 2])) # this works but of course I don't want the second variable every time 
     #sort(unique(get(input$dataset)[, input$columns])) # this fails but this is the idea I'm after 
    }) 

    # i want these to update the UI based on the reactive output above 
    observe({ 
     updateSelectInput(session, "columns", choices = outVar()) 
     updateSelectInput(session, "values", choices = outVar2()) 
    })  
    } 
)) 
+1

[R光澤通過反應性selectInput選擇](HTTP的可能重複。 com/questions/21465411/r-shiny-passing-reactive-to-selectinput-choices),一種方法是將'selectInput'封裝在服務器端的'renderUI'中。 – Alex 2014-10-09 00:11:32

回答

3

迴應我的第一個問題。看起來這很舊,但自從我上週一直在玩Shiny,我想我會直接回答這個問題。

要直接回答該問題,sort(unique(get(input$dataset)[, input$columns]))語句不會失敗,它是observe語句。因爲outVar()outVar2()都在observe語句中,所以在任何變量更新時都會運行observe語句。因此,當更改第二個selectInputinput$columns變量)時,它實際上會導致第一個selectInput通過updateSelectInput(session, "columns", choices = outVar())進行更新。

然後,這會將列下拉列表重置爲原始選項,使其看起來好像應用程序已損壞。要查看這個慢鏡頭,我會建議在observe語句添加browser()調用代碼是如何一步一步地:

observe({ 
    browser() 
    updateSelectInput(session, "columns", choices = outVar()) 
    updateSelectInput(session, "values", choices = outVar2()) 
}) 

總之,我打破了observe語句轉換成2個observeEvent語句(我有錯誤時,只當我更改數據集時,將聲明分成兩個單獨的observe聲明)。通過包裝中的observeEvent功能updateSelectInput它們將只嘗試時必要的輸入實際更新運行,如下所示://計算器:

library(shiny) 

runApp(list(

    ui = bootstrapPage(
    selectInput('dataset', 'Choose data set', c('mtcars', 'iris')), 
    selectInput('columns', 'Choose variable', ""), 
    selectInput('values', 'Show values', "") 
), 

    server = function(input, output, session){ 

    # updates variable names based on selected dataset 
    outVar = reactive({ 
     names(get(input$dataset)) 
    }) 

    # i want this to update the values of the selected variable 
    outVar2 = reactive({ 
     if (input$columns == "") return() 
     sort(unique(get(input$dataset)[, input$columns])) 
    }) 

    # create separate observeEvents to 
    observeEvent(input$dataset, { 
     updateSelectInput(session, "columns", choices = outVar()) 
    }) 

    observeEvent(input$columns, { 
     updateSelectInput(session, "values", choices = outVar2()) 
    }) 

    } 
))