'Error in R when changing my select due to an action button

I decided to make a plot where the user can choose London districts and a bar plot shows the number of accidents of selected districts. The plot was working perfectly well but was laggy for each change due to the high number of districts (33) so I decided to add an action button. The button is working but when I first run the app, no plot shows. To resolve this, I read on other subjects that I need to render a default plot, do I need to use an if condition, like mentioned in this post : In R Shiny App, how to render a default table when first invoking the App?. Would that work here ?

My second issue is that when I change the selected districts in my selectInput, an error shows until I click my action button. I read that to remove this error I should use either an isolate, a req or put my plot code staight in the eventReactive to solve this but I couldn't figure out how ?

Here's the plot after clicking the update button :

Plot

Here's what happens when I switch the selected districts before I click the update button again (translate to "arguments must be of same length"):

Error message

Here is the code I used for this plot :

Dashboard body :

 column(width=6,
               box(width=12,
                   solidHeader = TRUE,
                   status = "warning",
                   title = "Accidents en fonction des districts",
                   p("Cliquez sur le bouton Update pour mettre à jour le graphique !"),
                   ggiraphOutput(outputId ="choixdistricts"),
                   selectInput(inputId="selectDistrict",
                               label="Sélection des quartiers",
                               choices = unique(Data_vis$District),
                               multiple=TRUE,
                               selected = c("City of London", "Westminster", "Camden")),
                   actionButton(inputId = "update",
                                label = "Update"))
              )

In server function :

   output$choixdistricts <- renderggiraph({

    p2 <- Data_vis%>%
      group_by(District) %>% 
      filter(Year == 2018, District %in% input$selectDistrict) %>%
      summarise(Total = mean(Total)) %>%
      ggplot(aes(x=reorder(factor(selectDistrictInput()), Total), y=Total, fill = District,ymax=1850, tooltip=paste(District," : ",Total))) + 
      geom_bar_interactive(stat = "identity") +
      guides(fill = FALSE) +
      xlab("Nom des quartiers") + 
      ylab("Nombre d'accidents en 2018") + 
      ggtitle("Nombre d'accident en 2018 selon les quartiers") + 
      theme(axis.title.y = element_text(size=14), axis.text.y = element_text(size = 14), axis.title.x = element_text(size=14),axis.text.x = element_blank(), plot.title = element_text(size=14, hjust = 0.5))
    
    
    gp2 <- girafe(ggobj = p2)
    
    girafe_options(x = gp2,
                   opts_hover(css = "stroke:blue;stroke-width:2px"),
                   opts_selection(type = "none"))     
  })
  
  selectDistrictInput <- eventReactive(input$update, {
    input$selectDistrict
  })


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source