我想用反应表输出做一个层次结构菜单。我希望我的应用程序执行以下操作:
当我选择brand = w
时,我可以选择的模型输入应该只是w的较低层次结构(在这种情况下:model
应该是w123
和{{1表格输出应该是w456
当我选择brand = w和model = w123时,表格输出应列出brand w
这是我的代码,任何人都可以帮助我吗?感谢
UI:
brand = w & model = w123
服务器
library(shiny)
shinyUI((fluidPage(
titlePanel("hirearchy data group"),
sidebarLayout
(
sidebarPanel
(
selectInput("brand",label="choice the brand",choices=c("all","w","s")),
selectInput("model",label="choice the model",choices=c("all","w123","w456","s99","s88"))
),
mainPanel
(
dataTableOutput("table")
)
))))
答案 0 :(得分:1)
将此代码添加到您的服务器:
# This gets re-evaluated when input$brand changes
observeEvent(input$brand, {
brand <- input$brand
# Available model options based on brand
choices <- switch(brand, "w" = c("all","w123","w456"),
"s" = c("all","s99","s88"),
"all" = c("all","w123","w456","s99","s88"))
# Update the model input object to only display wanted choices
updateSelectInput(session, "model", choices = choices)
})
为了使updateSelectInput
起作用,您还需要修改服务器功能定义以包含session
对象:您的服务器定义应显示为shinyServer(function(input, output, session) {
。