[問題] [shiny] 如何設計有階層的input?
各位版友好,我現在想設計兩個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
})
})
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.222.207.246
※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1437364196.A.2FE.html
※ 編輯: ardodo (61.222.207.246), 07/20/2015 12:01:02
※ 編輯: ardodo (61.222.207.246), 07/20/2015 12:01:42
推
07/20 12:11, , 1F
07/20 12:11, 1F
推
07/25 00:08, , 2F
07/25 00:08, 2F
→
07/25 00:08, , 3F
07/25 00:08, 3F
→
07/25 00:09, , 4F
07/25 00:09, 4F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 6 篇):
R_Language 近期熱門文章
PTT數位生活區 即時熱門文章