celestialgod

shiny interactive select

Jul 19th, 2015
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.28 KB | None | 0 0
  1. library(shiny)
  2. df <- data.frame(id = c("1","1","1","1","1","1","2","2","2","2"),
  3.                  brand = c("w","w","w","s","s","s","w","w","w","s"),
  4.                  model =
  5. c("w123","w123","w456","s99","s88","s88","w123","w456","w456","s99"),
  6.                  amount = c(10,9,7,8,6,4,7,3,2,8))
  7. df$id=as.character(df$id)
  8. df$brand=as.character(df$brand)
  9. df$model=as.character(df$model)
  10. app = shinyApp(
  11.   ui = shinyUI(fluidPage(
  12.    titlePanel("hirearchy data group"),
  13.    sidebarLayout
  14.    (
  15.    sidebarPanel
  16.    (
  17.    selectInput("brand",label="choice the brand",choices=c("all","w","s")),
  18.    selectInput("model",label="choice the model",choices=c("all","w123","w456","s99","s88"))
  19.    ),
  20.    mainPanel
  21.    (
  22.    dataTableOutput("table")
  23.    )
  24.    ))),
  25.   server = function(input, output, session) {
  26.     output$table <- renderDataTable({
  27.       if(input$brand!="all") {df=df[which(df$brand==input$brand),]}
  28.       if(input$model!="all") {df=df[which(df$model==input$model),]}
  29.     df
  30.     })
  31.     models = observe({
  32.       input$brand
  33.       if (input$brand=="all"){
  34.         choice = c("all", unique(df$model))
  35.       }else{
  36.         choice = c("all", unique(df[df$brand==input$brand,]$model))
  37.       }
  38.       updateSelectInput(session, "model", choice = choice)
  39.     })
  40.   }
  41. )
  42. runApp(app)
Advertisement
Add Comment
Please, Sign In to add comment