mapedit
mapedit copied to clipboard
Exporting selected map data in Shiny
I put together some code to display data points on a map and then added in the edit functionality. My question is: Is it be possible to then take the selected data and push it to a data frame on Shiny that say, could be downloaded by the end user in CSV? Below is a screenshot of what it looks like so far, so the idea would be to take all the data points selected and push it to a CSV within Shiny:
library(mapedit)
library(mapview)
library(shiny)
library(leaflet)
library(leaflet.extras)
library(sf)
# select as a module
m = leaflet(turf_clean_mapped) %>%
addCircleMarkers(weight = 1, layerId = 1:nrow(turf_clean))
ui <- tagList(
selectModUI("test-mod"),
DT::dataTableOutput("selected")
)
server <- function(input, output, session) {
selections <- callModule(selectMod, "test-mod", m)
output$selected <- DT::renderDataTable({DT::datatable(selections())})
observe({str(selections())})
}
shinyApp(ui, server)
# edit as a module
m = mapview(turf_clean_mapped)@map
testsf = NULL
ui <- tagList(
editModUI("test-edit"),
h1("What You Draw"),
leafletOutput("edited")
)
server <- function(input, output, session) {
crud <- callModule(editMod, "test-edit", m, "turf_clean_mapped")
output$edited <- renderLeaflet({
req(crud()$finished)
mapview(crud()$finished)@map
})
}
shinyApp(ui, server)
# editMap module can easily be combined to make a selection tool
# do selection of breweries with drawn polygons
ui <- fluidPage(
fluidRow(
column(6,editModUI("brew-select")),
column(6,leafletOutput("mapout"))
)
)
server <- function(input,output,session) {
m = mapview(turf_clean_mapped)@map
turf_sf <- st_as_sf(turf_clean_mapped)
drawn <- callModule(editMod, "brew-select", m)
calc_sf <- reactiveValues()
observe({
req(drawn()$finished)
calc_sf$intersection <- st_intersection(drawn()$finished, turf_sf)
})
output$mapout <- renderLeaflet({
req(calc_sf$intersection)
(mapview(calc_sf$intersection) + mapview(drawn()$finished))@map
})
}
shinyApp(ui,server)
selectFeatures will do what you want, but I am not sure how to use it in a shiny app.
I do something similar to this in my app @za123 , and just use a standard shiny downloadhandler. How you implement that would depend on what columns/data you want in your CSV file.
In mine I have some underlying data, which is filtered by the drawn()$finished features collection; that filtered data is what I use in my downloadHandler / downloadButton
Hey @za213
I also have some code to handle this kind of thing. In your ui you need to have uiOutput("download") somehwere in a fluidRow(). In your server, i do the following:
#this makes the download button appear only once data are selected
output$download <- renderUI({
if(!is.null(calc_sf$intersection) {
downloadButton("downloadData", "Download .csv")
}
})
# Downloadable csv of selected dataset
output$downloadData <- downloadHandler(
filename = function() {
paste("mydata", Sys.Date(),".csv", sep="") #this just pastes the date to the filename
},
content = function(file) {
write.csv(calc_sf$intersection, file, row.names = FALSE)
})
Best of luck~