-
Notifications
You must be signed in to change notification settings - Fork 0
/
global.R
95 lines (71 loc) · 3.35 KB
/
global.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
library(shiny)
library(shinydashboard)
library(leaflet)
library(htmltools)
library(htmlwidgets)
library(dplyr)
library(ggmap)
library(googleVis)
library(tidyr)
library(shinyWidgets)
#Read in long-term trends data
nyctickettrends = read.csv('nyctickets_longterm.csv')
nyctickettrends$month = as.Date(nyctickettrends$month)
nyctickettrends$quarters = quarters(nyctickettrends$month)
nyctickettrends$yearqtr = paste0(format(nyctickettrends$month, '%Y'), '-', quarters(nyctickettrends$month))
nyctickettrends$boro = cut(nyctickettrends$violation_precinct, breaks=c(0, 35, 53, 95, 116, 150),
labels=c('Manhattan', 'Bronx', 'Brooklyn', 'Queens', 'Staten Island'))
nyctickettrends = filter(nyctickettrends, nyctickettrends$month < '2018-07-01')
nyctickettrends$weekday = factor(nyctickettrends$weekday, levels=0:6, labels=c('Monday', 'Tuesday', 'Wednesday', 'Thursday',
'Friday', 'Saturday', 'Sunday'))
#Define possible fields to split by on the first tab
split_by = c('None', 'Borough', 'Plate Type', 'Violation Category', 'Violation', 'Day of Week')
#Choices for the ticket types
plate_type_choices = c('Passenger', 'Commercial', 'Other')
violation_category_choices = levels(nyctickettrends$group)
#Setup for the precincts tab
metric_choices = c('Tickets (q2 2018)', 'Tickets (q2 2018) normalized by area', 'Change since q2 2014', 'Pct change since q2 2014')
#Read in shape file for police precincts
precincts_df = read.csv('precincts_df_warea.csv')
#Function to calculate the user's desired metric
calc_metric = function(data, metric) {
if(metric == 'Tickets (q2 2018)'){
data %>%
filter(., between(month, as.Date('2018-04-01'), as.Date('2018-06-30'))) %>%
group_by(., violation_precinct) %>%
summarize(., metric=sum(count))
} else if(metric == 'Tickets (q2 2018) normalized by area') {
data %>%
filter(., between(month, as.Date('2018-04-01'), as.Date('2018-06-30'))) %>%
group_by(., violation_precinct) %>%
summarize(., metric=sum(count))
} else if(metric=='Change since q2 2014') {
start = data %>%
filter(., between(month, as.Date('2015-04-01'), as.Date('2015-06-30'))) %>%
group_by(., violation_precinct) %>%
summarize(., start=sum(count))
data %>%
filter(., between(month, as.Date('2018-04-01'), as.Date('2018-06-30'))) %>%
group_by(., violation_precinct) %>%
summarize(., tickets=sum(count)) %>%
inner_join(., start, by='violation_precinct') %>%
mutate(., metric=tickets-start)
} else if(metric=='Pct change since q2 2014') {
start = data %>%
filter(., between(month, as.Date('2015-04-01'), as.Date('2015-06-30'))) %>%
group_by(., violation_precinct) %>%
summarize(., start=sum(count))
data %>%
filter(., between(month, as.Date('2018-04-01'), as.Date('2018-06-30'))) %>%
group_by(., violation_precinct) %>%
summarize(., tickets=sum(count)) %>%
inner_join(., start, by='violation_precinct') %>%
mutate(., metric=tickets/start - 1)
}
}
#Data input for the heat map
tickets=read.csv('bkparking_wgeo.csv')
tickets$dow = factor(tickets$dow,
levels=c('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'),
ordered = T)
tickets = tickets[!is.na(tickets$lat), ]