City Traffic Accidents

Marco Antonio Gonzalez Junior
November, 2015.

This project is part of the Developing Data Products course from the Data Science Specialization offered by the Johns Hopkins University through Coursera.

  • This project's goal is to compare the total number of traffic accidents by transport type

  • The dataset is from a city called Recife (state of Pernambuco, Brazil) and contains all registered occurrences for the current year: 2015.

  • The data shows the incredible majority of accidents are caused by motorbikes.

  • The dataset can be found here

Dataset: exploratory data analysis

        date      type occurrences
1 01/01/2015      Bike           1
2 01/01/2015     Other           1
3 02/01/2015 Motorbike           1
4 02/01/2015      Bike           1
5 03/01/2015 Motorbike           1
6 03/01/2015 Motorbike           1
7 03/01/2015       Car           1
8 03/01/2015     Other           1

Shiny: User Interface

This code snippet renders the input control to choose which accidents types to plot:

  output$typeInput <- renderUI({
    checkboxGroupInput("accident_types", "Accident types:", accident_types, selected = "Car")
  })

Shiny: Server Side

This following code snippets are responsible to deal with user selections and change the data accordingly.

    newdata <- reactive({
    temp <- merge(
      data.table(tipo = accident_types),
      data[tipo %in% input$accident_types, list(total=sum(quantidade)), by=list(tipo)], by=c('tipo'), all=TRUE)
    temp
  })
  output$accidentsChart <- renderChart({
    rdata <- newdata()
    types <- input$accident_types
    accidents <- subset(rdata, rdata$tipo == tolower(types))
    rdata <- rdata[order(-rdata$total),]
    accidentsChart <- Highcharts$new()
    accidentsChart$addParams(width = 500, height = 200, dom = 'accidentsChart')
    accidentsChart$series(data = rdata$total, type = 'bar', name = "# of Accidents")
    accidentsChart$xAxis(categories = rdata$tipo)
    return(accidentsChart)
  })