<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/street-crime-uk-shiny-app/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-video-tutorial.blogspot.com/2018/03/street-crime-uk-shiny-app.html"> R tutorial for Spatial Statistics</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h3 color>Introduction</h3>
<div color>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.</div>
<div color>The code for both <i><u>ui.R</u></i> and <i><u>server.R</u></i> is available from my <b>GitHub </b>at: <a rel="nofollow" href="https://github.com/fveronesi/StreetCrimeUK_Shiny" target="_blank">https://github.com/fveronesi/StreetCrimeUK_Shiny</a></div>
<div color></div>
<h3 color>Usage</h3>
<div color>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 shinyapps.io for free. However, <b>this also makes the app a bit slow, so please be patient.</b></div>
<div color>Users can select a date with two sliders (I personally do not like the <code>dateInput</code> tool), then a crime type and click <code>Draw Map</code> to update the map with new data. I also included a option to plot the Ripley K-function (function <code>Kest</code> in package <code>spatstat</code>) and the <em>p-value</em> of the <code>quadrat.test</code> (again from <code>spatstat</code>). 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 <a rel="nofollow" href="http://pro.arcgis.com/en/pro-app/tool-reference/spatial-statistics/h-how-multi-distance-spatial-cluster-analysis-ripl.htm" target="_blank">link from ESRI</a>.</div>
<div color>The <em>p-value</em> from the <a rel="nofollow" href="https://www.rdocumentation.org/packages/spatstat/versions/1.55-0/topics/quadrat.test?tap_a=5644-dce66f&tap_s=10907-287229" target="_blank">quadrat test</a> is testing a null hypothesis that the crimes are scattered randomly in space, against an alternative that they are clustered. If the <em>p-value</em> 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.</div>
<div color></div>
<h3 color><i>NOTE</i></h3>
<div color>Please not that the code here is not reproducible straight away. The app communicates with my Dropbox, though the package <code>rdrop2</code>, which requires a token to download data from Dropbox. More info <a rel="nofollow" href="https://github.com/karthik/rdrop2" target="_blank">github.com/karthik/rdrop2</a>.</div>
<div color>I am sharing the code to potentially use a taken downloaded from elsewhere, but the <code>url</code> that points to my Dropbox will clearly not be shared.</div>
<div color></div>
<h3 color>Preparing the dataset</h3>
<div color>Csv files with crime data can be downloaded directly from the <a rel="nofollow" href="https://data.police.uk/data/archive/" target="_blank">data.police.uk</a> 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.</div>
<div color>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:</div>
<pre lang="{r}"><code>lista = list.files(&quot;E:/CrimesUK&quot;,pattern=&quot;street&quot;,recursive=T,include.dirs=T,full.names=T,ignore.case = T)<br><br>for(i in lista){<br>  DF = read.csv(i)<br><br>   write.table(data.frame(LAT=DF$Latitude, LON=DF$Longitude, TYPE=DF$Crime.type),<br>               file=paste0(&quot;E:/CrimesUK/CrimesUK&quot;,substr(paste(DF$Month[1]),1,4),&quot;_&quot;,substr(paste(DF$Month[1]),6,7),&quot;.csv&quot;),<br>               sep=&quot;,&quot;,row.names=F,col.names=F, append=T)<br>   print(i)<br>}<br></code></pre>
<div color>Here I first create a list of all csv files, with full link, searching inside all sub directory. Then I started a <code>for</code> 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.</div>
<div color>Once I had these files I simply uploded them to my Dropbox.</div>
<div color></div>
<div color>The link to test the app is:</div>
<h3><a rel="nofollow" href="https://fveronesi.shinyapps.io/CrimeUK/" target="_blank">fveronesi.shinyapps.io/CrimeUK/</a></h3>
<p></p>
<div color>A snapshot of the screen is below:</div>
<div><a rel="nofollow" href="https://i2.wp.com/github.com/fveronesi/StreetCrimeUK_Shiny/raw/master/Screenshot.jpg?ssl=1" target="_blank"><img border="0" src="https://i2.wp.com/github.com/fveronesi/StreetCrimeUK_Shiny/raw/master/Screenshot.jpg?resize=450%2C450&ssl=1" data-original-width="450" width="450" data-original-height="450" data-recalc-dims="1"></a></div>
<div color></div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://r-video-tutorial.blogspot.com/2018/03/street-crime-uk-shiny-app.html"> R tutorial for Spatial Statistics</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/qbl4Y-j-olU" height="1"></a></div>