celestialgod

shiny multiple-interactive select

Aug 3rd, 2015
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.45 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("a11","a11","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("A","B","B","C","C","D","A","D","B","C","D","B","A","C","C","D"),
  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("tax",label="choice the tax",choices=c("all", sort(unique(df$tax)))),
  15.        selectInput("seller",label="choice the seller",choices=c("all", sort(unique(df$seller))))
  16.      ),
  17.      mainPanel(dataTableOutput("table"))
  18.    ))),
  19.   server = function(input, output, session) {
  20.     vals = reactiveValues()
  21.     vals$df = df
  22.     vals$brand = "all"
  23.     vals$tax = "all"
  24.     vals$seller = "all"
  25.     updateInput_f = function(panel_name){
  26.       choice = c("all", sort(unique(vals$df[[panel_name]])))
  27.       updateSelectInput(session, panel_name, choice = choice, selected = input[[panel_name]])
  28.     }
  29.     updateDF_f = function(){
  30.       vals$df = df
  31.       if (vals$brand != "all")
  32.         vals$df = vals$df[vals$df$brand == vals$brand,]
  33.       if (vals$tax != "all")
  34.         vals$df = vals$df[vals$df$tax == vals$tax,]
  35.       if (vals$seller != "all")
  36.         vals$df = vals$df[vals$df$seller == vals$seller,]
  37.     }
  38.     brand = observe({
  39.       input$brand
  40.       isolate({
  41.         vals$brand = ifelse(input$brand == "all", "all", input$brand)
  42.         updateDF_f()
  43.         updateInput_f("brand")
  44.         updateInput_f("tax")
  45.         updateInput_f("seller")
  46.       })
  47.     })
  48.     tax = observe({
  49.       input$tax
  50.       isolate({
  51.         vals$tax = ifelse(input$tax == "all", "all", input$tax)
  52.         updateDF_f()
  53.         updateInput_f("brand")
  54.         updateInput_f("tax")
  55.         updateInput_f("seller")
  56.       })
  57.     })
  58.     seller = observe({
  59.       input$seller
  60.       isolate({
  61.         vals$seller = ifelse(input$seller == "all", "all", input$seller)
  62.         updateDF_f()
  63.         updateInput_f("brand")
  64.         updateInput_f("tax")
  65.         updateInput_f("seller")
  66.       })
  67.     })
  68.     output$table = renderDataTable({vals$df})
  69.   }
  70. )
  71. runApp(app)
Advertisement
Add Comment
Please, Sign In to add comment