Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R leaflet and shiny How to clear map click data

Tags:

r

leaflet

shiny

The example below is taken from the RStudio tutorial on leaflet. I modified it slightly to fit my issue.

I have a map (here, earthquakes) which I draw on the map using addCircleMarkers and when clicked, the a popup appears with some information. What I want to do in my real app is make it so when a marker is clicked on the map, it filters the other graphs on the page to only the data relevant to that marker. I know how to get the info about where a user has clicked using input$map_marker_click - this will give me the latitude and longitude which will be sufficient for my needs. However - once set, this value doesn't change. It doesn't revert to NULL when the user clicks on the map in a non-marker area. How do I detect a user has clicked in the map on something other than a marker and reset input$map_marker_click to NULL

The example below doesn't have other graphs but I do have it displaying the value of 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)
like image 916
Mark Avatar asked Jun 27 '15 18:06

Mark


1 Answers

I asked this same question here and user NicE provided a solution there.

In case anyone comes across this page looking for a solution, the below code achieves the above request resetting the click value to NULL when the map is clicked after a marker. The only additional code from the example is between the two lines of #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)
like image 197
E Keith Avatar answered Nov 11 '22 02:11

E Keith