2015-06-27 42 views
4

以下示例摘自the RStudio tutorial小册子。我稍微修改它以适应我的问题。R小册子和有光泽如何清除地图点击数据

我有一张地图(这里,地震),我使用addCircleMarkers在地图上绘制,点击时弹出一些信息。我想在我的真实应用程序中执行的操作是在地图上单击标记时将其过滤,它会将页面上的其他图形过滤为仅与该标记相关的数据。我知道如何获得有关用户点击的位置的信息input$map_marker_click - 这会给我经度和纬度,足以满足我的需求。但是 - 一旦设置,该值不会改变。当用户在非标记区域单击地图时,它不会恢复为NULL。如何检测用户点击在地图上比其他标记的东西,重置input$map_marker_clickNULL

下面的例子没有其他的绘图,但我确实有它显示的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) 

回答

4

我问过同样的问题here,用户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)