-
Notifications
You must be signed in to change notification settings - Fork 0
/
appOld.R
273 lines (245 loc) · 12.4 KB
/
appOld.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
library(shiny)
library(dplyr)
library(ggplot2)
library(lubridate)
library(jsonlite)
library(httr)
library(leaflet)
# Load data.
data <- read.csv('rawData.csv')
data1 <- read.csv('data.csv')
censusTracts <- readLines('kcCensusTracts.geojson', warn = FALSE) %>%
paste(collapse = '\n') %>%
fromJSON(simplifyVector = FALSE)
neighborhoods <- readLines('kcNeighborhoods.geojson', warn = FALSE) %>%
paste(collapse = '\n') %>%
fromJSON(simplifyVector = FALSE)
# Set geoJSON style.
censusTracts$style = list(
weight = 1,
color = '#000000',
opacity = 1,
fillOpacity = 0.4
)
neighborhoods$style = list(
weight = 1,
color = '#000000',
opacity = 1,
fillOpacity = 0.4
)
# Format variables.
data1$Stat_Date <- as.character(data1$Stat_Date)
data1$Stat_Date <- as_date(data1$Stat_Date)
# Merge Select Columns from both datasets.
merged <- merge(x = data, y = data1[, c('Report_No', 'Stat_Date')], by = 'Report_No')
# Functions
# Define UI for application that draws a histogram
ui <- fluidPage(
navbarPage(
title = 'KCPD Crime Analysis Project',
theme = 'bootstrap5.css',
tabPanel('Project Overview',
tags$section(
id = 'greetings',
tags$div(
class = 'container',
tags$div(
class = 'row',
tags$div(
class = 'col-lg-12 text-center',
img(src = 'kcpd_patch.png'),
tags$h2(
class = 'section-heading text-uppercase',
"Crime in Kansas City, MO"
),
tags$h3(
class = 'section-subheading text-muted',
"A predictive analysis of KCPD crime data from 2016 thru 2019."
)
)
),
tags$div(
class = 'row',
tags$div(
class = 'col-lg-2'
),
tags$div(
class = 'col-lg-8',
p(
"In 1974, in an experiment conducted by", tags$a(href = 'https://www.policefoundation.org/projects/the-kansas-city-preventive-patrol-experiment/', 'Kelling et al'), " in support of The Police Foundation initiatives, it was concluded that random police patrols had little to no effect on occurrence of crime or the public’s feeling of safety. "
),
p(
"The study suggested that more emphasis should be placed on targeted crime prevention instead of random patrols. A", tags$a(href = 'https://onlinelibrary.wiley.com/doi/full/10.1002/CL2.159', 'review of the effects of police patrols'), " was then published in 2016 showing that hot-spot patrolling did result in a significant reduction of crime in those areas. "
),
p(
"In light of this result, we would like to help the Kansas City Police Department in determining where these hot spots are around the city. To do this we have prepared this application to aid in that endeavor. First we will dive into some basic crime statistics around the city. Then we will present a model using data from 2016 through 2018 on crime in Kansas City. This data was obtained from", tags$a(href = 'https://data.kcmo.org', "The City of Kansas City's Open Data page"), "."
),
p(
"To use this application click on the different tabs to advance through the story and see our interactive visualizations. Our hope is that the KCPD can use this to gain some insight crime hotspots around the city and can find some benefit."
)
),
tags$div(
class = 'col-lg-2'
)
),
tags$div(
class = 'row',
tags$div(
class = 'col-lg-2'
),
tags$div(
class = 'col-lg-8'
),
tags$div(
class = 'col-lg-2'
)
)
)
)
),
tabPanel('Basic Crime Statistics',
tags$div(
class = 'container',
tags$div(
class = 'row',
tags$div(
class = 'col-lg-12 text-center',
tags$h2(
"An Overview of Crime in Kansas City"
)
)
),
tags$div(
class = 'row',
tags$div(
class = 'col-lg-2'
),
tags$div(
class = 'col-lg-8',
p('On this page we Take a look at some of the basic crime stats from the KCPD crime Data from 2016 - 2019. To manipulate the visualisations use the controls on the left.')
),
tags$div(
class = 'col-lg-2'
)
),
tags$div(
class = 'row',
tags$div(
class = 'col-lg-12',
tags$hr()
)
),
tags$div(
class = 'row',
tags$div(
class = 'col-lg-4',
selectInput('offenses',
'Choose an Offense',
choices = sort(unique(merged$Offense.Description), decreasing = FALSE)),
checkboxGroupInput('years',
'Choose the years you want.',
choices = unique(merged$Year),
selected = c(2016, 2017, 2018))
),
tags$div(
class = 'col-lg-8',
tags$p('The final product will include an interactive heatmap that is filterable by Offense and Year. There will also be a section for mouse hover telling what neighborhood you are in.'),
leafletOutput('heatmap')
)
),
tags$div(
class = 'row',
tags$div(
class = 'col-lg-4'
),
tags$div(
class = 'col-lg-8',
tags$br()
)
),
tags$div(
class = 'row',
tags$div(
class = 'col-lg-4'
),
tags$div(
class = 'col-lg-8',
tags$p('And here we have an interactive plot of daily crime counts.'),
plotOutput('CrimePerDay')
)
)
)
),
tabPanel('Hot Spot Modeling',
tags$div(
class = 'container',
tags$div(
class = 'row',
tags$div(
class = 'col-lg-12 text-center',
tags$h2(
"Hot Spot Modeling"
)
)
),
tags$div(
class = 'row',
tags$div(
class = 'col-lg-2'
),
tags$div(
class = 'col-lg-8',
p('On this tab we model the 2019 crime data. Below we see two heatmaps. On the left is the predicted hotspots plotted on a map of Kansas City. To the right are the actual hotspots found in the 2019 data.')
),
tags$div(
class = 'col-lg-2'
)
),
tags$div(
class = 'row',
tags$div(
class = 'col-lg-12',
selectInput('offenses2',
'Choose an Offense',
choices = sort(unique(merged$Offense.Description), decreasing = FALSE))
)
),
tags$div(
class = 'row',
tags$div(
class = 'col-lg-5',
img(src = 'predcrimeplot.png', width = 600, height = 800)
),
tags$div(
class = 'col-lg-2'
),
tags$div(
class = 'col-lg-5',
img(src = 'actualcrimeplot.png', width = 600, height = 800)
)
)
)
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output, session) {
output$CrimePerDay <- renderPlot({
merged %>%
subset(Offense.Description == input$offenses &
Year == input$years) %>%
ggplot(aes(x = Stat_Date)) +
geom_point(stat = 'count', color = 'darkorchid4') +
labs(title = "Daily Offense Count",
y = "Offenses Per Day")
})
output$heatmap <- renderLeaflet({
map <- leaflet(width = 600, height = 800) %>%
addTiles() %>%
setView(lng = mean(data1$Lon), lat = mean(data1$Lat), zoom = 9) %>%
addGeoJSON(neighborhoods, color = 'red')
print(map)
})
}
# Run the application
shinyApp(ui = ui, server = server)