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")