Interactive Leaflet Map using Shiny


I am new to shiny and leaflet and I’m trying to develop a basic interactive map of road accidents. I am unsure how to create a reactive link from the ui to the server code.

My file contains 14 records with each record containing three variables called Longitude, Latitude and Type. Twelve of the 14 records are pedestrian and the remaining 2 are Other accidents. The first two records from the input file are:

Latitude Longitude Type
53.32799968 -6.43999979 Other
53.33212807 -6.23639523 Pedestrian

The intention is to select one of the two accident categories using a sidebar input from the shiny ui e.g. Pedestrian and have the 12 pedestrian accident locations plotted. Similarly when Other is selected I would like the remaining two accidents plotted.

I’m not sure how to do this and would be grateful for any assistance. My example code is below which plots all the 14 records fine but and what I need is. as stated earlie.r is some form of reactive expression connecting the ui select input to the serve code.


# reads in data 

accident <-read_xls("Desktop/InteractiveMap/RTA2.xls")

qpal <- colorFactor("Paired", accident$Type)

ui = fluidPage(

  titlePanel("Accidents in Dublin"),
      selectInput("accident$Type", "Accident Type", 

      helpText("Data from RSA")



server = function(input, output) {

  output$map <- renderLeaflet({
    # reactive expression code required here to connect with ui selection?
      accident %>%
      leaflet() %>%
      addTiles() %>%
      addCircleMarkers(lat = ~Latitude,lng = ~Longitude) %>%
      addLegend(pal = qpal,value = ~Type, title = "Accident Type")

# Run the application 

shinyApp(ui = ui, server = server)
1 Like

HI @statman,

Please check out for an example on how to use leafletProxy. It is a function that can update your leaflet map within a shiny application.


Many thanks Barret for the information - I'll check out the link.
Best regards,

1 Like