Create an Interactive Crime Map Using Shiny
Before we start
This is the very first post of my blog, Doctrine of the Mean. You can play around this demo in Shiny server. Also if you’re interested, the code is in my Github repo.
This post will give a brief introduction in using Shiny to create an interactive crime map and deploy it on ShinyApps.io. For details on how Shiny works, please check Shiny’s Documentation
At the end of the day, you can build your own crime map like I did above!
Set up
R packages will be used in making of the map:
Not so much, huh? Only 3 must-use packages. The rest are totally opitonal, you can absolutely add more widgets to enhance your map.
Fantastic Data and Where to Find Them
Data is not like Pokemon you can easily catch(at least not something like Pidgey). Originally, the data we used in this map is from DC Metropolitan Police Department, hosted by Socrata. For some reason after July, the data link provided in the Shiny App is no longer publicly accessible. Luckily, I saved the data between early 2013 to March 2016. Feel free to download it if you want follow along.
If you want to reproduce your own crime map, you can find a lot public crime report data from Socrata online database.
Shiny Architecture
Since I’m not very good at summarizing concepts, I will just quote from Shiny’s official homepage.
Shiny is a web applicaton framework for R
Turn your analysis into interactive web applications. No HTML, CSS, or JavaScript knowledge required
Yes, Shiny is as simple as it sounds!
Basically, if you’re a completely HTML/CSS newbie and also luckily know some R like me, then bang! Shiny is the savior for you.
With Shiny, you can easily build a web app just like building a lego.
In order to avoid lego epic fail like above, we need to have a brief understanding of the logic shiny organize everything together.
To build an app using Shiny, every app needs at least two components:
* a user-interface script, ui.R
* a server script, server.R
The ui.R
script controls the layout and appearance of your app. It receives user input data and transfer them to server.R
for back-end functions and graphs use.
The server.R
script contains all instructions and models for back-end computations and data processing. It uses input data from ui.R
to feed functions and output results to front-end to interact with users.
Work, Work
Data Pre-processing
The data Socrata provides is already well organized. We only need to make a few tweaks to make it easier for our afterwards processing.
UI
Shiny’s aesthetics is simple, everything is arranged in blocks. It’s your choice on how to divide and rearrange them.
Shinydashboard
provides an easy R interface to build a Bootstrap style website. In other words, besides calling Shinydashboard
function like regular R packages, if you happen to know some knowledge of HTML/CSS, there is no limit to your own customizable web design.
A dashboardPage
consists of three components, a header
, a sidebar
, a body
.
header
is as intuitive as it sounds. It’s the function we used to define our page title.
To make it easier, we disable sidebar
, and put all contents into the body
. Shiny adapts Bootstrap grid system. The overall width of a region is 12. Therefore, I set left 4 for dashboard and right 8 for the map.
For our customizable dashboard, we want to use it to select crime type, date range, day of week, time of day and a submit button to explicitly submit our input to server.R
to process. We will use Shiny input widgets to achieve our goals. The first parameter of all widgets functions we used is its inputID
, which will be referenced in server.R
to access the user input data. Likewise, we’ll also reference outputID
from server.R
to create ui elements.
Server
server.R
will reference user input data by inputID
defined in ui.R
and also define outputID
when create content render for ui.R
use.
Till now, we’ve finished scripting part. Now you can click on Run App to see how your baby looks like.
Hmm.., looks quite nice, isn’t it? Although it is a very simple version of a crime map, be proud of yourself if it’s your first Shiny app. There is always room to add more htmlwidgets to enrich your app step by step.
Deploy to ShinyApps.io
Once you finished testing and final checking your code, it’s time to showcase your baby to the world!
Since ShinyApps.io is also a RStudio product, we can have painless seamless one-click service to publish your apps online.
First you will need a ShinyApps.io account. Then at top right corner of your code pane, there is a blue drop down button let you manage your published applciations and accounts.
For first time user, click either Publish Application or Manage Acconts. RStudio will first install some necessary packages for deployment. Then you will get a prompt to ask you to connect your account. We will choose ShinyApps.io here.
The next page is censored because you need to enter your ShinyApps.io API key here. Once you connect your account to RStudio, you’re ready to deploy it to your own ShinyApps.io account.
And, voila! That’s it! You have your own crime report map with a customizable dashboard.
Please comment below if you have any questions or suggestions about this tutorial or my code!
Leave a Comment