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

如何在启动闪亮应用程序时首先关闭addLayersControl的图层显示,r,shiny,leaflet,R,Shiny,Leaflet,我正在创建一个带有闪亮+传单的应用程序。 当我第一次启动Shining应用程序时,我尝试显示addLayersControl显示的层的所有默认值。 有没有办法指定显示/隐藏 例如,如果您有以下代码。 (见附件) 库(闪亮) 图书馆(单张) 数据(地震) 地震如果我理解正确,您需要在addLayersControl(position=“topleft”,overlayGroups=c(“A组”,“B组”)之后添加%%>%hideGroup(“B组”),如下所示: library(shiny) li

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

例如,如果您有以下代码。 (见附件)

库(闪亮)
图书馆(单张)
数据(地震)

地震如果我理解正确,您需要在
addLayersControl(position=“topleft”,overlayGroups=c(“A组”,“B组”)
之后添加
%%>%hideGroup(“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)
库(闪亮)
图书馆(单张)
数据(地震)

地震谢谢你的评论。有这样一个功能。我发现的还不够。非常感谢。当然,您也可以将上一次观察中的if语句切换到B组上方的A组,这似乎更有意义。
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)