Advertisement
swchen

shiny multiple input done

Aug 5th, 2015
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 3.31 KB | None | 0 0
  1. library(shiny)
  2. df <- data.frame(
  3.    brand=c("a","a","a","a","a","a","b","b","b","b","b","c","c","c","c","c"),
  4.    model=c("a21","a21","a21","a21","a31","a31","b55","b55","b56","b56","b56","c33","c34","c34","c33","c34"),
  5.    tax=c("y","x","y","x","x","y","x","x","x","y","y","x","y","x","y","y"),
  6.    seller=c("B","B","B","C","C","D","A","D","B","C","C","B","A","C","C","B"),
  7.    price=c(1000,1100,1500,1400,1200,1300,2000,2200,1900,1800,1700,800,900,850,950,880), stringsAsFactors = F)
  8. app = shinyApp(
  9.    ui = shinyUI(fluidPage(
  10.       titlePanel("hirearchy data group"),
  11.       sidebarLayout(
  12.          sidebarPanel(
  13.             selectInput("brand",label="choice the brand",choices=c("all", sort(unique(df$brand)))),
  14.             selectInput("model",label="choice the model",choices=c("all",sort(unique(df$model)))),
  15.             selectInput("tax",label="choice the tax",choices=c("all", sort(unique(df$tax)))),
  16.             selectInput("seller",label="choice the seller",choices=c("all", sort(unique(df$seller))))
  17.          ),
  18.          mainPanel(dataTableOutput("table"))
  19.       ))),
  20.    server = function(input, output, session) {
  21.       vals = reactiveValues()
  22.       vals$df = df
  23.       vals$brand = "all"
  24.       vals$model="all"
  25.       vals$tax = "all"
  26.       vals$seller = "all"
  27.       updateInput_f = function(panel_name){
  28.          choice = c("all", sort(unique(vals$df[[panel_name]]))) #[[]]變成字串
  29.          updateSelectInput(session, panel_name, choice = choice, selected = input[[panel_name]])
  30.       }
  31.       updateDF_f = function(){
  32.          vals$df = df
  33.          if (vals$brand != "all")
  34.             vals$df = vals$df[vals$df$brand == vals$brand,]
  35.          if (vals$model != "all")
  36.             vals$df = vals$df[vals$df$model == vals$model,]
  37.          if (vals$tax != "all")
  38.             vals$df = vals$df[vals$df$tax == vals$tax,]
  39.          if (vals$seller != "all")
  40.             vals$df = vals$df[vals$df$seller == vals$seller,]
  41.       }
  42.       brand = observe({
  43.          input$brand
  44.          isolate({
  45.             vals$brand = ifelse(input$brand == "all", "all", input$brand)
  46.             updateDF_f()
  47.             #updateInput_f("brand")
  48.             updateInput_f("model")
  49.             updateInput_f("tax")
  50.             updateInput_f("seller")
  51.          })
  52.       })
  53.       model = observe({
  54.          input$model
  55.          isolate({
  56.             vals$model = ifelse(input$brand == "all", "all", input$model)
  57.             updateDF_f()
  58.             updateInput_f("brand")
  59.             #updateInput_f("model")
  60.             updateInput_f("tax")
  61.             updateInput_f("seller")
  62.          })
  63.       })
  64.       tax = observe({
  65.          input$tax
  66.          isolate({
  67.             vals$tax = ifelse(input$tax == "all", "all", input$tax)
  68.             updateDF_f()
  69.             updateInput_f("brand")
  70.             updateInput_f("model")
  71.             #updateInput_f("tax")
  72.             updateInput_f("seller")
  73.          })
  74.       })
  75.       seller = observe({
  76.          input$seller
  77.          isolate({
  78.             vals$seller = ifelse(input$seller == "all", "all", input$seller)
  79.             updateDF_f()
  80.             updateInput_f("brand")
  81.             updateInput_f("model")
  82.             updateInput_f("tax")
  83.             #updateInput_f("seller")
  84.          })
  85.       })
  86.      
  87.       output$table = renderDataTable({vals$df})
  88.    }
  89. )
  90. runApp(app)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement