-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.R
More file actions
executable file
·206 lines (173 loc) · 6.44 KB
/
Copy pathserver.R
File metadata and controls
executable file
·206 lines (173 loc) · 6.44 KB
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
library(ggplot2)
library(plotly)
library(scales)
library(leaflet)
source("global.R")
function(input,output){
#311 outputs
#by borough
output$bar_311_by_borough <- renderPlot({
#unspecified logic
if(input$exclude_unspecified == TRUE) {
df_311subset <- df_311subset[!df_311subset$Borough %in% c('Unspecified'),]
}
g <- ggplot(data = df_311subset, aes(x = Borough))
g <- g+geom_bar(aes(fill = Year))
g <- g+ggtitle("311 Heating Complaints by Borough")
g <- g+theme(legend.title=element_text(size=14))
g <- g+scale_y_continuous(labels = comma)
g <- g+ylab("Total Complaints")
g
})
#by year
output$bar_311_by_year <- renderPlot({
#unspecified logic
if(input$exclude_unspecified == TRUE) {
df_311subset <- df_311subset[!df_311subset$Borough %in% c('Unspecified'),]
}
g <- ggplot(data = df_311subset, aes(x = Year))
g <- g+geom_bar(aes(fill = Borough))
g <- g+ggtitle("311 Heating Complaints by Year")
g <- g+theme(legend.title=element_text(size=14))
g <- g+scale_y_continuous(labels = comma)
g <- g+ylab("Total Complaints")
g
})
#by Winter
output$bar_311_by_winter <- renderPlot({
#unspecified logic
if(input$exclude_unspecified == TRUE) {
df_311subset <- df_311subset[!df_311subset$Borough %in% c('Unspecified'),]
}
#not winters logic
if(input$exclude_not_winters == TRUE) {
df_311subset <- df_311subset[!is.na(df_311subset$Winters),]
}
g <- ggplot(data = df_311subset, aes(x = Winters))
g <- g+geom_bar(aes(fill = Borough))
g <- g+ggtitle("311 Heating Complaints by Winter (Oct 1st - May 31st)")
g <- g+theme(legend.title=element_text(size=14))
g <- g+scale_y_continuous(labels = comma)
g <- g+ylab("Total Complaints")
g
})
#Heat Seek outputs
output$line_hs <- renderPlotly({
#all data
plotable_data <- df_hs[!is.na(df_hs$temp),]
#filter by address
plotable_data <- plotable_data %>% filter(., clean_address == input$hs_address_select)
#remove outliers logic
if(input$remove_outliers == TRUE) {
plotable_data$temp <- remove_outliers(plotable_data$temp)
}
#highlight violations logic
if(input$group_by_violations == TRUE) {
cols <- c('#62B73A','#F7161A')
# plotable_data$violation <- as.factor(plotable_data$violation, levels = c('Violation','In Compliance'))
plot_ly(
x = ~plotable_data$created_at,
y = ~plotable_data$temp,
name = 'Non-Violations',
type = 'scatter',
color = plotable_data$violation,
colors = cols,
mode = 'markers',
text = paste(plotable_data$created_at, ' - ', plotable_data$temp, ' ºF')
) %>%
layout(
title = 'Remote Sensor Temperature Readings',
xaxis = list(
title = 'Time'
),
yaxis = list(
title = "Temperature (ºF)"
)
)
} else {
plot_ly(
x = ~plotable_data$created_at,
y = ~plotable_data$temp,
type = 'scatter',
mode = 'markers',
text = paste(plotable_data$created_at, ' - ', plotable_data$temp, ' ºF')
) %>%
layout(
title = 'Remote Sensor Temperature Readings',
xaxis = list(
title = 'Time'
),
yaxis = list(
title = "Temperature (ºF)"
)
)
}
})
#sensor map
output$map_hs <- renderLeaflet({
plotable_data <- df_hs[!is.na(df_hs$temp),]
#outliers removal
if(input$remove_outliers == TRUE) {
plotable_data$temp <- remove_outliers(plotable_data$temp)
}
#plottable data logic
if(input$violations_only == TRUE) {
plotable_data <- tbl_df(plotable_data) %>%
filter(violation == 'true')
}
#filter data for popup
unique_sensors <- tbl_df(plotable_data) %>%
group_by(full_address, lat, lon) %>%
summarise(avg_temp=mean(temp, na.rm = TRUE)) %>%
dplyr::select(full_address, avg_temp, lat, lon) %>%
dplyr::filter(!full_address == ', NY NA')
#num violations & violation rate
num_vio <- tbl_df(plotable_data) %>%
group_by(full_address, violation) %>%
count(violation) %>%
dplyr::filter(!full_address == ', NY NA') %>%
dplyr::filter(violation == 'true') %>%
dplyr::select(full_address, n)
num_no_vio <- tbl_df(plotable_data) %>%
group_by(full_address, violation) %>%
count(violation) %>%
dplyr::filter(!full_address == ', NY NA') %>%
dplyr::filter(violation == 'false') %>%
dplyr::select(full_address, n)
unique_sensors <- left_join(unique_sensors, num_vio) %>%
dplyr::rename(violations=n) %>%
left_join(., num_no_vio) %>%
dplyr::rename(no_violations=n)
unique_sensors$violations <- sapply(unique_sensors$violations, function(x) if (is.na(x)) {0} else {x})
unique_sensors <- unique_sensors %>%
mutate(total_readings = violations + no_violations) %>%
mutate(violation_rate = violations / total_readings)
#formatting
unique_sensors$avg_temp <- format(unique_sensors$avg_temp, digits = 3)
unique_sensors$violation_rate <- percent(unique_sensors$violation_rate)
leaflet() %>%
addProviderTiles('CartoDB.Positron') %>%
addMarkers(lng = unique_sensors$lon,
lat = unique_sensors$lat,
popup = paste(sep = "<br/>",
'<b>Address:</b>', unique_sensors$full_address,
'<b>Average Temp (ºF):</b>', unique_sensors$avg_temp,
'<b>Total Hours in Violation:</b>', unique_sensors$violations,
'<b>Total Readings:</b>', unique_sensors$total_readings,
'<b>Violation Rate:</b>', unique_sensors$violation_rate)
)
})
#data
output$data <- renderDataTable(
df_hs,
options = list(
pageLength = 50,
scrollCollapse = TRUE)
)
outputOptions(output, 'bar_311_by_borough', suspendWhenHidden = FALSE)
outputOptions(output, 'bar_311_by_year', suspendWhenHidden = FALSE)
outputOptions(output, 'bar_311_by_winter', suspendWhenHidden = FALSE)
outputOptions(output, 'line_hs', suspendWhenHidden = FALSE)
outputOptions(output, 'map_hs', suspendWhenHidden = FALSE)
outputOptions(output, 'data', suspendWhenHidden = FALSE)
}