R leaflet - Show/Hide addControl() element with group layers

Tom Newton

I have a leaflet map which uses a custom legend using HTML and added using the addControl function (following: Leaflet Legend for Custom Markers in R).

However, I only want the legend to show when one group is shown, I have tried using the argument group = "group name"which doesn't work with the addControl function. I've also tried using layerId arguments but without success.

Any ideas?

Reproducible example:

library(leaflet)
# Sample Data
data(quakes)
quakes <- quakes[1:10,]

# Choose Icon:
leafIcons <- icons(
  iconUrl = ifelse(quakes$mag < 4.6,
               "http://leafletjs.com/docs/images/leaf-green.png",
               "http://leafletjs.com/docs/images/leaf-red.png"
 ),
  iconWidth = 38, iconHeight = 95,
  iconAnchorX = 22, iconAnchorY = 94)

html_legend <- "<img src='http://leafletjs.com/docs/images/leaf-
green.png'>green<br/>
<img src='http://leafletjs.com/docs/images/leaf-red.png'>red"

# Produce Map:
leaflet(data = quakes) %>% addTiles() %>%
  addMarkers(~long, ~lat, icon = leafIcons, group = "Group A") %>%
  addMarkers(~long, ~lat, icon = leafIcons, group = "Group B") %>%
  addControl(html = html_legend, position = "bottomleft") %>%
  addLayersControl(position = "topleft", overlayGroups = c("Group A","Group B"))

Where I would like the addControl html_legend element to only be visible when Group A is visible.

SeGa

Alright, now I think i understand your problem. Below is another example, which shows only the legend and control of the active groups. For that, I created 2 html_legends for group A and for group B.

library(shiny)
library(leaflet)

html_legend_A <- "<img src='http://leafletjs.com/docs/images/leaf-green.png'>green<br/>"
html_legend_B <- "<img src='http://leafletjs.com/docs/images/leaf-red.png'>red<br/>"

ui <- fluidPage(
  leafletOutput("map")
)
server <- function(input, output, session){
  output$map <- renderLeaflet({
    map <- leaflet(data = quakes) %>% addTiles() %>%
      addMarkers(~long, ~lat, icon = leafIcons, group = "Group A", layerId = "A") %>%
      addMarkers(~long, ~lat, icon = leafIcons, group = "Group B", layerId = "B") %>%
      addLayersControl(position = "topleft", overlayGroups = c("Group A","Group B"))
    map
  })

  observe({
    map <- leafletProxy("map") %>% clearControls()
    if (any(input$map_groups %in% "Group A")) {
      map <- map %>% 
        addControl(html = html_legend_A, layerId = "A", position = "bottomleft") %>% 
        addLegend(title="Group A", position="bottomright", opacity=1, colors="green",labels = "Group A")}
    if (any(input$map_groups %in% "Group B")) {
      map <- map %>% 
        addControl(html = html_legend_B, layerId = "B", position = "bottomleft") %>% 
        addLegend(title="Group B", position="bottomright", opacity=1,colors="red",labels = "Group B")}
  })
}

shinyApp(ui, server)

When using the LayerId argument, it only shows 1 marker per group. If you want to see all markers, the LayerId argument should not be given. I made you another example. I think this should be right now :) I also create 2 icons and I am filtering the quakes data, based on the mag-column inside the renderLeaflet function, as you do in the icon assignment.

library(shiny)
library(leaflet)

data(quakes)
quakes <- quakes[1:10,]

leafIcons_A <- icons(
  iconUrl = "https://leafletjs.com/examples/custom-icons/leaf-green.png",
  iconWidth = 38, iconHeight = 95,
  iconAnchorX = 22, iconAnchorY = 94)
leafIcons_B <- icons(
  iconUrl = "https://leafletjs.com/examples/custom-icons/leaf-red.png",
  iconWidth = 38, iconHeight = 95,
  iconAnchorX = 22, iconAnchorY = 94)

html_legend_A <- "<img src='https://leafletjs.com/examples/custom-icons/leaf-green.png'>green<br/>"
html_legend_B <- "<img src='https://leafletjs.com/examples/custom-icons/leaf-red.png'>red<br/>"

