Shape click event with multiple layers

I am trying to create an interactive online map with two visualizations of the same water quality data. One version has a specific color for each shapefile class and the other is symbolized by regulatory status. I used the shape_click tag in shiny to access the layerId of the shapefile and pass it to functions that produce text, plots, and tables interactively. Everything works except that I can only assign a layerId to one of the shapefiles I have as an overlay. If that layer is deselected, the shape_click function does not work. I would like users to be able to be able to select from either layer to produce the graphs and tables. I have tried many workarounds, but it seems as if leaflet in shiny can only assign a layerId to pass to a shape_click function to one layer. A potential solution I thought may work is adding a radio button so only one overlay is visible at a time, but it seems r leaflet only allows that option for base layers, not overlay layers.

My server code is below, but since I use shapefile data and multiple helper functions, I don't think I can add the data to reproduce. Any suggestions would be appreciated. Thank you!

server <- function(input, output) {
  # render leaflet map with shapefile and options for basemaps
  factpal <- colorFactor(rainbow(36), seg_shp$AUID)
  newpal <- colorFactor(palette = c("orange","yellow","yellow","green"), levels = c("5","3a","3b","1"))
  bounds <- bbox(seg_shp)
  
  output$mymap <- renderLeaflet(
      leaflet(data = seg_shp, options = leafletOptions(minZoom = 9)) %>%
        addProviderTiles("Esri.WorldTopoMap", group = "Topo") %>% 
        addProviderTiles("Esri.WorldImagery", group = "Imagery") %>%
        addProviderTiles(providers$Stamen.Terrain, group = "Terrain") %>%
        # add polyline layer with individual segments
        addPolylines(layerId = ~AUID,label = ~PortionDes, weight = 3, group = "Segments", color = ~factpal(AUID),
                     highlightOptions = highlightOptions(color = "mediumseagreen",opacity = 1.0,weight = 3,bringToFront = TRUE)) %>%
        # add polyline layer with regulatory status codes
        addPolylines(label = ~PortionDes, weight = 3, group = "Impairment", fillOpacity = 1, color = ~newpal(Cat),
                     highlightOptions = highlightOptions(color = "mediumseagreen",opacity = 1.0,weight = 3,bringToFront = TRUE)) %>%
        # set boundaries for viewing
        setMaxBounds( lng1 = bounds[1,1] - 1
                      , lat1 = bounds[2,1] - 1
                      , lng2 = bounds[1,2] + 1
                      , lat2 = bounds[2,2] + 1) %>% 
      # Layers control
      addLayersControl(
        baseGroups = c("Topo", "Imagery", "Terrain"),
        overlayGroups = c("Segments", "Impairment"),
        options = layersControlOptions(collapsed = FALSE)) %>% 
      addLegend(title = "Regulatory status",
                colors = c("orange","yellow","green"),
                labels = c("5: (303(d) Impaired)", "3a/b: (Not enough information/M & E)","1: All attaining standards"),
                group = "Impairment") %>% 
      hideGroup(group = "Impairment")  
  )
  
  # selec segment by map shape click, return text
  observe({
    click = input$mymap_shape_click
    seg = input$mymap_shape_click$id
    if(is.null(click))
      output$temp <- renderText({
        "No segment selected. Click a segment to initiate program."})
    else
      output$temp <- renderText({
        paste("Segment:", seg)
      })
  })
  
  # render interactive useclass, category, and water quality parameter selection
  df0 <- eventReactive(input$useclass,{
    wqStandards %>% filter(UseClass %in% input$useclass)
  })
  output$category <- renderUI({
    selectInput("category","Choose category",sort(unique(df0()$Category)))
  })
  
  df1 <- eventReactive(input$category,{
    df0() %>% filter(Category %in% input$category)
  })
  
  output$indicator <- renderUI({
    selectInput("indicator","Desired water quality parameter",sort(unique(df1()$Indicator)))
  })
  # render boxplot with interactive data
  output$boxplot <- renderPlot({
    buildBoxplot(watershedData, input$useclass, input$indicator, input$category, input$mymap_shape_click$id, input$daterange[1], input$daterange[2])
  })
  # render scatterplot with interactive data
  output$scatterplot <- renderPlot({
    buildScatterplot(watershedData, input$useclass, input$indicator, input$category, input$mymap_shape_click$id, input$daterange[1], input$daterange[2])
  })
  # render table with interactive data
  output$table <- renderFormattable({
    getSummaryStats(watershedData, input$useclass, input$indicator, input$category, input$mymap_shape_click$id, input$daterange[1], input$daterange[2])
  })

}
1 Like

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.