【问题标题】:Split code of one leaflet map (so that input updates of one part does not affect other part of code)一张传单地图的拆分代码(这样一个部分的输入更新不会影响其他部分的代码)
【发布时间】:2021-03-31 01:32:16
【问题描述】:

是否可以拆分地图的代码,以便地图的一部分仅在其自己的输入发生更改时才更新?

在下面的再现示例中,选择“碳粉”瓦片并选择新站时,再次执行整个传单映射,因为需要更新addLegend 987654321。这使磁贴跳回“OSM(默认)”磁贴。我想留在我选择其他电台时选择的图块。

library(leaflet)
library(shiny)
library(dplyr)

pal <- colorFactor(
  palette = "YlGnBu",
  domain = quakes$stations
)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      checkboxGroupInput("stations", 
                  "Choose a station",
                  choices=sort(unique(quakes$stations)),
                  selected = c(10, 11))
    ),
    mainPanel(
      leafletOutput("map")
    )
  )
)

server <- function(input, output) {
  points <- reactive({
    quakes %>%
      filter(stations %in% input$stations)
  })
  
  output$map <- renderLeaflet({
    leaflet(quakes) %>%
      addTiles(group = "OSM (default)") %>%
      addProviderTiles(providers$Stamen.Toner, group = "Toner") %>%
      addLayersControl(
        baseGroups = c("OSM (default)", "Toner"),
        options = layersControlOptions(collapsed = FALSE)) %>%
      addLegend("Legend", position = "topleft", pal = pal, values = input$stations)
  })
  
  observe({
    if(nrow(points()) == 0) {
      leafletProxy("map", data = points()) %>%
        clearMarkers()
    } else {
      leafletProxy("map", data = points()) %>%
        clearMarkers() %>%
        addCircleMarkers(radius = 2)
    }
  })
}

shinyApp(ui, server)

我尝试了几件事,包括在else 语句中添加addLegend,但效果并不好。我是传单/闪亮的新手,移动addLegend 对我来说似乎最合乎逻辑。我非常感谢任何建议!

【问题讨论】:

    标签: r shiny leaflet shinydashboard


    【解决方案1】:

    据我所知,您尝试将addLegend 移动到观察者是在正确的轨道上。这样做对我来说很好。

    1. addLegend 移动到observe
    2. 在添加图例之前,使用 clearControls 删除任何现有图例(否则您会得到多个图例)
    3. 我删除了observe 中的重复代码
    4. 据我所知,条件nrow(points()) &gt; 0 只需要决定是否应该绘制图例。对于标记,这无关紧要。
    
        library(leaflet)
        library(shiny)
        library(dplyr)
        
        pal <- colorFactor(
          palette = "YlGnBu",
          domain = quakes$stations
        )
        
        ui <- fluidPage(
          sidebarLayout(
            sidebarPanel(
              checkboxGroupInput("stations", 
                                 "Choose a station",
                                 choices=sort(unique(quakes$stations)),
                                 selected = c(10, 11))
            ),
            mainPanel(
              leafletOutput("map")
            )
          )
        )
        
        server <- function(input, output) {
          points <- reactive({
            quakes %>%
              filter(stations %in% input$stations)
          })
          
          output$map <- renderLeaflet({
            leaflet(quakes) %>%
              addTiles(group = "OSM (default)") %>%
              addProviderTiles(providers$Stamen.Toner, group = "Toner") %>%
              addLayersControl(
                baseGroups = c("OSM (default)", "Toner"),
                options = layersControlOptions(collapsed = FALSE))
          })
          
          observe({
            proxy <- leafletProxy("map", data = points()) %>%
              clearMarkers() %>% 
              clearControls() %>% 
              addCircleMarkers(radius = 2)
            
            if (nrow(points()) > 0) 
              proxy <- proxy %>% addLegend("Legend", position = "topleft", pal = pal, values = input$stations)
            
            proxy
          })
        
        }
        
        shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-14
      • 1970-01-01
      • 1970-01-01
      • 2017-09-06
      • 2023-04-08
      • 2021-04-18
      • 1970-01-01
      • 2020-03-04
      相关资源
      最近更新 更多