区分 input$map_click 和 input$map_shape_click

问题描述 投票:0回答:1

我想做的是,如果用户单击一条线,它会在地图右侧的框中显示该线的名称,如果用户单击地图上的其他位置,它会“取消选择”该线: enter image description here

enter image description here

问题是,当用户单击折线时,leaflet 会同时触发 map_shape_click (折线)和 map_click (地图)事件。更烦人的是,它会在 map_click 事件之前触发 map_shape_click 事件。

如何区分用户是否点击了一条线,或者只是点击了底图,以便我的选择/取消选择起作用?可重现的示例:

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)
r shiny r-leaflet
1个回答
1
投票
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)

事情有点棘手。我们使用

freezeReactiveValue
来冻结地图点击,这意味着如果有任何形状点击事件,我们不会更新
map_click
的值。这是有点高级的闪亮。我建议您阅读帮助文件并阅读本章:https://mastering-shiny.org/action-dynamic.html#freezing-reactive-inputs

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.