Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exporting selected map data in Shiny #104

Open
zachasman opened this issue Aug 21, 2019 · 3 comments
Open

Exporting selected map data in Shiny #104

zachasman opened this issue Aug 21, 2019 · 3 comments

Comments

@zachasman
Copy link

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:

image

 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)
@tim-salabim
Copy link
Member

tim-salabim commented Aug 21, 2019

selectFeatures will do what you want, but I am not sure how to use it in a shiny app.

@jennet
Copy link

jennet commented Oct 15, 2019

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

@m-e-cws
Copy link

m-e-cws commented Mar 2, 2020

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~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants