Slide 1

Slide 1 text

Using Shiny to visualise crime and collision data Henry Partridge Data Visualisation Summit, 11 November 2015

Slide 2

Slide 2 text

Outline R What is R? Why use R? Shiny What is Shiny? The structure of a Shiny app A simple Shiny app Shiny app development at TfL · · · · · · · ·

Slide 3

Slide 3 text

About me Data analyst at Transport for London Background in philosophy and crime science Programming in R for two years · · ·

Slide 4

Slide 4 text

R

Slide 5

Slide 5 text

What is R? is a programming language for statistical analysis and data visualisation. Created by Ross Ihaka and Robert Gentleman (University of Auckland). Released in 1995. Implements the S programming language created at Bell Labs. Companies like Google, Facebook and the New York Times use it. · · · ·

Slide 6

Slide 6 text

Why use R? Leading tool for statistics, data analysis, and machine learning Cutting edge analytics: over 7,000 user-contributed packages available on finance, genomics, animal tracking, crime analysis … Powerful graphics and data visualisations: used by New York Times, FiveThirtyEight etc Open source: no vendor lock-in Reproducibility: code can be shared and the results repeated Transparency: explicitly documents all the steps of your analyses Automation: analyses can be run and re-run with new and existing data sets Support network: worldwide community of developers and users · · · · · · · ·

Slide 7

Slide 7 text

Shiny

Slide 8

Slide 8 text

What is Shiny? An R package developed by RStudio (https://www.rstudio.com) that allows data analysts to analyse, visualise and share their results with non-R users. Interactive web applications connected to an R session. No knowledge of HTML, CSS, and JavaScript is required but web apps are customisable and extensible. Integrates with JavaScript libraries. Uses a reactive programming framework. An input is sent to an R process which generates a plot in a web browser. · · · ·

Slide 9

Slide 9 text

The structure of a Shiny app Each Shiny app has two components: a ui script and a server script. The ui specifies the user interface elements, e.g. HTML widgets like dropdowns, sliders, radio buttons etc whilst the server specifies how to generate the output, e.g. table, plot, text. A single app.R file contains both components: You can also use separate ui.R and server.R files. library(shiny) ui <- fluidPage() server <- function(input, output) {} shinyApp(ui = ui, server = server)

Slide 10

Slide 10 text

A simple Shiny app

Slide 11

Slide 11 text

No data available in table Station Line(s) Entry-Exit © Transport for London

Slide 12

Slide 12 text

The code library(shiny) library(png) ; library(grid) ; library(ggplot2) ; library(DT) ; library(dplyr) # data loading and raster rendering code removed for brevity ui <- fluidPage( fluidRow(column(width = 5, dataTableOutput("brush_info")), column(width = 7, align = "left", plotOutput("plot", height = 450, click = "plot_click", brush = brushOpts(id = "plot_brush", fill = "#ce1256")), h6("© Transport for London", align = "right", style = "color:blue"))) ) server <- function(input, output) { output$plot <- renderPlot({ ggplot(stations, aes(long, lat)) + annotation_custom(bimg, xmin=a, xmax=c, ymin=b, ymax=d) + geom_point(size = 1, colour = "white", alpha = 0.1) + ggtitle("Click and drag for station details") + theme(axis.text.x = element_blank(), axis.text.y = element_blank(), axis.ticks.length = unit(0.001,"mm"), panel.background = element_blank(), plot.title = element_text(face = "bold", colour = "red"))}) output$brush_info <- DT::renderDataTable({ brushedPoints(stations, input$plot_brush) %>% select(Station = station, `Line(s)` = lines, `Entry-Exit` = entry_exit) }, rownames = FALSE, extensions = 'Scroller', options = list(deferRender = TRUE, dom = "t", scrollY = 400, scrollCollapse = TRUE)) } shinyApp(ui = ui, server = server)

Slide 13

Slide 13 text

Shiny app development at TfL

Slide 14

Slide 14 text

Crime Harm Index Harm is calculated by multiplying the crime by the number of days in prison that crime would attract according to the sentencing guideline (Sherman et al., 2014). Bus-related Theft is the highest volume category of crime but Robbery is the most harmful.

Slide 15

Slide 15 text

Sentiment analysis of Night Tube tweets Geotagged tweets that mention the Night Tube were mined and plotted according to their sentiment, an estimate of the emotion contained in the tweet's text.

Slide 16

Slide 16 text

Behaviour of electronic points on the railway Dashboard built to interrogate the behaviour of electronic points to help track pre-failure incidents. How long point took to move Mean current needed to move the point · ·

Slide 17

Slide 17 text

STATS19_scanner Web app designed to allow users to interrogate road casualties reported in Greater London between 2005 and 2014.

Slide 18

Slide 18 text

STATS19_scanner R code is available on GitHub (https://github.com/cat-lord/STATS19_scanner) for users to 'fork' and modify change the data re-design the user interface alter the display of the results · · · ·

Slide 19

Slide 19 text

Advantages of using R and Shiny Process of loading, cleaning, manipulating and visualising data possible entirely within R. Lowers barrier of entry to web development. R bindings for JavaScript visualization libraries becoming available all the time. Open source code encourages collaboration. · · · ·

Slide 20

Slide 20 text

Current disadvantages Programming skills App hosting Use of sensitive data · · ·

Slide 21

Slide 21 text

The future of R and Shiny at TfL The R community within TfL is growing fast. Increasing demand for developing Shiny apps (and interactive documents) for internal and external use. Promoting external collaboration in Shiny app development. · · ·

Slide 22

Slide 22 text