2

I have a map that I generate using Leaflet in an R/Shiny app, and am able to listen to event clicks on points on the map using code like this

observeEvent(input$map_marker_click, {
  p = input$map_marker_click
  p1 = filteredData()[filteredData()$Longitude == p$lng & filteredData()$Latitude == p$lat),]
  if(p$id != 'Selected') {
    //do stuff
  }
  output$ggplot = renderPlot({
    //change plot based on selected point
  })
}

But I'm wondering if it's possible to allow a user to select multiple points at once -- perhaps by shift-select or some other way. Would I need to somehow add a listener to see if they are holding down SHIFT, and then pass along a vector of clicked points?

EDIT:

I found an example that shows how to use MapEdit with selectFeatures to multi-select points on a leaflet map and then "do something" with the selection.

Here's the code sample along (taken from: https://gis.stackexchange.com/questions/253483/leaflet-tool-for-multiple-marker-selection-and-computation-of-summary)...:

# devtools::install_github("r-spatial/mapedit")

library(sf)             # for spatial data type representation
library(mapview)        # for the raster data and quick viewing
library(mapedit)        # for the interaction (selection of the data) 

# create the base map with a raster layer
m = mapview(poppendorf[[5]])

# create some mock data points in the vicinity of the raster layer
set.seed(42) # to be reproducible
dframe = data.frame(a = 1:50, 
                    b = rnorm(50, 2, 1), 
                    x = runif(50, 11.15, 11.25),
                    y = runif(50, 49.7, 49.75))

# convert data.frame to sf object as we need 
# geo-spatial data type for this kind of objective
# epsg 4326 is geographic longlat 'projection'
dframe_sf = st_as_sf(dframe, coords = c("x", "y"), crs = 4326)

# inspect data on base map
mapview(dframe_sf, map = m)

# select features via polygon/rectangle/line/point or the like
# by using the draw tools of the draw toolbar on the left 
# and press "Done" when finished.
# multiple selections are also possible.
# (line/point selection will not work as points have no dimension!)
# mode = "click" will enable selection via clicking on features.
selected = selectFeatures(dframe_sf, map = m, mode = "draw")

# check the selection (selected will be diplayed in blue)
mapview(dframe_sf, map = m, col.regions = "red") + selected

# given that selected is a sf object (and hence a data.frame) 
# claculating summaries works just as expected with a normal data.frame
summary(selected)
mean(selected$a)
mean(selected$b)
sd(selected$b)

# we can also set other selection criteria. e.g. invert selection via st_disjoint
diff_selected = selectFeatures(dframe_sf, map = m, mode = "draw", op = st_disjoint)

# check the selection (selected will be diplayed in blue)
mapview(dframe_sf, map = m, col.regions = "red") + diff_selected

I understand that the following line is responsible for taking the SF dataframe and allowing us to select points on the map:

selected = selectFeatures(dframe_sf, map = m, mode = "draw")

My question is this -- how do I get it to render that same view but in Shiny, as opposed to my R console?

In Shiny, we have to wrap the leaflet map in the renderLeaflet function... If I were doing it without Shiny, I could just do something like this:

m = leaflet(data = df) %>% 
 addCircleMarkers() %>%
 addLegend... 

selected = selectFeatures(dframe_benthic_sf, map = m, mode = "click")
shishy
  • 787
  • 1
  • 15
  • 31
  • 2
    Yes, it's possible, can you post your code with server and other ui parts? – pogibas Sep 23 '17 at 18:30
  • Is there anything specific you need from there? The full codebase is somewhat long and not 100% clean yet but I can try to get you the relevant parts of interest... – shishy Sep 23 '17 at 18:44
  • So you need plot where you want to select multiple points and send to another process? – pogibas Sep 23 '17 at 18:45
  • Basically if I select one point now, I have an event listener that then generates a plot for that point. But I want users to be able to select multiple points and when done, show a plot that consolidates the data from all of them. If I know how to listen for multiple map marker clicks and get their data, I should be able to take it from there – shishy Sep 23 '17 at 18:49
  • maybe `selectMap` from the [mapedit package](https://github.com/r-spatial/mapedit) can be of help here? – TimSalabim Sep 23 '17 at 18:50
  • @TimSalabim I updated my question trying your suggestion(s). – shishy Oct 19 '17 at 01:40
  • @shishy here's a blog post describing how to use the select module within a shiny environment http://r-spatial.org/r/2017/06/09/mapedit_0-2-0.html#shiny-modules – TimSalabim Oct 19 '17 at 05:49

0 Answers0