如何在启动闪亮的应用程序时先关闭addLayersControl的图层显示

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

我正在创建一个带有 shiny + leaflet 的应用程序。 当我第一次启动闪亮的应用程序时,我尝试显示 addLayersControl 显示的图层的所有默认值。 有没有办法指定显示/隐藏?

例如,如果您有以下代码。 (参见

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)

执行时,默认显示“GroupA”和“GroupB”。是否可以控制以便在启动闪亮时仅显示“GroupA”并隐藏“GroupB”?

如果我没理解错你需要在addLayersControl(position = "topleft", overlayGroups = c("Group A","Group B"))后面加上%>% hideGroup("Group B")如下:

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)