简体   繁体   中英

Get "ALL" value in selectinput to plot with multi-filters (shiny)

I would like to get a reactive plot with multiple filters and an initial minimal plot with the number of ALL diagnostics by month.

The aim requires 4 filters AND a scale of period (display data by week,month ..).

I tried but not it's not working on the step to sum data (ALL). If you have a suggestion I am open . Thank you :)

library(shiny)
library(ggplot2)
library(dplyr)

#data
data.hosp <- data.frame(stringsAsFactors=FALSE,
                    id= c(1,2,3,4,5),
                  Annee = c(2018, 2018, 2018, 2018, 2018),
                  Mois = c("2018-01","2018-01","2018-02","2018-03","2018-03"),
                  Semaine = c("2018-001","2018-003","2018-008","2018-011","2018-013"),
                  uf= c("A3352","Z6687", "A3352", "A3352", "Z6687"),
                  um= c(3350,6687, 3352, 3350, 6687),
                  ghm= c("AAAAA","DFFDF","DDFDA","AZEEA","DDFDA"),
                  diag= c("A","A","C","Z","R"),
                  nb=c(1,3,1,1,10))

  ui <- fluidPage(
          titlePanel("Plot with filters"),
        
        fluidRow(
          column(2,
                 selectInput(inputId = "sel_uf",
                             label = "UF",
                             choices = as.character(unique(data.hosp$uf)), 
                             multiple=TRUE,
                             width = validateCssUnit(200))),
           column(2,
                 selectInput(inputId = "sel_um", 
                             label = "UM",
                             choices = as.character(unique(data.hosp$um)), 
                             multiple=TRUE,
                             width = validateCssUnit(200))),
          column(3,
                 selectInput(inputId = "sel_ghm", 
                             label = "GHM",
                             choices =as.character(unique(data.hosp$ghm)), 
                             multiple=TRUE,
                             width = validateCssUnit(200))),
          column(3,
                 selectInput(inputId = "sel_diag", 
                             label = "Diagnostic",
                             choices =c('Tous', as.character(unique(data.hosp$diag))), 
                             multiple=TRUE,
                             width = validateCssUnit(250))),
          column(4,  selectInput("periodec",
                      label="Affichage par :",
                      choices = c("Semaine"= "Semaine",
                                "Mois"   = "Mois",
                                "Année"  = "Annee"),
                    selected   = "Mois" ) ),
          
          plotOutput("graph1", height=300 )
))


server <- function(input, output) {
  
  #Period filter (display by :)
  periode <- reactive({
    ifelse(input$periodec=="Semaine", "Semaine",
    ifelse(input$periodec=="Mois", "Mois",
    ifelse(input$periodec=="Année", "Annee")))     })
  
 
  # Data filters
  df_dat <- reactive({        
    df_dat  <- data.hosp

  #code to get "ALL" in selectsizeInput 
    if ('Tous' %in% input$sel_diag) { sel_diag <- unique(data.hosp$diag)
    } else {
      data.hosp <- data.hosp %>% filter(nb == input$sel_diag)
      nb <- unique(data.hosp$nb) }

    if (!is.null(input$sel_um)) {
      df_dat  <- df_dat  %>% filter(um == input$sel_um)   %>% group_by_(periode()) %>% summarise(sum_active = sum(nb))}
    if (!is.null(input$sel_uf)) {
      df_dat  <- df_dat  %>% filter(uf == input$sel_uf)   %>% group_by_(periode()) %>% summarise(sum_active = sum(nb))}
    if (!is.null(input$sel_ghm)) {
      df_dat  <- df_dat  %>% filter(ghm == input$sel_ghm)   %>% group_by_(periode()) %>% summarise(sum_active = sum(nb)) }
    if (!is.null(input$sel_diag)) {
      df_dat  <- df_dat  %>% filter(diag == input$sel_diag)   %>% group_by_(periode()) %>% summarise(sum_active = sum(nb)) }
    return(df_dat)
  })
  
  
  # Ensures that our filter works properly
  observe(print(str(df_dat())))
  # graph 
  output$graph1 <- renderPlot({
    req(df_dat())
    ggplot(df_dat(), aes_string(x =periode(), y = "sum_active",  group = factor(periode() ))) +
             geom_bar(aes_string(periode(), "sum_active"), stat = "identity", fill="steelblue")
  }) 
} 
shinyApp(ui,server)

Maybe this is what you are looking for. In my opinion the main issue is that your approach was overly complicated. (;

  1. My approach sets all filters first. If NULL (and/or 'Tous') the "selection" is set to all categories of a variable, otherwise only the chosen ones are included.

  2. Doing so you only need one pipe to filter and summarise the data

  3. There is no need for the observe statement which I dropped

  4. I fixed the req in the renderPlot to check wether the filtered data contains any rows. Otherwise ggplot will raise an error.

     server <- function(input, output) { #Period filter (display by :) # While a nested ifelse works in the present case a better choice is using `if periode <- reactive({ if (input$periodec == "Semaine") { "Semaine" } else if (input$periodec == "Mois") { "Mois" } else { "Annee" } }) df_dat <- reactive({ # First: Setup the filters. If NULL: all categories else: chosen ctaegories # In case of diag additionally check for "Tous" sel_diag <- if (is.null(input$sel_diag) | 'Tous' %in% input$sel_diag) unique(data.hosp$diag) else input$sel_diag sel_um <- if (is.null(input$sel_um)) unique(data.hosp$um) else input$sel_um sel_uf <- if (is.null(input$sel_uf)) unique(data.hosp$uf) else input$sel_uf sel_ghm <- if (is.null(input$sel_ghm)) unique(data.hosp$ghm) else input$sel_ghm # Filter the data and summarise data.hosp %>% filter(diag %in% sel_diag, um %in% sel_um, uf %in% sel_uf, ghm %in% sel_ghm) %>% group_by_(periode()) %>% summarise(sum_active = sum(nb), .groups = "drop") }) # graph output$graph1 <- renderPlot({ # Plot only if any data req(nrow(df_dat()) > 0) ggplot(df_dat(), aes_string(x = periode(), y = "sum_active", group = factor(periode() ))) + geom_bar(aes_string(periode(), "sum_active"), stat = "identity", fill="steelblue") }) }

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM