Introducing 'CrimeMap'

Using R, Shiny and ShinyApps for Quick Web App Development

Jo-fai Chow (University of Exeter & XP Solutions)
Final Year EngD Candidate in Hydroinformatics (Water + Machine Learning)

Agenda


  1. Why R?
  2. Introducing 'CrimeMap'
  3. What's Next?
  4. Beyond Data and Maps
  5. Conclusions
  6. Q & A



Above: Hadley Wickham's LondonR talk on dplyr and ggvis back in July 2013 (my first ever LondonR experience).

Why R ?

What People Think I Do

peoplethink

What People Think I Do

peoplethink

R Can Do That ?

cat

... and more !?

Thanks to Tal Galili's

cat

Taking My Leap of Faith (Jan - Mar 2013)


Data Analysis (Coursera)

  • MOOC by Jeff Leek

  • Practical data analysis using R

  • Peer reviewed assignments

  • Kaggle Competition


The 'R' Awesomeness

  • Data Manipulation (reshape2, plyr)
  • Machine Learning (caret)
  • Parallel Processing (foreach, doSNOW)
  • Big Data (bigmemory, ff, RHadoop)
  • Visualisation (rCharts, ggplot2, animation)
  • Dynamic Documentation (knitr, roxygen2)
  • User Community (LondonR)
  • Active Development of New Packages (ggvis, dplyr, SparkR ...)

Introducing 'CrimeMap'

CrimeMap - The Original by David Kahle

ggmap

dk_map1

CrimeMap - Maybe I can try it with different data?

Is there any?

results

Open Data and API!

Download crime data within a 1 mile radius of a single point (JSON)

http://data.police.uk/api/
       crimes-street/all-crime?
      lat=52.629729&
      lng=-1.131592&
      date=2012-04

Raw Data in JSON format

{"category" : "anti-social-behaviour", 
 "month"    : "2012-04", 
 "location" : {
  "latitude"  : "52.640374", 
  "longitude" : "-1.122319",
  "street"    : {
   "id"   : 884330, 
   "name" : "On or near Christow Street"}}, 
  ... and some other stuff}


Improvement since then

Specific area (JSON) and batch download (CSV).

Putting it together!

First Milestone: A Wrapper Function (June 2013)

crimeplot.wrapper <- function(

    ## user-defined location
    point.of.interest = "London Eye", 

    ## period of time in YYYY-MM
    period = c("2013-01","2013-02",
               "2013-03","2013-04"),

    ## terrain, satellite, hybrid
    type.map          = "roadmap",

    ## month, category, type
    type.facet        = NA

    ...)   

london

CrimeMap - Application Examples (Map Type)

brighton

Brighton (Map: Hybrid)

bristol

Bristol (Map: Terrain)

CrimeMap - Application Examples (Facet)


shef

Sheffield (by Category)


exeter

Exeter (by Month)

Making it Shiny!

CrimeMap - Making it Shiny!


Shiny

"Shiny makes it super simple for R users like you to turn analyses into interactive web applications that anyone can use." - RStudio


ShinyApps Server

"ShinyApps is a platform as a service (PaaS) for hosting Shiny applications ... allowing you to create your online account, and deploy your first Shiny application to the cloud." - RStudio


Motivation

"It looks fun! Maybe I can learn a new trick!"


Challenges

"Can I make it super user-friendly ... so easy that even my mom knows how to use it?"

"Is it really possible? I have NO prior web app development experience at all."

"Will it work on mobile devices?"

Second Milestone: Shiny Web App (Nov 2013)

CrimeMap - Keep It Simple, Stupid

Basic Controls

basic

Push the Button!

london

Output
london

CrimeMap - Control Freak !!!

More Controls
more

What's Your Favourite?
more

Sharing it with the world!

CrimeMap - Hosting it with ShinyApps

Set Credentials

setAccountInfo(name   = "blenditbayes", 
               token  = "your_token", 
               secret = "your_secret") 

Deploy it!

deployApp(appDir  = "your_app_dir",
          appName = "your_app_name")

Configure Instance Type

configureApp(appName = "crimemap", 
             size    = "xlarge") 

Available Options

Instance Memory Size
small (default) 256 MB
medium 512 MB
large 1024 MB
xlarge 2048 MB
xxlarge 4096 MB


For More Information

  1. ShinyApps (Hassle-free Cloud Hosting) Link
  2. Shiny Server (Host Your Own) - Free and Professional Version Link

CrimeMap - Using it on iPhone 4s & Nexus 7

iphone

nexus7

CrimeMap - Forked Projects

Summary of Experience

CrimeMap - Summary of My Shiny Experience


Developing with Shiny

  1. Rapid development - from noob to my first web app in a few days!
  2. Default layout is nice and simple. You can further customise it with CSS.
  3. It works on mobile devices!
  4. Easy code sharing.
  5. Even my mom knows how to use it!!!


Hosting with ShinyApps

  1. Hassle-free. No need to worry about maintenance and data usage.
  2. Deploy (or update) your app in minutes.
  3. Great support from the RStudio team!

What's Next?

Discovering Cool Stuff from Ramnath Vaidyanathan

You've probably seen ...

... and now 'rMaps' !

What if ... rMaps + CrimeMap?

Introducing Package 'rCrimemap'

Prerequisites

require(devtools)
install.packages(c("ggmap", "rjson",
                   "dplyr"))
install_github('ramnathv/rCharts@dev')
install_github('ramnathv/rMaps')

Latest RStudio IDE (v0.98.501+)


Install 'rCrimemap'

install_github('woobe/rCrimemap')

Function 'rcmap()'

rcmap(
  location = "Ball Brothers EC3R 7PP", 
  period = "2014-01",
  type = "All",
  map_size = c(1000, 500), 
  provider = "Nokia.normalDay",
  zoom = 10)


Codes bit.ly/rCrimemap

Introducing Package 'rCrimemap' - LondonR Demo

require(rCrimemap); rcmap("Ball Brothers EC3R 7PP");

# [rCrimemap]: Downloading '2010-12.rda' from author's Bitbucket account ...
# [rCrimemap]: Converting raw data into JSON format for Leaflet ...
# [rCrimemap]: Creating Leaflet with Heat Map ...

# [rCrimemap]: =======================================================
# [rCrimemap]: Summary of Crime Data Used and Leaflet Map
# [rCrimemap]: =======================================================

# Point of Interest           : Ball Brothers EC3R 7PP 
# Police Force(s)             : Metropolitan Police Service City of London Police 
# Period of Crime Records     : 2010-12 
# Type of Crime Records       : All 
# Total No. of Crime Records  : 93077 
# Map Resolution              : 1000 x 500 

Introducing Package 'rCrimemap' - LondonR Demo

rcmap("Ball Brothers EC3R 7PP", 
      "2011-08", "All", c(1000,1000),
      "Nokia.normalDay")

rcmap("Manchester", 
      "2014-01", "All", c(1000,1000), 
      "MapQuestOpen.OSM")

Beyond Data Points and Fancy Graphs

Related BBC Articles

Beyond Exploratory Analysis


  • Can we trust the data?
  • Can we improve the data?
  • Can we learn from the data?
  • Can we predict crime rate?
  • Can we optimise the resources?
  • Can we build a new decision support system?

Conclusions

Conclusions

  • Reasons why I shifted to R
    • R is excellent for data mining, visualisation, big data, dynamic doc etc.
  • CrimeMap
    • a wonderful learning experience.
    • a case study for Shiny and ShinyApps.
  • Things keep evolving
    • rMaps + CrimeMap = rCrimemap

Want to Find Out More About Shiny?

John M. Chambers on R



"R has had a revolutionary effect on the way statistics are communicated ..."



"... for many things now, people produce the code, they produce an R package, an instantly, everyone else can use what they've got freely and contribute their ideas to let things evolve."

F.Y.I.

Acknowledgement

Thanks For

Coursera & Jeff Leek

Data Analysis Online Course

David Kahle & Hadley Wickham

ggmap and ggplot2

data.police.uk

Open Crime Data and API

RStudio Team (esp. Tareef Kawaf)

RStudio IDE, Shiny & ShinyApps

Chris Beeley & Packt

For the Shiny Cookbook

Ramnath Vaidyanathan

Slidify, rCharts and rMaps

... and, most importantly, of course ...

Mango Solutions

... kindly sponsoring and supporting

LondonR since 2009!

Thank You for Your Attention!