如何在启动shiny app时先关闭addLayersControl的图层显示

问题描述 投票:0回答:1

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

例如,如果您有以下代码。 (参见 R 传单 - 显示/隐藏带有图层组的 addControl() 元素

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”。是否可以控制使得shiny启动时只显示“GroupA”而隐藏“GroupB”?

r shiny r-leaflet
1个回答
4
投票

如果我理解正确的话,你需要在

%>% hideGroup("Group B")
之后添加
addLayersControl(position = "topleft", overlayGroups = c("Group A","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)
© www.soinside.com 2019 - 2024. All rights reserved.