Street Crime UK – Shiny App

综合技术 2018-03-11 阅读原文

(This article was first published on R tutorial for Spatial Statistics , and kindly contributed toR-bloggers)


This is a shiny app to visualize heat maps of Street Crimes across Britain from 2010-12 to 2018-01 and test their spatial pattern.

The code for both ui.R and server.R is available from my GitHub at:


Please be aware that this apps downloads data from my personal Dropbox once it starts and every time the user changes some of the settings. This was the only work-around I could think of to use external data in for free. However, this also makes the app a bit slow, so please be patient.

Users can select a date with two sliders (I personally do not like the dateInput tool), then a crime type and click Draw Map to update the map with new data. I also included a option to plot the Ripley K-function (function Kest in package spatstat ) and the p-value of the quadrat.test (again from spatstat ). Both tools work using the data shown within the screen area, so their results change as users interact with the map. The Ripley K function shows a red dashed line with the expected nearest neighbour distribution of points that are randomly distributed in space (i.e. follow a Poisson distribution). The black line is the one computed from the points shown on screen. If the black line is above the red means the observations shown on the map are clustered, while if it is below the red line means the crimes are scattered regularly in space. A more complete overview of the Ripley K function is available at this link from ESRI .

The p-value from the quadrat test is testing a null hypothesis that the crimes are scattered randomly in space, against an alternative that they are clustered. If the p-value is below 0.05 (significance level of 5%) we can accept the alternative hypothesis that our data are clustered. Please be aware that this test does not account for regularly space crimes.


Please not that the code here is not reproducible straight away. The app communicates with my Dropbox, though the package rdrop2 , which requires a token to download data from Dropbox. More info .

I am sharing the code to potentially use a taken downloaded from elsewhere, but the url that points to my Dropbox will clearly not be shared.

Preparing the dataset

Csv files with crime data can be downloaded directly from the website. Please check the dates carefully, since each of these files contains more that one years of monthly data. The main issue with these data is that they are divided by local police forces, so for example we will have a csv for each month from the Bedfordshire Police, which only covers that part of the country. Moreover, these csv contain a lot of data, not only coordinates; they also contain the type of crimes, plus other details, which we do not need and which makes the full collection a couple of Gb in size.

For these reasons I did some pre-processing, First of all I extracted all csv files into a folder named “CrimeUK” and then I ran the code below:

lista = list.files("E:/CrimesUK",pattern="street",recursive=T,include.dirs=T,full.names=T, = T)

for(i in lista){
  DF = read.csv(i)

   write.table(data.frame(LAT=DF$Latitude, LON=DF$Longitude, TYPE=DF$Crime.type),
               sep=",",row.names=F,col.names=F, append=T)

Here I first create a list of all csv files, with full link, searching inside all sub directory. Then I started a for loop to iterate through the files. The loop simply loads each file and than save part of its contents (namely coordinates and crime type) into new csv named after using year and month. This will help me identify which files to download from Dropbox, based on user inputs.

Once I had these files I simply uploded them to my Dropbox.

The link to test the app is:

A snapshot of the screen is below:




Shiny layout guide(shinydashboard) Shiny的UI界面可用多个方式来展示,你可以用shiny基本布局函数+theme主题函数来实现;也可以用shinydashboard来定义你网页的布局,当然还有shiny的JS插件以及其他技巧。 这里主要介绍一下shiny的一些基本布局函数(对之前Learning Shiny的一些补充)以及s...
Weekly Links: August 5th JavaScript and the Web The Cost Of JavaScript in 2018 | Addy Osmani This was a great breakdown of how exactly large JavaScript bundles ...
Shiny application in production with ShinyProxy, D... You created some great Shiny applications, following our advice of Shiny packaging for example, and you want to put them into production, self-hostin...
shiny用法整理(二) 继续整理shiny的几个用法 DataTables in a Shiny App DT包可以说是shiny网页展示表格的首选(个人觉得哈),其功能丰富,是JS的DataTables库的R端口,功能非常强强大,简单的使用即可满足常用的需求,如: library(DT)ui <- ...
Run shiny R application from Emacs Right now I use Windows box for development at work and I use Emacs, but I needed to run RStudio to run the shiny application that I was working on...