简体   繁体   English

区分 Leaflet R Shiny 中的 input$map_click 和 input$map_shape_click

[英]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.

相关问题 带有两个同时输入的 R flexdashboard$map_shape_click 不起作用 - R flexdashboard with two simultaneous input$map_shape_click not working Select 在 leaflet 中使用 map_click 的多个项目,链接到 shiny 应用程序 (R) 中的 selectizeInput() - Select multiple items using map_click in leaflet, linked to selectizeInput() in shiny app (R) 使用传单中的 map_click 选择多个项目,根据过滤后的数据链接到 shiny app (R) 中的 selectizeInput() - Select multiple items using map_click in leaflet, linked to selectizeInput() in shiny app (R) based on filtered data 我可以在 r 中使用传单“map_shape_click”事件来用数据表填充 box() 吗? - Can I use in r the leaflet "map_shape_click" event to populate a box() with a datatable? 单击传单地图中的点作为闪亮图形的输入 - Click on points in a leaflet map as input for a plot in shiny R小册子和闪亮如何清除地图点击数据 - R leaflet and shiny How to clear map click data R Shiny:用 Leaflet Z46F3EA056CAA31268CZEA Click 更新多个相关下拉菜单 - R Shiny: Updating Multiple Dependent Dropdown Menus with Leaflet Map Click 单击Leaflet地图上的点以在Shiny中生成ggplot - Click on points on Leaflet map to generate ggplot in Shiny 如何使Shiny Leaflet Map Reac更改输入值R - How to make Shiny leaflet map reac to change in Input value R Shiny/Leaflet:colors 由用户输入的 choropleth map - Shiny/Leaflet: colors for choropleth map by user input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM