Advertisement
celestialgod

shiny multiple-interactive select V2

Aug 5th, 2015
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.15 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 = FALSE)
  8. app = shinyApp(
  9.   ui = shinyUI(fluidPage(
  10.    titlePanel("dynamic renderUI test"),
  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.     vars = names(df)[-5]
  22.     vars_selectChoice = lapply(vars, function(name) c("all", sort(unique(df[[name]]))))
  23.     names(vars_selectChoice) = vars
  24.     df_update = reactive({
  25.       out = df
  26.       for (name in vars){
  27.         if (input[[name]] != "all")
  28.           out = out[out[[name]] == input[[name]],]
  29.       }
  30.       return(out)
  31.     })
  32.     updateInput_f = function(penal_name){
  33.       vars_tmp = setdiff(vars, penal_name)
  34.       for (name in vars_tmp){
  35.         vars_selectChoice[[name]] = c("all", sort(unique(df_update()[[name]])))
  36.         updateSelectInput(session, name, choice = vars_selectChoice[[name]], selected = input[[name]])
  37.       }
  38.     }
  39.     brand = observe({
  40.        input$brand
  41.        isolate(updateInput_f("brand"))
  42.     })
  43.     model = observe({
  44.        input$model
  45.        isolate(updateInput_f("model"))
  46.     })
  47.     tax = observe({
  48.        input$tax
  49.        isolate(updateInput_f("tax"))
  50.     })
  51.     seller = observe({
  52.        input$seller
  53.        isolate(updateInput_f("seller"))
  54.     })
  55.     output$table = renderDataTable(df_update())
  56.   }
  57. )
  58. runApp(app)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement