I need to take the shapes drawn in an R Leaflet Shiny app using addDrawToolbar in leaflet.extras and save them to a file that can be re-imported by an R Leaflet Shiny app at a later time.
I am focusing on the leaflet.extras information in GitHub by Bhaskar Karambelkar where it lists the commands to pull out the data for the shapes drawn. How do I parse out this data in R?
The following code is what I can do so far: Draw shapes and print them out as a .csv or .txt file. I've included both examples. So in this code, you draw whatever shapes you want from the Draw Toolbar and then hit the Generate Shape List button.
It works for capturing all the shape coordinates, but in these formats the data is not as usable as I need them to be. Is there a way to parse this data so that it can be re-imported, displayed, and edited if need be? Any insights on this is really appreciated!
library(shiny)
library(leaflet)
library(leaflet.extras)
library(utils)
sh <- data.frame()
ui <- bootstrapPage(
tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
leafletOutput("mymap", width = "100%", height = "100%"),
absolutePanel(top = 10, right = 10, width = 300,
style = "padding: 8px",
actionButton("printShapes", h5(strong("Generate Shape List")))
)
)
server <- function(input, output, session) {
output$mymap <- renderLeaflet({
leaflet() %>%
addTiles(group = "Default", attribution = 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors') %>%
setView(lng = -98, lat = 38, zoom = 4) %>%
addDrawToolbar(targetGroup = "draw", position = "topleft", editOptions = editToolbarOptions(edit=TRUE))
})
# Generate Shape List Action Button
observeEvent(input$printShapes, {
shapedf <- data.frame()
reactive(shapedf)
shapedf <-input$mymap_draw_all_features
sh <<- as.data.frame(shapedf)
sh <- t(sh)
shpwrite <- write.csv(sh, paste0("OUTPUTdrawings",".csv"))
shpwrite1 <- dput(sh, file = "OUTPUTdrawings1.txt")
})
}
shinyApp(ui = ui, server = server)