Leaflet proxy in r only shows latest added Layer

I want to visualise the development of fatal traffic accidents and accidents with severe injuries on state and community level in a leaflet map.
As a solution i use a shiny app with a radio button for switching between state and community level and leaflet base Layer Groups to switch between visualisation of fatalities or severe injuries.In addition there is a checkbox to select different clusters of communities.
For communities everything works fine, but for states it seems like only the last added layer in leaflet proxy is shown. When i switch to the fatalities of state level nothing is shown. If i change the order accidents with severe injuries for state level is not shown.
I tried to seperate the leaflet proxy in two observe blocks, but nothing seems to work.

library(sf)
library(tidyverse)
library(leaflet)
library(shiny)
library(leaflet.extras)
library(ggplot2)
library(htmlwidgets)
library(dplyr)


readRDS(land, "stackoverflow_state.rds")
readRDS(communities, "stackoverflow_communities.rds")

pal_leaf_heavy= colorFactor(palette = c("red", "yellow", "green", "darkgreen"), 
                      levels = c(4, 3,2,1))

pal_leaf_fatal = colorFactor(palette=c("yellow", "green", "darkgreen"),
                             levels=c(3,2,1))


ui = fluidPage(
  sidebarLayout(position = "left",
                sidebarPanel(
                  checkboxGroupInput("regiostar_selection", "Regiostarklasse", 
                                     choices=c(51,52,53,54),
                                     selected=c(51,52,53,54),
                                     inline=TRUE),
                  radioButtons("level", "Ebene: ", choices=c("Gemeinde", "Bundesland"), inline=TRUE, selected="Gemeinde")
                ),
                mainPanel( width=12,
                             column(8, leafletOutput("map", height="85vh"))
                           )
                )
  )

server <- function(input, output, session){
  f_communities <- reactive({
    if(length(input$regiostar_selection) > 0){
      communities %>% filter(RegioStaRGem5 %in% input$regiostar_selection)
    }
    else{
      communities
    }
  })
  
  
  level_data <- reactive({
    switch(input$level,
           "Gemeinde" = f_communities(),
           "Bundesland" = land
    )
  })
  
  output$map=renderLeaflet({leaflet() %>% addTiles() %>% 
      addPolygons(data=communities, layerId =communities$AGS, col=pal_leaf_heavy(communities$heavy_rating), label=communities$GEN, group="Entwicklung Unfaelle mit Schwerstverletzten") %>%
      addPolygons(data=communities, col=pal_leaf_fatal(communities$fatal_rating), group="Entwicklung toedliche Unfaelle", label=communities$GEN,
                  layerId=communities$AGS) %>%
      addLegend(colors=c("darkgreen","lightgreen", "yellow", "red"), 
                labels = c("Zielwert für 2030 erreicht", "Jahresziel erreicht", "Zahlen sind gesunken seit 2019, aber Jahresziel nicht erreicht", "Zahlen seit 2019 nicht gesunken" ), 
                group="Legend Unfaelle mit Schwerstverletzten") %>%
      addLegend(colors=c("darkgreen", "lightgreen", "yellow"),
                labels=c("Vision Zero erreicht", "Zielwert für 2030 erreicht", "Ziel noch nicht erreicht"),
                group="Legend toedliche Unfaelle") %>%
      addLayersControl(baseGroups = c("Entwicklung Unfaelle mit Schwerstverletzten", "Entwicklung toedliche Unfaelle"),
                       overlayGroups = c("Legend Unfaelle mit Schwerstverletzten", "Legend toedliche Unfaelle")) %>%
      addSearchOSM(options=searchOptions(collapsed = FALSE, zoom = 9))
  })
  
  
  
  
  observe({
    leafletProxy("map", data=level_data()) %>% clearGroup(group="Entwicklung toedliche Unfaelle") %>%
      addPolygons(data=level_data(), col=pal_leaf_fatal(level_data()$fatal_rating), group="Entwicklung toedliche Unfaelle", label=level_data()$GEN,
                  layerId=level_data()$AGS) %>%
      clearGroup(group="Entwicklung Unfaelle mit Schwerstverletzten") %>%
      addPolygons(data=level_data(), layerId =level_data()$AGS, col=pal_leaf_heavy(level_data()$heavy_rating), label=level_data()$GEN, group="Entwicklung Unfaelle mit Schwerstverletzten")
    
    
  })
  
  
}

shinyApp(ui=ui,server=server)

Leave a Comment