2

I created a shiny dashboard to analysis the crimes in chicago. Therefore I also built a leaflet map of chicago where we can see based on markers how many crimes happened in which area of chicago. I have three filter widgets (Date, Crime Type and Location). Based on these 3 widgets the user can play around and everytime the map is ploted again with the selected inputs. Everything works perfect and fine, I just would like to add a "Download" section where the CURRENT PLOT WITH THE MARKERS can be exported into different format (png, pdf ...). My dashbord looks like this:

Dashboard - Map Page

As you can see I would like to give the user the choice which format of export he would like to have. I tried some code but it didnt worked.

My ui.R:

tabItem(tabName = "map",

      fluidRow(

        column(width = 9,
        # Main plot
        box(
          title = "Chicago Map", 
          status = "danger", 
          solidHeader = TRUE,
          width = NULL,
          height = NULL,
          #tags$style(type = "text/css", "#map {height: calc(100vh - 80px) !important;}"),
          leafletOutput("map",height = "95vh")
        )),

        column(width = 3,
        box(
          title = "Date", 
          status = "primary", 
          solidHeader = TRUE,
          width = NULL,
          #height = 142,
          sliderInput("mapYear", label = "Select Year", min = 2001, max = 2016, step = 1, sep = '', value = c(2001,2016))
        ),

        box(
          title = "Crime Type", 
          status = "primary", 
          solidHeader = TRUE,
          width = NULL,
          selectInput("mapCrimeType", label= "Select Crime Type", choices = unique(cc$Primary.Type), multiple = TRUE)
        ),

        box(
          title = "Location", 
          status = "primary", 
          solidHeader = TRUE,
          width = NULL,
          selectInput("mapLocation", label= "Select Location", choices = unique(cc$Location.Description), multiple = TRUE)
        ),

        box(
          title = "Download", 
          status = "success", 
          solidHeader = TRUE,
          width = NULL,
          radioButtons("mapFormat", "Document format", c("PNG"="png", "EPS"="eps", "PDF"="pdf"), inline = TRUE, selected = "png"),
          downloadButton("mapDownload")
        )
      )
    )
  ),

And my server like this:

  ### Draw Map ###
  output$map = renderLeaflet({
    m <- leaflet() %>% 
      addProviderTiles(providers$Esri.WorldStreetMap) %>% 
      setView(lng = -87.623177, lat = 41.881832, zoom=11)
  }) 

  reactMap = reactive({
    cc %>%
      filter(Primary.Type %in% input$mapCrimeType &
               Location.Description %in% input$mapLocation &
               Year2 %in% cbind(input$mapYear[1],input$mapYear[2]))
  })

  observe({
    proxy = leafletProxy("map", data = reactMap()) %>%
      clearMarkers() %>%
      clearMarkerClusters() %>%
      addCircleMarkers(clusterOptions = markerClusterOptions(),
                       lng =~ Longitude, lat =~ Latitude, radius = 5, group = 'Cluster', 
                       popup =~ paste('<b><font color="Black">', 'Crime Information', 
                                      '</font></b><br/>', 'Crime Type:', Primary.Type,'<br/>',
                                      'Date:', Date,'<br/>', #'Time:', Time,'<br/>',
                                      'Location:', Location.Description,'<br/>', 'Block:', Block, '<br/>', 'Arrest:', Arrest, '<br/>'))
  })  

  fnMap <- reactive({paste(input$mapCrimeType,input$mapLocation,sep = ".")})
  doMap <- reactive({input$mapFormat})

  # Download current plot
  output$mapDownload <- downloadHandler(
    filename = fnMap,
    content = function(file) {
      mapshot(m, file)
    }
  )

Does somebody has any idea, or suggestion? I would be very happy to solve this problem. Thanks so much in advance!

UPDATE: Does somebody know how to solve my code? The other posts with similar problems doesnt work for me.

S002
  • 31
  • 7
  • Could this be a possible duplicate of [How to save a leaflet map in Shiny](https://stackoverflow.com/questions/44259716/how-to-save-a-leaflet-map-in-shiny). If not could you elaborate on the differences? – Tonio Liebrand Jan 10 '19 at 18:39
  • my code is different. I am also working with markers and would like to have them also inside the export file. – S002 Jan 11 '19 at 11:29
  • @BigDataScientist could you help me please do figure out how to solve the problem? That the last problem of my shiny app to solve... – S002 Jan 13 '19 at 11:02
  • @BigDataScientist in 12 hours I have to pass my paper. Do you have any solution for my special code? – S002 Jan 17 '19 at 05:38

0 Answers0