How to hide/toggle legends based on addLayerControl() in Leaflet for R

前端 未结 2 1233
甜味超标
甜味超标 2021-01-02 20:17

I\'m wondering how to hide/toggle legends based on addLayerControl() in Leaflet for R? When option layer a is toggled, then the data of option layer b is not showed by addPo

2条回答
  •  生来不讨喜
    2021-01-02 20:45

    This code worked for me:

    observeEvent(input$mymap_groups,{
        mymap <- leafletProxy("mymap", data = SalesMap)
        mymap %>% clearControls()
        if (input$mymap_groups == '1') {
          mymap %>% addLegend(position="bottomright", pal=pal1, values=SalesMap$SALES, title="a")
        }
        else if (input$mymap_groups == '2') {
          mymap %>% addLegend(position="bottomright", pal=pal2, values=SalesMap$Bonnen, title="b")
        }
      })
    

    You can use input$mymap_groups to identify what kind of group is selected.In the observeEvent() you can use an if/else statement to create a legend based on a group.

提交回复
热议问题