下面的例子摘自RStudio 教程 http://rstudio.github.io/leaflet/shiny.html传单上。我稍微修改了一下以适应我的问题。
我有一张地图(这里是地震),我使用它在地图上绘制addCircleMarkers
单击时,会出现一个弹出窗口,其中包含一些信息。我想要在我的真实应用程序中做的是,当单击地图上的标记时,它会过滤页面上的其他图形,仅显示与该标记相关的数据。我知道如何获取有关用户点击位置的信息input$map_marker_click
- 这将为我提供足以满足我的需要的纬度和经度。然而,一旦设置,该值就不会改变。它不会恢复到NULL
当用户单击地图上的非标记区域时。如何检测用户在地图中单击了标记以外的其他内容并重置input$map_marker_click
to 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)