Mass shootings

Building Web Applications with Shiny in R

Ramnath Vaidyanathan

VP of Product Research

Explore data

The first 10 rows from the mass shootings dataset

Building Web Applications with Shiny in R

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

Building Web Applications with Shiny in R

Add UI

ui <- bootstrapPage(
  theme = shinythemes::shinytheme('simplex'),

leaflet::leafletOutput('map', width = '100%', height = '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;} ")
)
Building Web Applications with Shiny in R

Add output: interactive map

server <- function(input, output, session){

output$map <- leaflet::renderLeaflet({
leaflet() %>%
addTiles() %>%
setView( -98.58, 39.82, zoom = 5)
})
}
Building Web Applications with Shiny in R

An app with an interactive map of the US and controls to select a date range and number of fatalities

Building Web Applications with Shiny in R

Add reactive expression

rval_mass_shootings <- reactive({

mass_shootings %>%
filter(
date >= input$date_range[1],
date <= input$date_range[2],
fatalities >= input$nb_fatalities
)
})
Building Web Applications with Shiny in R

Update output: interactive map

output$map <- leaflet::renderLeaflet({

rval_mass_shootings() %>%
leaflet() %>% addTiles() %>% setView( -98.58, 39.82, zoom = 5) %>%
addCircleMarkers(
popup = ~ summary,
radius = ~ fatalities,
fillColor = 'red', color = 'red', weight = 1
)
})
Building Web Applications with Shiny in R

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

Building Web Applications with Shiny in R

Update app: add action button and modal

ui <- bootstrapPage(
  theme = shinythemes::shinytheme('simplex'),
  leaflet::leafletOutput('map', width = '100%', height = '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"),

actionButton('show_about', 'About')
) )
server <- function(input, output, session){

observeEvent(input$show_about, {
showModal(modalDialog(text_about, title = 'About'))
})
}
Building Web Applications with Shiny in R

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

Building Web Applications with Shiny in R

Let's practice!

Building Web Applications with Shiny in R

Preparing Video For Download...