我一直試圖擴展Ramnath發佈的第二個解決方案(使用updateSelectInput
的那個),最初發布在R shiny passing reactive to selectInput choices處。我希望允許用戶從分層的一系列輸入中選擇輸入,其中每個選擇中的選擇更新下一個選擇中的可能值。作爲一個例子,在下面我通過簡單地添加第三個輸入來修改Ramnath的代碼,第三個輸入列出了第二個輸入中所選變量的值。此示例基於mtcars
和iris
數據集,並在R v 3.1.1和RStudio v 0.98.1062上運行。它不會拋出錯誤,但您會看到我被困在如何添加第二個reactive()
,其輸入爲input$dataset
和input$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())
})
}
))
[R光澤通過反應性selectInput選擇](HTTP的可能重複。 com/questions/21465411/r-shiny-passing-reactive-to-selectinput-choices),一種方法是將'selectInput'封裝在服務器端的'renderUI'中。 – Alex 2014-10-09 00:11:32