ui <- fluidPage(
  leafletOutput("map")
)
server <- function(input, output, session){
  output$map <- renderLeaflet({
    dataA <- quakes[quakes$mag < 4.6,]
    dataB <- quakes[quakes$mag > 4.6,]

    map <- leaflet() %>% addTiles() %>%
      addMarkers(dataA$long, dataA$lat, icon = leafIcons_A, group = "Group A") %>%
      addMarkers(dataB$long, dataB$lat, icon = leafIcons_B, group = "Group B") %>%
      addLayersControl(position = "topleft", overlayGroups = c("Group A","Group B"))
    map
  })

  observe({
    map <- leafletProxy("map") %>% clearControls()
    if (any(input$map_groups %in% "Group A")) {
      map <- map %>%
        addControl(html = html_legend_A, position = "bottomleft") %>%
        addLegend(title="Group A", position="bottomright", opacity=1, colors="green",labels = "Group A")}
    if (any(input$map_groups %in% "Group B")) {
      map <- map %>%
        addControl(html = html_legend_B, position = "bottomleft") %>%
        addLegend(title="Group B", position="bottomright", opacity=1,colors="red",labels = "Group B")}
  })
}

shinyApp(ui, server)

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

List of all available Tile Layers for leaflet

分類Dev

How to add markers to different layers in leaflet using onEachFeature and geojson

分類Dev

Leaflet.js: How to remove multiple layers from map

分類Dev

k_th smallest element per group in R

分類Dev

ShowHide Multiple li> blockquote #Quote

分類Dev

Leaflet marker click always shows last element

分類Dev

Clustering on a Map using R and Leaflet

分類Dev

Jquery group element together

分類Dev

Cannot read property 'addControl' of null

分類Dev

R: Efficiently extract rows with different element in specified column by group in data.table

分類Dev

Rリーフレット-グループレイヤーでaddControl()要素を表示/非表示

分類Dev

RのリーフレットからaddControl()のhtml文字列に画像が表示されない

分類Dev

Leaflet for R: How to customize the coloring of clusters?

分類Dev

Setting Boundaries of Leaflet with sf geometry in R

分類Dev

trying to add a time slider in R leaflet

分類Dev

Angularで動的にFormGroupにaddControl

分類Dev

onclick to show <p><h1><iframe> , function showHide(shID)

分類Dev

Customized normalize by group in R

分類Dev

Forecast for group in R with output

分類Dev

R group by substring

分類Dev

Lag difference by group in R

分類Dev

R Dplyr group_by

分類Dev

How to divide group in R

分類Dev

clearShapes()が機能しない-Rのleaflet()

分類Dev

R Leaflet - use date or character legend labels with colorNumeric() palette

分類Dev

r leaflet shiny app in shiny.io error

分類Dev

RのLeafletのopenweathermap天気タイル

分類Dev

How can I remove attribution in leaflet map in R

分類Dev

Shiny Leaflet R Won't Correctly Change Color of Circle Markers

Related 関連記事

  1. 1

    List of all available Tile Layers for leaflet

  2. 2

    How to add markers to different layers in leaflet using onEachFeature and geojson

  3. 3

    Leaflet.js: How to remove multiple layers from map

  4. 4

    k_th smallest element per group in R

  5. 5

    ShowHide Multiple li> blockquote #Quote

  6. 6

    Leaflet marker click always shows last element

  7. 7

    Clustering on a Map using R and Leaflet

  8. 8

    Jquery group element together

  9. 9

    Cannot read property 'addControl' of null

  10. 10

    R: Efficiently extract rows with different element in specified column by group in data.table

  11. 11

    Rリーフレット-グループレイヤーでaddControl()要素を表示/非表示

  12. 12

    RのリーフレットからaddControl()のhtml文字列に画像が表示されない

  13. 13

    Leaflet for R: How to customize the coloring of clusters?

  14. 14

    Setting Boundaries of Leaflet with sf geometry in R

  15. 15

    trying to add a time slider in R leaflet

  16. 16

    Angularで動的にFormGroupにaddControl

  17. 17

    onclick to show <p><h1><iframe> , function showHide(shID)

  18. 18

    Customized normalize by group in R

  19. 19

    Forecast for group in R with output

  20. 20

    R group by substring

  21. 21

    Lag difference by group in R

  22. 22

    R Dplyr group_by

  23. 23

    How to divide group in R

  24. 24

    clearShapes()が機能しない-Rのleaflet()

  25. 25

    R Leaflet - use date or character legend labels with colorNumeric() palette

  26. 26

    r leaflet shiny app in shiny.io error

  27. 27

    RのLeafletのopenweathermap天気タイル

  28. 28

    How can I remove attribution in leaflet map in R

  29. 29

    Shiny Leaflet R Won't Correctly Change Color of Circle Markers

ホットタグ

アーカイブ