r - 如何在闪亮中获取和设置传单(代理)地图的中心位置
问题描述
我想检索当前中心位置并将其设置为相同的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
解决方案
如果其他人好奇,当将延迟应用于 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)
推荐阅读
- r - 在 R 中为 Waffle() 函数使用字形
- python - python - 如何在pandas dataframe python中将名称的第一个字符转换为大写?
- r - 在 GGPLOT 上扩展回归线和标准误差?
- python - 如何使用值的数据框和键的符号将信息行添加到字典
- c - 我想用 0 填充矩阵结构
- excel - excel中的匹配函数
- python-3.x - Python DB连接关闭,在finally子句中使用try catch
- javascript - Jest - 在解构模块上模拟一组函数
- html - 根据复选框选择发送哪些 html 表单数据?
- c++ - 为什么 CreateWindowW 不接受 ATOM 作为 lpClassName?