【问题标题】:How to get and set center location of a leaflet (proxy) map in shiny如何在闪亮中获取和设置传单(代理)地图的中心位置
【发布时间】:2021-02-27 15:22:57
【问题描述】:

我想检索当前中心位置并将其设置为相同的numericInput。一般来说,我可以实现它,但是,在更改输入字段时,传单很难到达稳定的位置,但会来回弹跳。

我还尝试包含一个短暂的延迟 (throttle()/debounce()) 来规避问题,但事实证明这并不成功。有什么想法可以让它双向工作吗?

library(shiny)
library(shinyWidgets)
library(leaflet)

ui <- fluidPage(
  fluidRow(
  numericInputIcon(inputId = "longitude",
                   label = "Longitude",
                   icon = icon("arrows-alt-h"),
                   min = -180,
                   max = 180,
                   value = 18,
                   step = .1),
  numericInputIcon(inputId = "latitude",
                   label = "Latitude",
                   icon = icon("arrows-alt-v"),
                   min = -90,
                   max = 90,
                   value = 50,
                   step = .1)
  ),
  leafletOutput("map")
)
server <- function(input, output, session) {

  output$map <- renderLeaflet({
    leaflet() %>%
      addTiles() %>%
      setView(lng = isolate(input$longitude),
              lat = isolate(input$latitude),
              zoom = 3)
  })

  observeEvent(input$map_center, {
    updateNumericInputIcon(session = session,
                           inputId = "longitude",
                           value = round(input$map_center$lng, 1))
    updateNumericInputIcon(session = session,
                           inputId = "latitude",
                           value = round(input$map_center$lat, 1))
  })


  # with delay -------------------------------------
  updateMap <- reactive({
    leafletProxy("map", session) %>%
      setView(lng = input$longitude,
              lat = input$latitude,
              zoom = 3)
  })

  updateMap_t <- updateMap %>% throttle(1000)

  observe(updateMap_t())

  # without any delay ------------------------------
  # observe({
  #   leafletProxy("map", session) %>%
  #     setView(lng = input$longitude,
  #             lat = input$latitude,
  #             zoom = 3)
  # })
}

shinyApp(ui, server)

旁注:我不想再次渲染完整的地图,因为这需要一些时间。因此,leafletProxy

【问题讨论】:

    标签: r shiny leaflet shiny-reactivity r-leaflet


    【解决方案1】:

    如果其他人好奇,当对 numericInput 应用延迟而不是地图更新时,它会更加稳定且实际可用。仍然有轻微的震动,但这是可以挽救的 IMO。任何其他建议仍不胜感激。

    library(shiny)
    library(shinyWidgets)
    library(leaflet)
    
    ui <- fluidPage(
      fluidRow(
        numericInputIcon(inputId = "longitude",
                         label = "Longitude",
                         icon = icon("arrows-alt-h"),
                         min = -180,
                         max = 180,
                         value = 18,
                         step = .1),
        numericInputIcon(inputId = "latitude",
                         label = "Latitude",
                         icon = icon("arrows-alt-v"),
                         min = -90,
                         max = 90,
                         value = 50,
                         step = .1)
      ),
      leafletOutput("map")
    )
    server <- function(input, output, session) {
    
      output$map <- renderLeaflet({
        leaflet() %>%
          addTiles() %>%
          setView(lng = isolate(input$longitude),
                  lat = isolate(input$latitude),
                  zoom = 3)
      })
    
    
    
      # update Input with delay
      map_long <- eventReactive(input$map_center$lng, {
        input$map_center$lng
      })%>% debounce(1000)
      map_lat <- eventReactive(input$map_center$lat, {
        input$map_center$lat
      })%>% debounce(1000)
    
      observe({
        updateNumericInputIcon(session = session,
                               inputId = "longitude",
                               value = round(map_long(), 1))
        updateNumericInputIcon(session = session,
                               inputId = "latitude",
                               value = round(map_lat(), 1))
      })
    
    
      # without any delay
      observe({
        leafletProxy("map", session) %>%
          setView(lng = input$longitude,
                  lat = input$latitude,
                  zoom = 3)
      })
    
    
    
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-03
      • 2019-06-26
      • 1970-01-01
      • 2016-01-16
      • 2014-07-26
      • 1970-01-01
      • 2015-03-28
      • 2019-10-08
      相关资源
      最近更新 更多