Search code examples
rshinyr-leaflet

How to turn off the layer display of addLayersControl first when launching the shiny app


I am creating an app with shiny + leaflet. When I launch the shiny app for the very first time, I try to show all the default values of the layers displayed by addLayersControl. Is there a way to specify show / hide?

For example, if you have the following code. (See R leaflet - Show/Hide addControl() element with group layers)

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)

When executed, "GroupA" and "GroupB" are shown by default. Is it possible to control so that only "GroupA" is shown when shiny is started and "GroupB" is hidden?


Solution

  • If I understand correctly you need to add %>% hideGroup("Group B") after addLayersControl(position = "topleft", overlayGroups = c("Group A","Group B")) as below:

    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")) %>%
                hideGroup("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)