Get startedGet started for free

Mass shootings: modify output

In this exercise, you will extend the Shiny app you built previously so that red circles sized based on the number of fatalities appear on the interactive map, along with a summary of the case when the circle is clicked. Your app will resemble the image in this screenshot.

An app displaying red circles for each mass shooting incident with details appearing on clicking the circle

This exercise is part of the course

Building Web Applications with Shiny in R

View Course

Exercise instructions

  • Update the reactive expression rval_mass_shootings so that the dataset is filtered based on the number of fatalities (nb_fatalities) and date range (date_range) selected by the user.
  • Update the code inside addCircleMarkers() so that radius of the circle is mapped to fatalities and the popup parameter is mapped to summary. Note that you can map a parameter p to a field f using p = ~ f.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

ui <- bootstrapPage(
  theme = shinythemes::shinytheme('simplex'),
  leaflet::leafletOutput('map', height = '100%', width = '100%'),
  absolutePanel(top = 10, right = 10, id = 'controls',
    sliderInput('nb_fatalities', 'Minimum Fatalities', 1, 40, 10),
    dateRangeInput('date_range', 'Select Date', "2010-01-01", "2019-12-01")
  ),
  tags$style(type = "text/css", "
    html, body {width:100%;height:100%}     
    #controls{background-color:white;padding:20px;}
  ")
)

server <- function(input, output, session) {
  rval_mass_shootings <- reactive({
    # MODIFY CODE BELOW: Filter mass_shootings on nb_fatalities and 
    # selected date_range.
    mass_shootings

      
      
      
      
  })
  output$map <- leaflet::renderLeaflet({
    rval_mass_shootings() %>%
      leaflet() %>% 
      addTiles() %>%
      setView( -98.58, 39.82, zoom = 5) %>% 
      addTiles() %>% 
      addCircleMarkers(
        # CODE BELOW: Add parameters popup and radius and map them
        # to the summary and fatalities columns

        fillColor = 'red', color = 'red', weight = 1
      )
  })
}

shinyApp(ui, server)
Edit and Run Code