@@ -3,22 +3,23 @@ library(ggvis)
3
3
library(knitr )
4
4
library(stats )
5
5
library(DT )
6
+ require(plyr )
6
7
7
8
shinyServer(function (input , output , session ) {
8
9
img.width <- 700
9
10
img.height <- 400
10
11
options(RCHART_HEIGHT = img.height , RCHART_WIDTH = img.width )
11
12
opts_chunk $ set(fig.width = 7 , fig.height = 4 )
12
-
13
+
13
14
unfiltered = readRDS(" juneAggData_raw.rds" )
14
- filtered = readRDS(" juneAggData .rds" )
15
+ filtered = readRDS(" juneAggData_clean .rds" )
15
16
data = filtered
16
17
stationMeta = readRDS(" stations.rds" )
17
18
lane = " all"
18
19
quant = " speed"
19
20
hour = 8
20
21
weekdays = c(" Mon" ," Tue" ," Wed" ," Thu" ," Fri" ," Sat" ," Sun" )
21
-
22
+
22
23
observeEvent(input $ quant ,({
23
24
quant = tolower(input $ quant )
24
25
if (quant == " speed" ){
@@ -29,7 +30,7 @@ shinyServer(function(input, output, session) {
29
30
}else if (quant == " volume" ){
30
31
binRange = c(100 ,1000 )
31
32
binInc = 100
32
- histStart = 500
33
+ histStart = 200
33
34
binLabel = " Bin Width (vph)"
34
35
}else if (quant == " occupancy" ){
35
36
binRange = c(1 ,10 )
@@ -39,7 +40,7 @@ shinyServer(function(input, output, session) {
39
40
}
40
41
updateSliderInput(session = session , inputId = " hist" ,label = binLabel ,value = histStart ,min = binRange [1 ],max = binRange [2 ],step = binInc )
41
42
}))
42
-
43
+
43
44
plotFrame = reactive({
44
45
laneSelect = input $ lane
45
46
if (laneSelect == " All (Aggregated)" ){
@@ -48,40 +49,41 @@ shinyServer(function(input, output, session) {
48
49
lane <<- laneSelect
49
50
}
50
51
# print(lane)
51
-
52
+
52
53
if (input $ filter == " Unfiltered" ){
53
54
data = unfiltered
54
55
}else {
55
56
data = filtered
56
57
}
57
-
58
+
58
59
hour <<- input $ hour
59
60
weekdays <<- input $ weekdays
60
-
61
+
61
62
quant <<- tolower(input $ quant )
62
63
# print(quant)
63
64
# print(weekdays)
64
65
# print(hour)
65
66
# print(lane)
66
-
67
+
67
68
pf = data.frame (matrix (nrow = length(data ),ncol = 3 ))
68
69
colnames(pf )= c(" station_id" ," quantity" ," out" )
69
70
if (lane == " all" ){
70
71
for (i in 1 : length(data )){
71
- if (! is.null (data [[ i ] ])){
72
- sid = data [[ i ]] $ station_id
72
+ if (! is.na (data [i ])){
73
+ sid = as.numeric(names( data [i ]))
73
74
pf $ station_id [i ]= sid
74
- sData = data [[i ]]$ data
75
+ sData = data [[i ]]
75
76
if (is.data.frame(sData )){
76
77
rows = sData $ hour == hour & sData $ dow %in% weekdays
77
78
if (sum(rows > 0 )){
78
79
sub = sData [rows ,]
79
80
if (quant == " volume" ){
80
- q = sum(sub $ volume )
81
+ temp = ddply(sub ," lanenumber" ,summarise ,volume = sum(volume ))
82
+ q = mean(temp $ volume , na.rm = TRUE )
81
83
}else if (quant == " occupancy" ){
82
- q = mean(sub $ occupancy )
84
+ q = mean(sub $ occupancy , na.rm = TRUE )
83
85
}else if (quant == " speed" ){
84
- q = weighted.mean(sub $ speed ,sub $ volume )
86
+ q = weighted.mean(sub $ speed ,sub $ volume , na.rm = TRUE )
85
87
}
86
88
pf $ quantity [i ]= q
87
89
}else {
@@ -96,7 +98,7 @@ shinyServer(function(input, output, session) {
96
98
}
97
99
}
98
100
}
99
- #
101
+ #
100
102
# else if(lane=="On-Ramp"){
101
103
# for (i in 1:length(data)){
102
104
# if(!is.null(data[[i]])){
@@ -133,20 +135,20 @@ shinyServer(function(input, output, session) {
133
135
# }
134
136
else {
135
137
for (i in 1 : length(data )){
136
- if (! is.null (data [[ i ] ])){
137
- sid = data [[ i ]] $ station_id
138
+ if (! is.na (data [i ])){
139
+ sid = as.numeric(names( data [i ]))
138
140
pf $ station_id [i ]= sid
139
- sData = data [[i ]]$ data
141
+ sData = data [[i ]]
140
142
if (is.data.frame(sData )){
141
143
rows = sData $ hour == hour & sData $ dow %in% weekdays & as.character(sData $ lanenumber )== lane
142
144
if (sum(rows )> 1 ){
143
145
sub = sData [rows ,]
144
146
if (quant == " volume" ){
145
- q = mean(sub $ volume )
147
+ q = mean(sub $ volume , na.rm = TRUE )
146
148
}else if (quant == " occupancy" ){
147
- q = mean(sub $ occupancy )
149
+ q = mean(sub $ occupancy , na.rm = TRUE )
148
150
}else if (quant == " speed" ){
149
- q = mean(sub $ speed )
151
+ q = mean(sub $ speed , na.rm = TRUE )
150
152
}
151
153
pf $ quantity [i ]= q
152
154
}else {
@@ -159,12 +161,12 @@ shinyServer(function(input, output, session) {
159
161
pf $ station_id [i ]= NA
160
162
pf $ quantity [i ]= NA
161
163
}
162
-
164
+
163
165
}
164
166
}
165
167
pf $ out = NA
166
168
comp = pf [complete.cases(pf [,1 : 2 ]),]
167
-
169
+
168
170
if (nrow(comp )> 0 ){
169
171
quantiles = quantile(comp $ quantity ,c(0.25 ,0.75 ),na.rm = TRUE )
170
172
iqr = as.numeric(quantiles [2 ]- quantiles [1 ])
@@ -174,64 +176,64 @@ shinyServer(function(input, output, session) {
174
176
comp $ out [(comp $ quantity < fences [1 ])| (comp $ quantity > fences [4 ])]= " Major"
175
177
comp $ out = factor (comp $ out ,ordered = TRUE )
176
178
}
177
-
179
+
178
180
return (comp )
179
181
})
180
-
182
+
181
183
plt = reactive({
182
184
quant = input $ quant
183
-
185
+
184
186
# print(quant)
185
-
187
+
186
188
if (quant == " Speed" ){
187
189
xAxis = " Speed (mph)"
188
-
190
+
189
191
}else if (quant == " Volume" ){
190
192
xAxis = " Volume (vph)"
191
193
}else if (quant == " Occupancy" ){
192
194
xAxis = " Occupancy (% of time)"
193
195
}
194
-
196
+
195
197
h <- plotFrame %> % ggvis(x = ~ quantity ,fill = ~ out ) %> % group_by(out ) %> %
196
- # layer_histograms(width=input$hist) %>%
198
+ layer_histograms(width = input $ hist ) %> %
197
199
scale_nominal(" fill" , label = " Outlier Status" ,
198
200
domain = c(" OK" , " Minor" , " Major" ),
199
201
range = c(" green" , " yellow" , " red" )) %> %
200
- set_options(width = img.width , height = img.height ) %> %
202
+ set_options(width = img.width , height = img.height ) %> %
201
203
add_axis(" x" , title = xAxis )%> %
202
- add_axis(" y" , title = " Frequency" )
203
-
204
+ add_axis(" y" , title = " Frequency" )
205
+
204
206
return (h )
205
207
})
206
-
208
+
207
209
densityPlot = reactive({
208
210
quant = input $ quant
209
-
211
+
210
212
# print(quant)
211
-
213
+
212
214
if (quant == " Speed" ){
213
215
xAxis = " Speed (mph)"
214
-
216
+
215
217
}else if (quant == " Volume" ){
216
218
xAxis = " Volume (vph)"
217
219
}else if (quant == " Occupancy" ){
218
220
xAxis = " Occupancy (% of time)"
219
221
}
220
-
222
+
221
223
h <- plotFrame %> % ggvis(x = ~ quantity ) %> %
222
- set_options(width = img.width , height = img.height ) %> %
224
+ set_options(width = img.width , height = img.height ) %> %
223
225
add_axis(" x" , title = xAxis )%> %
224
226
add_axis(" y" , title = " Probability Density" ) %> %
225
227
layer_densities(
226
228
adjust = input $ bandwidth ,kernel = " gaussian" )
227
-
229
+
228
230
return (h )
229
231
})
230
-
232
+
231
233
plt %> % bind_shiny(" ggvis" , " ggvis_ui" )
232
-
234
+
233
235
densityPlot %> % bind_shiny(" ggvis_d" , " ggvis_ui_d" )
234
-
236
+
235
237
output $ mytable = renderDataTable({
236
238
df = plotFrame()
237
239
quantity = input $ quant
@@ -247,18 +249,18 @@ shinyServer(function(input, output, session) {
247
249
for (i in 1 : nrow(df )){
248
250
locations [i ]= stationMeta $ locationtext [stationMeta $ stationid == df $ `Station ID` [i ]][1 ]
249
251
}
250
-
252
+
251
253
createLink <- function (val ) {
252
254
sprintf(' <a href="http://portal.its.pdx.edu/Portal/index.php/stations/view/id/%s/" target="_blank" class="btn btn-primary">%s</a>' ,val ,val )
253
255
}
254
-
256
+
255
257
links = createLink(df $ `Station ID` )
256
-
258
+
257
259
outTable = cbind(links ,locations ,df [,2 : 3 ])
258
260
colnames(outTable )= c(" Station ID" ," Location" ,qLab ," Outlier Status" )
259
261
datatable(outTable ,escape = FALSE ) %> % formatStyle(" Outlier Status" ,backgroundColor = styleEqual(c(" OK" ," Minor" ," Major" ),c(" white" ," yellow" ," red" )))
260
262
})
261
-
263
+
262
264
output $ sumTable = renderDataTable({
263
265
df = plotFrame()
264
266
statSum = as.data.frame(as.matrix(summary(df $ quantity )))
@@ -274,6 +276,6 @@ shinyServer(function(input, output, session) {
274
276
colnames(final )= c(" Statistic" ," Value" )
275
277
datatable(final ) %> % formatStyle(" Statistic" ,backgroundColor = styleEqual(c(" Lower Outer Fence" ," Lower Inner Fence" ," Upper Inner Fence" ," Upper Outer Fence" ),c(" red" ," yellow" ," yellow" ," red" )))
276
278
})
277
-
279
+
278
280
})
279
281
0 commit comments