lundi 20 mai 2019

I wanna make my facet_grid optionnal by a checkbox in shiny

I am building an application using shiny. The main point is just showing the user the sum of the movement with certain filters. I actually have some option that you can chose and the graph change in function. Now I want to go a step further and let the person choose if she want those options appears as a facet grid. There is 3 category that can appears in a facetgrid : operator, segment and region

this is how my dataset looks :

[enter image description here][1]

and this is my code :

region <-c('Wallonia', 'Brussels','Flanders')
tms <- c('OthersB2C', 'OthersB2B', 'Eagle', 'Panther', 'Dolphin', 'Koala', 'Cheetah','Shape Intense', 'Shape Ultimate', 'Shape Traveller', 'Shape Plus', 'Shape Light', 'Shape Basic')
opers <- c('Others', 'Proximus', 'Telenet', 'Base')
segm <- c('CMA', 'MASS', 'SMA', 'SOHO')

ui <- fluidPage( 
   titlePanel("MNP Movements"),
        mainPanel(
            dropdownButton(
                tags$h3("List of Input"),
                dateRangeInput("daterange", label = "Choose a date range:",
                                             start  = "2017-06-28",
                                             end    = "2018-03-20",
                                             min    = "2016-06-30",
                                             max    = "2019-04-30",
                                             format = "dd/mm/yy",
                                             separator = " - "),

                prettyCheckboxGroup("operators","Choose an operator",
                                                        choices = opers,
                                                        selected = "Proximus", inline = TRUE, outline = TRUE, 
                                                        animation = "smooth", shape = "round",  fill = TRUE, bigger = TRUE),
                prettyCheckboxGroup("segment","Choose an operator",
                                                        choices = segm,
                                                        selected = "MASS", inline = TRUE, outline = TRUE, 
                                                        animation = "smooth", shape = "round",  fill = TRUE, bigger = TRUE),
                prettyCheckboxGroup("tmg","Choose an operator",
                                                        choices = tms,
                                                        selected = "Koala", inline = TRUE, outline = TRUE, 
                                                        animation = "smooth", shape = "round",  fill = TRUE, bigger = TRUE),
                prettyCheckboxGroup("reg","Choose an operator",
                                                        choices = region,
                                                        selected = "Brussels", inline = TRUE, outline = TRUE, 
                                                        animation = "smooth", shape = "round",  fill = TRUE, bigger = TRUE),
                prettyCheckboxGroup("facet","Comparison?",
                                                        choices = c("REGION", "SEGMENT", "OPERATOR"),
                                                        selected = "REGION", inline = TRUE, outline = TRUE, 
                                                        animation = "smooth", shape = "round",  fill = TRUE, bigger = TRUE),
                circle = TRUE, status = "danger", icon = icon("gear"), width = "300px",
                tooltip = tooltipOptions(title = "Click to see inputs !"))

      ),

      mainPanel(
         plotOutput("mnp_raw")
      )
   )


server <- function(input, output) {

   output$mnp_raw <- renderPlot({

    mnp <- mnp_raw %>% 
        filter(OPERATOR%in%input$operators,
                     PORT_DT > input$daterange[1],
                     PORT_DT < input$daterange[2])%>% 
        filter(SEGMENT%in%input$segment,
                     PORT_DT > input$daterange[1],
                     PORT_DT < input$daterange[2])%>% 
        filter(TARIFF_MODEL_GROUP%in%input$tmg,
                     PORT_DT > input$daterange[1],
                     PORT_DT < input$daterange[2])%>% 
        filter(REGION%in%input$reg,
                     PORT_DT > input$daterange[1],
                     PORT_DT < input$daterange[2])%>% 
        group_by(PORT_DT) %>% 
        summarise(total = sum(MOVEMENT))

    ggplot(mnp,  aes(x = PORT_DT, y = total))+
        geom_bar(stat="identity")
   })
}

shinyApp(ui = ui, server = server)

As you can see my comparison button doens't work. I want that the user press on what he want to compare (region, operator or segment) and if he didn't press it, it's just a normal sum of everything.

Please can someone help me? 




Aucun commentaire:

Enregistrer un commentaire