R传单和闪亮如何清除地图点击数据

R leaflet and shiny How to clear map click data

以下示例摘自 the RStudio tutorial 的传单。我稍微修改了一下以适应我的问题。

我有一张地图(此处为地震),我使用 addCircleMarkers 在地图上绘制,单击时会出现一个弹出窗口,其中包含一些信息。我想在我的真实应用程序中做的是,当在地图上单击标记时,它将页面上的其他图表过滤为仅与该标记相关的数据。我知道如何使用 input$map_marker_click 获取有关用户点击位置的信息 - 这将为我提供足以满足我需要的纬度和经度。然而 - 一旦设置,这个值就不会改变。当用户在非标记区域中单击地图时,它不会恢复为 NULL。如何检测用户在地图中单击了标记以外的其他内容并将 input$map_marker_click 重置为 NULL

下面的示例没有其他图表,但我有它显示 input$map_marker_click

的值
library(shiny)
library(leaflet)
library(RColorBrewer)

ui <- bootstrapPage(
  tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
  leafletOutput("map", width = "100%", height = "100%"),
  absolutePanel(top = 10, right = 10,
                sliderInput("range", "Magnitudes", min(quakes$mag), max(quakes$mag),
                            value = range(quakes$mag), step = 0.1
                ),
                selectInput("colors", "Color Scheme",
                            rownames(subset(brewer.pal.info, category %in% c("seq", "div")))
                ),
                checkboxInput("legend", "Show legend", TRUE),
                verbatimTextOutput("clickInfo")
  )
)

server <- function(input, output, session) {

  output$clickInfo = renderPrint({input$map_marker_click})

  filteredData <- reactive({
    quakes[quakes$mag >= input$range[1] & quakes$mag <= input$range[2],]
  })

  colorpal <- reactive({
    colorNumeric(input$colors, quakes$mag)
  })

  output$map <- renderLeaflet({
    leaflet(quakes) %>% addTiles() %>%
      fitBounds(~min(long), ~min(lat), ~max(long), ~max(lat))
  })

  observe({
    pal <- colorpal()
    leafletProxy("map", data = filteredData()) %>%
      clearShapes() %>%
      addCircleMarkers(radius = ~mag^2/3, weight = 1, color = "#777777",
                 fillColor = ~pal(mag), fillOpacity = 0.7, popup = ~paste(mag)
      )
  })

  observe({
    proxy <- leafletProxy("map", data = quakes)
    proxy %>% clearControls()
    if (input$legend) {
      pal <- colorpal()
      proxy %>% addLegend(position = "bottomright",
                          pal = pal, values = ~mag
      )
    }
  })
}

shinyApp(ui, server)

我问了同样的问题,用户 NicE 在那里提供了一个解决方案。

万一有人浏览此页面寻找解决方案,下面的代码实现了上述请求,在标记后单击地图时将点击值重置为 NULL。该示例中唯一的附加代码位于 #s.

的两行之间
library(shiny)
library(leaflet)
library(RColorBrewer)

ui <- bootstrapPage(
  tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
  leafletOutput("map", width = "100%", height = "100%"),
  absolutePanel(top = 10, right = 10,
                sliderInput("range", "Magnitudes", min(quakes$mag), max(quakes$mag),
                            value = range(quakes$mag), step = 0.1
                ),
                selectInput("colors", "Color Scheme",
                            rownames(subset(brewer.pal.info, category %in% c("seq", "div")))
                ),
                checkboxInput("legend", "Show legend", TRUE),
                verbatimTextOutput("clickInfo")
  )
)

server <- function(input, output, session) {

  #########################################################

  data <- reactiveValues(clickedMarker=NULL)

  observeEvent(input$map_marker_click,
               {data$clickedMarker <- input$map_marker_click})

  observeEvent(input$map_click,
               {data$clickedMarker <- NULL})

  output$clickInfo <- renderPrint({data$clickedMarker})

  ##########################################################

  filteredData <- reactive({
    quakes[quakes$mag >= input$range[1] & quakes$mag <= input$range[2],]
  })

  colorpal <- reactive({
    colorNumeric(input$colors, quakes$mag)
  })

  output$map <- renderLeaflet({
    leaflet(quakes) %>% addTiles() %>%
      fitBounds(~min(long), ~min(lat), ~max(long), ~max(lat))
  })

  observe({
    pal <- colorpal()
    leafletProxy("map", data = filteredData()) %>%
      clearShapes() %>%
      addCircleMarkers(radius = ~mag^2/3, weight = 1, color = "#777777",
                       fillColor = ~pal(mag), fillOpacity = 0.7, popup = ~paste(mag)
      )
  })

  observe({
    proxy <- leafletProxy("map", data = quakes)
    proxy %>% clearControls()
    if (input$legend) {
      pal <- colorpal()
      proxy %>% addLegend(position = "bottomright",
                          pal = pal, values = ~mag
      )
    }
  })
}

shinyApp(ui, server)