Don´t change ggplot dependent many selectinputs

Hi I want to do the following:

1.Have a ggplot whose values change depending on a 3 selectinput, but when I choose the third selectinput don´t change ggplot, only first and second.

This is my code:

library(foreign)
library(shiny)
library(leaflet)
library(quantmod)
library(readxl)
library(sf)
library(dplyr)
library(ggplot2)
library(tidyverse)
library(datasets)

setwd("/Users/rmalavers/documents/salasitua/")

so5_df <- read_xlsx("~/salasitua/noti_spco.xlsx",1)
so4_df <- read.dbf("~/salasitua/diagno.DBF")
so3_df <- read_xlsx("~/salasitua/renoti.xlsx")


ui <- fluidPage(
  
 
  
  # Titulo de la APP
  
  titlePanel("Sala Situacional"),
  
  sidebarLayout(
    sidebarPanel(
      helpText("" ),
      
      
      selectInput("var",
                  label = "Selecciona la Red:",
                  choices = c("CONTUMAZA",
                              "CAJAMARCA",
                              "CELENDIN",
                              "SAN MARCOS",
                              "CAJABAMBA",
                              "SAN MIGUEL",
                              "SAN PABLO",
                              "CHOTA",
                              "BAMBAMARCA",
                              "SANTA CRUZ",
                              "CUTERVO",
                              "JAEN",
                              "SAN IGNACIO",
                              "NO PERTENECE A NINGUNA RED"),
                  selected = "CONTUMAZA"),
      
      selectInput("var1",
                  label = "Selecciona la Microred:",
                  choices = NULL),
      
      selectInput("var2",
                  label = "Selecciona el Establecimiento de Salud:",
                  choices = NULL),
      
      
      
      
    ),  
    
    mainPanel(
      
     
      br(),
      
      plotOutput("plot")
     )
    
    
  )  
)

# Server--------------------------

print(str(so3_df))
print(str(so5_df))
print(str(so4_df))

shinyServer <- function(input, output, session) {
  
  
  observe({
    
    print(input$var)     
    
    x <- so3_df$Microred[so3_df$Red == input$var]
    print(x)
    updateSelectInput(session,"var1","Selecciona una Microred",choices = unique(x) )
    
    
    
  })
  
  
  output$plot <- renderPlot({
    
    filtro <- so5_df %>%
      
      select(DIAGNOSTIC,SEMANA) %>%
      filter(so5_df$MICRORED == input$var1) 
    
    
    
    ggplot(filtro) +
      geom_bar(mapping = aes(x = SEMANA, fill = DIAGNOSTIC), position = "dodge")
    
  })   
  
  
  observe({
    
    relaeess <- so3_df$Nom_est[so3_df$Microred == input$var1]
    updateSelectInput(session,"var2","Selecciona un EESS",choices = relaeess )
    
    
    
  })
  
  
  # Here is the problem, if hide so5_df$establecimiento == input$var2 display the ggplot--------------------------
  
   output$plot <- renderPlot({

   filtro1 <- so5_df$red == input$var & so5_df$microred == input$var1 & so5_df$establecimiento == input$var2

      ggplot(filtro1)
      geom_bar(mapping = aes_string(x = SEMANA, fill = DIAGNOSTIC), position = "dodge")

  })

  
  
# llamada Shiny--------------------------

shinyApp(ui = ui, server = shinyServer)

Hi Raquel, welcome!

Could you please turn this into a proper REPRoducible EXample (reprex) as explained in this link? A reprex makes it much easier for others to understand your issue and figure out how to help.

1 Like