[問題] [shiny] 如何設計有階層的input?

作者: ardodo (米蟲)   2015-07-20 11:49:53
各位版友好,我現在想設計兩個selectInput在我的shiny app上,
第一個input叫做廠牌,第二個input叫做型號
我想要在選擇廠牌時(ex: w),第二個input的型號只剩下屬於廠牌w的型號(ex:w123,w456)
選擇廠牌s時,第二個input的型號只剩下屬於廠牌s的型號(s99,s88)
同時我的output table可以跟著我選擇的input互動
我目前的程式碼只能做到選擇廠牌時table有互動,選擇input時table有互動,
還沒辦法做到第二個input隨著第一個input的變化而跟著變化
以上是我的問題,若有描述不清的請告知我,以下是我的code
ui:
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")
)
))))
server:
library(shiny)
## test dataframe
df <- data.frame(id = c("1","1","1","1","1","1","2","2","2","2"),
brand = c("w","w","w","s","s","s","w","w","w","s"),
model =
c("w123","w123","w456","s99","s88","s88","w123","w456","w456","s99"),
amount = c(10,9,7,8,6,4,7,3,2,8))
df$id=as.character(df$id)
df$brand=as.character(df$brand)
df$model=as.character(df$model)
shinyServer(function(input, output) {
output$table <- renderDataTable({
if(input$brand!="all") {df=df[which(df$brand==input$brand),]}
if(input$model!="all") {df=df[which(df$model==input$model),]}
df
})
})
作者: lofu (lofu)   2015-07-20 12:11:00
我也剛學,也想知道這種狀況下的處理
作者: superalf (外星人)   2015-07-25 00:08:00

Links booklink

Contact Us: admin [ a t ] ucptt.com