R leaflet map - Change legends based on selected layer group

微笑、不失礼 提交于 2019-12-22 06:01:15

问题


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.

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))

回答1:


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))




回答2:


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")}
  }) 


来源:https://stackoverflow.com/questions/50641092/r-leaflet-map-change-legends-based-on-selected-layer-group

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!