[英]Distinguish between input$map_click and input$map_shape_click in Leaflet R Shiny
What I would like to do is that if a user clicks on a line, it displays the line name in the box to the right of the map, and if a user clicks somewhere else on the map, it 'deselects' that line:我想做的是,如果用户单击一行,它会在 map 右侧的框中显示行名称,如果用户单击 map 上的其他位置,它会“取消选择”该行:
The problem is that when a user clicks the polyline, leaflet fires both a map_shape_click (the polyline) and map_click (the map) event.问题在于,当用户单击折线时,leaflet 会同时触发 map_shape_click(折线)和 map_click(地图)事件。 Even more annoyingly, it fires the map_shape_click event before the map_click event.
更烦人的是,它会在 map_click 事件之前触发 map_shape_click 事件。
How can I distinguish whether the user has clicked a line, or just the base map, so that my select/deselect works?如何区分用户是单击了一行,还是仅单击了基本 map,以便我的选择/取消选择有效? Reproducible example:
可重现的例子:
library(shiny)
library(tidyverse)
library(leaflet)
ui <- fluidPage(
fluidRow(
column(
width = 8,
leafletOutput("map")
),
column(
width = 4,
uiOutput("info")
)
)
)
server <- function(input, output) {
output$map <- renderLeaflet({
leaflet() %>%
addTiles() %>%
setView(lng = -71.03165, lat = 42.37595, zoom = 13) %>%
addPolylines(lng = c(-71.05884, -71.02), lat = c(42.360081, 42.359),
layerId = "line1") %>%
addPolylines(lng = c(-71.05884, -71.05), lat = c(42.360081, 42.4),
layerId = "line2")
})
observeEvent(input$map_shape_click, {
x <- input$map_shape_click
output$info <- renderUI({
div(
"Line: ", x$id
)
})
})
observeEvent(input$map_click, {
output$info <- renderUI({
div(
"Nothing selected"
)
})
})
}
shinyApp(ui = ui, server = server)
library(shiny)
library(tidyverse)
library(leaflet)
ui <- fluidPage(
fluidRow(
column(
width = 8,
leafletOutput("map")
),
column(
width = 4,
uiOutput("info")
)
)
)
server <- function(input, output) {
output$map <- renderLeaflet({
leaflet() %>%
addTiles() %>%
setView(lng = -71.03165, lat = 42.37595, zoom = 13) %>%
addPolylines(lng = c(-71.05884, -71.02), lat = c(42.360081, 42.359),
layerId = "line1") %>%
addPolylines(lng = c(-71.05884, -71.05), lat = c(42.360081, 42.4),
layerId = "line2")
})
clicked <- reactiveVal()
observeEvent(input$map_shape_click, {
freezeReactiveValue(input, 'map_click')
clicked(input$map_shape_click)
})
observeEvent(input$map_click, {
clicked(input$map_click)
})
output$info <- renderUI({
req(clicked())
if(is.null(clicked()[['id']])) return(div("Nothing selected"))
div("Line: ", clicked()$id)
})
}
shinyApp(ui = ui, server = server)
Things are a little tricky here.这里的事情有点棘手。 we use
freezeReactiveValue
to freeze the map click, meaning if there is any shape click event, we do not update the value of map_click
.我们使用
freezeReactiveValue
来冻结 map 点击,这意味着如果有任何形状点击事件,我们不会更新map_click
的值。 This is a little advanced shiny.这是一个有点高级的 shiny。 I recommend you read the help file and read this chapter: https://mastering-shiny.org/action-dynamic.html#freezing-reactive-inputs
我建议您阅读帮助文件并阅读本章: https://mastering-shiny.org/action-dynamic.html#freezing-reactive-inputs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.