【发布时间】:2014-12-03 18:48:13
【问题描述】:
我一直在尝试扩展 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 的组合。
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 shiny passing reactive to selectInput choices 的可能重复项,一种方法是将
selectInput包装在服务器端的renderUI中。