Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(shiny)
- df <- data.frame(
- brand=c("a","a","a","a","a","a","b","b","b","b","b","c","c","c","c","c"),
- model=c("a21","a21","a21","a21","a31","a31","b55","b55","b56","b56","b56","c33","c34","c34","c33","c34"),
- tax=c("y","x","y","x","x","y","x","x","x","y","y","x","y","x","y","y"),
- seller=c("B","B","B","C","C","D","A","D","B","C","C","B","A","C","C","B"),
- price=c(1000,1100,1500,1400,1200,1300,2000,2200,1900,1800,1700,800,900,850,950,880), stringsAsFactors = F)
- app = shinyApp(
- ui = shinyUI(fluidPage(
- titlePanel("hirearchy data group"),
- sidebarLayout(
- sidebarPanel(
- selectInput("brand",label="choice the brand",choices=c("all", sort(unique(df$brand)))),
- selectInput("model",label="choice the model",choices=c("all",sort(unique(df$model)))),
- selectInput("tax",label="choice the tax",choices=c("all", sort(unique(df$tax)))),
- selectInput("seller",label="choice the seller",choices=c("all", sort(unique(df$seller))))
- ),
- mainPanel(dataTableOutput("table"))
- ))),
- server = function(input, output, session) {
- vals = reactiveValues()
- vals$df = df
- vals$brand = "all"
- vals$model="all"
- vals$tax = "all"
- vals$seller = "all"
- updateInput_f = function(panel_name){
- choice = c("all", sort(unique(vals$df[[panel_name]]))) #[[]]變成字串
- updateSelectInput(session, panel_name, choice = choice, selected = input[[panel_name]])
- }
- updateDF_f = function(){
- vals$df = df
- if (vals$brand != "all")
- vals$df = vals$df[vals$df$brand == vals$brand,]
- if (vals$model != "all")
- vals$df = vals$df[vals$df$model == vals$model,]
- if (vals$tax != "all")
- vals$df = vals$df[vals$df$tax == vals$tax,]
- if (vals$seller != "all")
- vals$df = vals$df[vals$df$seller == vals$seller,]
- }
- brand = observe({
- input$brand
- isolate({
- vals$brand = ifelse(input$brand == "all", "all", input$brand)
- updateDF_f()
- #updateInput_f("brand")
- updateInput_f("model")
- updateInput_f("tax")
- updateInput_f("seller")
- })
- })
- model = observe({
- input$model
- isolate({
- vals$model = ifelse(input$brand == "all", "all", input$model)
- updateDF_f()
- updateInput_f("brand")
- #updateInput_f("model")
- updateInput_f("tax")
- updateInput_f("seller")
- })
- })
- tax = observe({
- input$tax
- isolate({
- vals$tax = ifelse(input$tax == "all", "all", input$tax)
- updateDF_f()
- updateInput_f("brand")
- updateInput_f("model")
- #updateInput_f("tax")
- updateInput_f("seller")
- })
- })
- seller = observe({
- input$seller
- isolate({
- vals$seller = ifelse(input$seller == "all", "all", input$seller)
- updateDF_f()
- updateInput_f("brand")
- updateInput_f("model")
- updateInput_f("tax")
- #updateInput_f("seller")
- })
- })
- output$table = renderDataTable({vals$df})
- }
- )
- runApp(app)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement