【发布时间】: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