forked from npct/pct-shiny
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpct-shiny-funs.R
251 lines (188 loc) · 9.16 KB
/
pct-shiny-funs.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
# Data Frame which contains the links of lines and their colours
lineAndColourDF <- data.frame(
lineType = c("straight_line", "quieter_route", "faster_route", "route_network"),
lineColour = c("maroon","turquoise","purple","blue")
)
getLineColour <- function(lineType){
lineAndColourDF$lineColour[lineAndColourDF$lineType == lineType]
}
numericLineColNames <- c(
"Average Slope" = "avslope",
"Straight Line Distance (km)" = "dist",
"Cirquity" = "cirquity",
"Fastest Route Distance (km)" = "dist_fast",
"Quietest Route Distance (km)" = "dist_quiet",
"Cycling Observed (%)" = "clc",
"Cyclists at Government target" = "govtarget_slc",
"Increase at Government target" = "govtarget_sic",
"Cyclists at gender equality" = "gendereq_slc",
"Increase at gender equality" = "gendereq_sic",
"Cyclists at Dutch levels" = "dutch_slc",
"Increase at Dutch levels" = "dutch_sic",
"Cyclists at Ebikes scenario" = "ebike_slc",
"Increase at Ebikes scenario" = "ebike_sic"
)
lineColNames <- c(
"Start and end zones" = "id",
"All commutes" = "All",
"Rail" = "Rail",
"Bus" = "Bus",
"Car" = "Car_driver",
"Bicycle" = "Bicycle",
numericLineColNames
)
numericZoneColNames <- c(
"Average slope" = "avslope",
"Cyclists in census" = "Bicycle",
"Cyclists at govenment target" = "govtarget_slc",
"Increase at govenment target" = "govtarget_sic",
"Cyclists at gender equality" = "gendereq_slc",
"Increase at gender equality" = "gendereq_sic",
"Cyclists at Dutch levels" = "dutch_slc",
"Increase at Dutch levels" = "dutch_sic",
"Cyclists at Ebike scenario" = "ebike_slc",
"Increase at Ebike scenario" = "ebike_sic"
)
zoneColNames <- c(
"Geo code" = "geo_code",
"Geo label" = "geo_label",
numericZoneColNames
)
scenariosNames <- c(
"olc" = "Cyclists (recorded)",
"slc" = "Cyclists (scenario)"
)
# Normalise the data ready for plotting
normalise <- function(values, min = 0, max = 1){
min + max * (values - min(values))/diff(range(values))
}
# Generate a series of colours based on the input range
getColourRamp <- function(colors, values) {
v <- normalise(values)
x <- colorRamp(colors)(v)
x[is.na(x)] <- 1
rgb(x[,1], x[,2], x[,3], maxColorValue = 255)
}
dataFilter <- function(scenario, type){
ifelse(scenario == "olc", "Bicycle", paste(scenario, type, sep = "_"))
}
tableStart <- '<table><tbody>'
tableEnd <- '</table></tbody>'
# SLC and SIC NOT IN 'olc' scenario
tableOLC <- knitr::kable(data.frame(
Attribute =c("Total commuters:\t", "Cyclists (baseline):\t", "Drivers (baseline):\t"),
Value =c("%s" ,"%s (%s%%)" , "%s (%s%%)" )), format="html", col.names=NULL)
#scenarios table
tableCommon <- knitr::kable(data.frame(
Attribute = c("Total commuters:\t", "Cyclists (baseline):\t", "Cyclists (scenario):\t",
"Change in no. cyclists:\t", "Change in no. drivers:\t", "Change in no. deaths/year:\t",
"Value of deaths avoided (pounds/year):\t ", "Change in CO2 (e/year):\t"),
Value = c("%s", "%s (%s%%)" , "%s (%s%%)",
"%s", "%s", "%s",
" %s", "%s T.")), format="html", col.names=NULL)
# Popup function for straight line data in html table
straightPopup <- function(data, scenario){
if(scenario == 'olc') {
scenario <- 'base'
tableInterm <- sprintf(paste0(tableOLC, '<tr>
<td> Distance: </td>
<td> %s km</td>
</tr>'),
data$All, data$Bicycle, round(100 * data$Bicycle / data$All),
data$Car_driver, round(100 * data$Car_driver / data$All),round(data$dist, 1))
}
else {
tableInterm <- sprintf(paste0(tableCommon, '<tr>
<td> Distance: </td>
<td> %s km</td>
</tr>'),
data$All, data$Bicycle, round(100 * data$Bicycle / data$All), #baseline & %
round(data[[dataFilter(scenario, "slc")]]), round(100*data[[dataFilter(scenario, "slc")]]/ data$All), # slc, slc%
round(data[[dataFilter(scenario, "sic")]]), round(data[[dataFilter(scenario, "sid")]]), #changes sic, sid
round(data[[dataFilter(scenario, "sideath_heat")]],2), round(data[[dataFilter(scenario, "sivalue_heat")]],1), #heat, heat value
round(data[[dataFilter(scenario, "sico2")]]/1000,1), round(data$dist, 1) ) #co2, distance
}
paste(tableStart,tableInterm,tableEnd)
}
routeTypeLabel <- NULL
routeTypeLabel[['fastest']] <- 'Direct'
routeTypeLabel[['quietest']] <- 'Quiet'
# Route popup function
routePopup <- function(data, scenario){
blob <- knitr::kable(data.frame(
Attribute = c("Route Distance:\t \t", "Hilliness (av. gradient):\t \t"),
Value = c("%s km" , "%s (%%)" )), format="html", col.names=NULL)
ifelse(("rqincr" %in% colnames(data@data)), routeType <-'quiet', routeType <-'fast')
if(scenario == 'olc') {
scenario <- 'base'
tableInterm <- sprintf(paste0(tableOLC,blob),
data$All, data$Bicycle, round(100 * data$Bicycle / data$All),
data$Car_driver, round(100 * data$Car_driver/ data$All),
round(data$dist_fast, 1), round(100*data$av_incline, 1) )
} #olc
else {
tableInterm <- sprintf(paste0(tableCommon,blob),
data$All, data$Bicycle, round(100 * data$Bicycle / data$All), # olc, olc%
round(data[[dataFilter(scenario, "slc")]]),round(100*data[[dataFilter(scenario, "slc")]]/ data$All), # slc, slc%
round(data[[dataFilter(scenario, "sic")]]), round(data[[dataFilter(scenario, "sid")]]), #change: sic, sid
round(data[[dataFilter(scenario, "sideath_heat")]],1), round(data[[dataFilter(scenario, "sivalue_heat")]],1), #heat, heat values
round(data[[dataFilter(scenario, "sico2")]],1), round(data$dist_fast, 1), round(100*data$av_incline, 1) )
}
paste(tableStart,tableInterm,tableEnd)
}
# Network Route popup function
networkRoutePopup <- function(data, scenario){
############ % increase in distance vs. fastest route ONLY FOR QUIETEST ??
if(scenario == 'olc') {
tableInterm <- sprintf (paste0 (knitr::kable(data.frame(
Attribute = c("Between-zone cyclists (baseline):\t", "Within-zone cyclists (baseline):\t"),
Value = c("%s" , "up to %s" )), format="html", col.names=NULL)),
data$Bicycle, 'NA')
}
else {
tableInterm <- sprintf (paste0(knitr::kable(data.frame(
Attribute = c("Between-zone cyclists (baseline):\t", "Within-zone cyclists (baseline):\t",
"Between-zone cyclists (scenario):\t", "Within-zone cyclists (scenario):\t"),
Value = c("%s", "up to %s", "%s", "up to %s" )), format="html", col.names=NULL)),
data$Bicycle,'NA', round(data[[dataFilter(scenario, "slc")]]), 'NA' )
}
paste(
tableStart,
tableInterm,
tableEnd
)
}
####ZONE = ANYWHERE INSIDE THE AREA but the centroid
zonePopup <- function(data, scenario, zone){
zone_filter_name <- scenariosNames[zone]
if(scenario == 'olc') {
t1 <- knitr::kable(data.frame(
Attribute = c("Zone:\t ", "Total commuters:\t ","Cyclists (baseline):\t","Drivers (baseline):\t" ),
Value = c("%s", "%s" , "%s (%s%%)" , "%s (%s%%)")), format="html", col.names=NULL)
tableInterm <-sprintf(t1,
data$geo_label, data$All,round(data[[dataFilter('olc', zone)]] ),round(100*data[[dataFilter('olc', zone)]] /data$All),
data$Car, " -- %" )
}
else {
t1 <- knitr::kable(data.frame(
Attribute = c("Zone:\t",
"Total commuters:\t",
"Cyclists (baseline):\t",
"Cyclists (scenario):\t",
"Change in no. cyclists:\t",
"Change in no. drivers:\t" ),
Value = c("%s", " %s " , "%s (%s%%)" , "%s (%s%%)", "%s", "%s")), format="html", col.names=NULL)
tableInterm <-sprintf(t1,
data$geo_label,
data$All,
round(data[[dataFilter('olc', zone)]] ),
round(100*data[[dataFilter('olc', zone)]] /data$All),
round(data[[dataFilter(scenario, 'slc')]]),
round(100*data[[dataFilter(scenario, 'slc')]]/data$All),
round(data[[dataFilter(scenario, "sic")]]),
round(data[[dataFilter(scenario, "sid")]]) )
}
paste(tableStart,
tableInterm,
tableEnd)
}