9

I am making an R leaflet map (not Shiny) and I have two control groups, and based on the selection I would like a different legend to become visible. Currently I only manage to have both legends visible at all time.

Below is the code for the leaflet map, and the output can be seen in the image.

Two legends are always visible

leaflet() %>% addSearchOSM() %>% 
  addProviderTiles(providers$CartoDB.Positron,
                   options = providerTileOptions(noWrap = TRUE),
                   group = "kaart") %>%
  # addFullscreenControl() %>%
  addCircleMarkers(data = table@data,
             lat = ~lng, 
             lng = ~lat,
             color = ~palverbruikplaats(Verbruiksplaats),
             label = bepaalPopup(),
             group = "Verbruikplaatscircles"
             )%>%
  addCircleMarkers(data = table@data,
                   lat = ~lng, 
                   lng = ~lat,
                   color = ~palstatus(`Status omschrijving`),
                   label = bepaalPopup(),
                   group = "statuscircles"
                    )%>%
  leaflet::addLegend("bottomleft", pal = palverbruikplaats, values = verbruikplaatsuniek, title = "Legenda") %>%
  leaflet::addLegend("bottomleft", pal = palstatus, values = statusuniek, title = "Legenda") %>%
  addLayersControl(baseGroups = c("Verbruikplaatscircles", "statuscircles"),
                      options = layersControlOptions(collapsed = FALSE))
Younes
  • 129
  • 1
  • 6

2 Answers2

7

In your addLayersControl did you mean to set the overlayGroups argument instead of baseGroups?

library(leaflet)
leaflet() %>%
  addTiles(group = "OpenStreetMap") %>%
  addCircleMarkers(runif(20, -75, -74), runif(20, 41, 42), group = "Markers1", color ="red") %>%
  addMarkers(runif(20, -75, -74), runif(20, 41, 42), group = "Markers2") %>%
  addLegend(values = 1, group = "Markers1", position = "bottomleft", labels = "1", colors= "red") %>%
  addLegend(values = 2, group = "Markers2", position = "bottomleft", labels = "2" ,colors= "blue") %>%  
  addLayersControl(overlayGroups = c("Markers1", "Markers2"),
                   options = layersControlOptions(collapsed = FALSE))

enter image description here

Weihuang Wong
  • 12,868
  • 2
  • 27
  • 48
0

what you need to do is, you need to make your legends values reactive

addLegend("bottomright", pal = pal, values = maindata@data[,req_var1()],

you can declare the req_var1() in server before calling

req_var1<-reactive({if(input$`Comparison Metric`=="Current Territory Factors vs GeoProxy Smoothing"){
    paste(input$Curr2,"Curr",sep="_")
  } else if(input$`Comparison Metric`=="Current Written Premium Vs Indicated Written Premium"){
    paste(input$Curr2,"CWP",sep="_")
  }
  }) 

and also the pal can be declared as

pal1 <- reactive({if(input$ColorType=="Percentile"){

    colorQuantile(
    palette = "Spectral",
    domain = tempdata()@data[,req_var1()],
    probs = if(input$`Comparison Metric`=="Current Territory Factors vs GeoProxy Smoothing"){seq(0,1,by=0.25)
    } else if(input$`Comparison Metric`=="Current Written Premium Vs Indicated Written Premium"){
      seq(0,1,by=0.5)
    }
    ## In case of Current written premium the variation is very less so while executing color mapping code is throwing error.
    ## This is because the some of quantiles values are not differentiable.
    ## So in colorQuantile function we have given two different prob values depending on metric selection.
    ) 
  } else if(input$ColorType=="Absolute Value"){colorNumeric(
    palette = "Spectral",
    domain = tempdata()@data[,req_var1()])
  }else{print("Plese select Any one color map")}
  }) 
Sovik Gupta
  • 147
  • 2
  • 13
  • 2
    For people looking at this answer: this has the advantage of keeping the layers as `baseGroups()` (radio buttons) BUT only when leaflet is used in `shiny`. – YGS Feb 14 '19 at 20:56
  • 1
    @Sovik Gupta: Your answer is not totally clear. Can you add a working example of this, please? – Chintan Pathak Jul 16 '19 at 01:34