A little over a month ago I attended FOSS4GUK 2019. It was a great conference and there were a few presentations (like mine) on Leaflet. One conversation I had was about using Leaflet to gather data and the question was whether it could be used draw and export polygons/polylines. I thought I would see if it could be. I have previously used a shiny app to record points submitted by users but not to record polygons.
We can initialize a map quite easily.
library(leaflet)
leaflet() %>%
setView(lng = -4, lat = 58, zoom = 7) %>%
addTiles()
We can also use the Leaflet Extras package to add Leaflet Draw to it.
library(leaflet)
library(leaflet.extras)
leaflet() %>%
setView(lng = -4, lat = 58, zoom = 7) %>%
addTiles() %>%
addDrawToolbar()
This now gives users the possibility of drawing polygons. If we embed one of these in a shiny app then we are able to discover when the user has completed the polygon by using observeEvent(input$map_draw_new_feature, {...})
. Having done this we can access the geometry of the polygon using input$map_draw_new_feature$geometry$coordinates[[1]]
, convert to a shapefile and allow the user to download it.
Having got a shiny app that lets uers draw and download a polygon I was trying to figure how to publish it. I could have put it on my shinyserver but then I read about binder and the holepunch package. Binder is a resource that lets you turn almost any Github repository into a open version of RStudio or Jupyter notebooks. The holepunch package helps you turn your R
based Github repo into an RStudio instance, including any dependencies. The documentation is available here but the process is pretty straightforward. At some point I may document the whole thing but for the moment I will leave it to the documentation.
The final shiny app can be found in this repo and clicking on the Binder badge will launch things.