-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCosting by zone - Shiny App.R
391 lines (330 loc) · 11.8 KB
/
Costing by zone - Shiny App.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
library(shiny)
library(tidyverse)
library(pryr)
library(readxl)
library(readr)
library(stringr)
library(dplyr)
library(DT)
library(tools)
# Define UI for data upload app ----
ui <- shinyUI(fluidPage(
# App title ----
titlePanel("Movia - Costing by Zone"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(width = 3,
# Input: Upload GTFS ----
fileInput("zip", "Upload GTFS",
multiple = FALSE,
accept = c(".zip")),
# Input: Upload Excel with regions ----
fileInput("regions", "Excel with Regions",
multiple = FALSE,
accept = c(".xlsx")),
# Horizontal line ----
tags$hr(),
#Hourly cost
numericInput(inputId = "cost", label = "Hourly cost:", value = 100),
tags$hr(),
#Group by Zone, Line or Day type
selectInput(inputId = "selected_group",
label = "Group by:",
choices = c("Zone" = 'zone',
"Route" = 'route',
"Day type" = 'day_type'
),
selected = "zone",
multiple = TRUE,
selectize = TRUE),
# Line filter
uiOutput("routes_selected_dt"),
tags$hr(),
# Select which types of day types to see
checkboxGroupInput(inputId = "selected_service",
label = "Select day type(s):",
choices = c("Weekday" = 1,
"Saturday" = 2,
"Sunday" = 3),
selected = 1),
#Link to Remix
tags$a("Go to Remix", href = "https://www.remix.com/")
),
# Main panel for displaying outputs ----
mainPanel(
#Create different tabs
tabsetPanel(type = "tabs",
id = "tabspanel",
tabPanel(title = "Yearly values",
br(),
fluidRow(
column(2,
numericInput(inputId = "weekdays", label = "Weekdays", value = 255)
),
column(2,
numericInput(inputId = "saturdays", label = "Saturdays", value = 55)
),
column(2,
numericInput(inputId = "sundays", label = "Sundays", value = 55)
),
column(1, offset = 4,
# Download bUtton
downloadButton("downloadYearlyData", "Download")
)
),
fluidRow(
tags$hr(),
DT:: dataTableOutput(outputId = "group_selected_yearly_dt")
)
),
tabPanel(title = "Daily values",
br(),
fluidRow(
column(1, offset = 10,
downloadButton("downloadData", "Download")
)
),
tags$hr(),
DT:: dataTableOutput(outputId = "group_selected_dt")
)
)
)
)
))
# Define server logic to read selected file ----
options(shiny.maxRequestSize=30*1024^2)
server <- function(input, output, session) {
#get the GTFS path to use it later
gtfs <- reactive({
req(input$zip)
input$zip$datapath
})
#Create a temp directory
exdir <- reactive({
req(gtfs())
substring(gtfs(), 1, nchar(gtfs())-4)
})
#Get the list of files from the GTFS
files <- reactive({
req(exdir())
unzip(gtfs(), list = FALSE, exdir = exdir())
})
#Define all the data frames I need
#Create the path to find the files
routes <- reactive({
req(files())
routes_path <- paste(exdir(), 'routes.txt', sep = '/')
read_csv(routes_path)
})
all_routes <- reactive({
req(routes())
sort(unique(routes()$route_long_name))
})
trips <- reactive({
req(files())
trips_path <- paste(exdir(), 'trips.txt', sep = '/')
read_csv(trips_path)
})
stops <- reactive({
req(files())
stops_path <- paste(exdir(), 'stops.txt', sep = '/')
read_csv(stops_path)
})
#Get the Excel file
regions <- reactive({
req(input$regions)
read_excel(input$regions$datapath) %>%
select(#TariffZoneKey,
TariffZoneDisplayName,
TariffZoneNumber,
#MunicipalityDisplayName,
#MunicipalityCode,
#StopPointId,
#StopPointDisplayName,
StopPointNumber,
#StopPointName,
StopPointTypeCode,
IsCurrent) %>%
filter(IsCurrent == 1) %>%
filter(StopPointTypeCode == 'BUSSTOP') %>%
select(-StopPointTypeCode, - IsCurrent)
})
#Now that I have all the files I read and built stop_times
stop_times <- reactive({
req(files())
stop_times_path <- paste(exdir(), 'stop_times.txt', sep = '/')
read_csv(stop_times_path, col_types= cols(arrival_time = col_character(), departure_time = col_character())) %>%
left_join(trips()) %>%
left_join(stops()) %>%
mutate(StopPointNumber = as.numeric(stop_id)) %>%
left_join(regions()) %>%
select(-block_id)
})
#Start building the data frama to find the breaks
#See when we change trip and tarif zone
filtered <- reactive({
req(regions(), stop_times())
stop_times() %>%
select(trip_id, TariffZoneNumber)
})
trips_ids <- reactive({
req(regions(), stop_times())
filtered() %>%
#trip_id from the previous stop
mutate(prev_trip = ifelse(is.na(lag(trip_id)),
filtered()[nrow(filtered()), 1],
lag(trip_id))) %>%
#trip_id from the next stop
mutate(next_trip = ifelse(is.na(lead(trip_id)),
filtered()[1, 1],
lead(trip_id))) %>%
#TariffZoneNumber of the previous stop
mutate(prev_zone = ifelse(is.na(lag(TariffZoneNumber)),
TariffZoneNumber,
lag(TariffZoneNumber)) ) %>%
#TariffZoneNumber of the next stop
mutate(next_zone = ifelse(is.na(lead(TariffZoneNumber)),
TariffZoneNumber,
lead(TariffZoneNumber)) )
})
#Find the breaks
breaks_1 <- reactive({
req(trips_ids())
stop_times()[(
trips_ids()$trip_id != trips_ids()$prev_trip |
trips_ids()$trip_id != trips_ids()$next_trip |
trips_ids()$TariffZoneNumber != trips_ids()$prev_zone |
trips_ids()$TariffZoneNumber != trips_ids()$next_zone
), ]
})
#We'll only keep the ones that are not NA.
breaks_2 <- reactive({
req(breaks_1())
breaks_1() %>%
filter(is.na(TariffZoneNumber) != 'TRUE')
})
breaks <- reactive({
req(breaks_2())
#calculate running time for each
hours <- as.integer(substr(breaks_2()$arrival_time, 1, 2))
minutes <- as.integer(substr(breaks_2()$arrival_time, 4, 5))/60
breaks_2() %>%
mutate(hours_minutes = hours + minutes) %>%
mutate(travel_hours = ifelse(trip_id == lead(trip_id),round((lead(hours_minutes) - hours_minutes),digits = 2), 0)) %>%
select(trip_id, route_id, TariffZoneDisplayName, travel_hours,-timepoint, - direction_id, -hours_minutes) %>%
left_join(trips()) %>%
select(trip_id, route_id, service_id, TariffZoneDisplayName, travel_hours) %>%
left_join(routes()) %>%
select('route' = route_long_name,
'trip' = trip_id,
'zone' = TariffZoneDisplayName,
'hours' = travel_hours,
'day_type' = service_id)
})
hourly_cost <- reactive({
input$cost
})
#Daily table - Group by selectd columns
group_selected <- reactive({
req(input$selected_group, breaks()) # ensure availablity of value before proceeding
if(!is.null(input$selected_route)){
breaks() %>%
filter(day_type %in% input$selected_service) %>%
filter(route %in% input$selected_route) %>%
mutate(cost = hours * hourly_cost()) %>%
group_by_at(vars(input$selected_group)) %>%
summarise(hours = sum(hours), cost = sum(cost))
} else {
breaks() %>%
filter(day_type %in% input$selected_service) %>%
mutate(cost = hours * hourly_cost()) %>%
group_by_at(vars(input$selected_group)) %>%
summarise(hours = sum(hours), cost = sum(cost))
}
})
weekday <- reactive({
input$weekdays
})
saturdays <- reactive({
input$saturdays
})
sundays <- reactive({
input$sundays
})
#Yearly reference table
yearly_table <- reactive({
req(breaks())
breaks() %>%
mutate(hours = case_when(
day_type == 1 ~ hours * weekday(),
day_type == 2 ~ hours * saturdays(),
day_type == 3 ~ hours * sundays()
)) %>%
group_by(route, zone, day_type) %>%
summarize(hours = sum(hours))
})
#Yearly table - Group by selectd columns
group_selected_yearly <- reactive({
req(yearly_table()) # ensure availablity of value before proceeding
if(!is.null(input$selected_route)){
yearly_table() %>%
filter(day_type %in% input$selected_service) %>%
filter(route %in% input$selected_route) %>%
mutate(cost = hours * hourly_cost()) %>%
group_by_at(vars(input$selected_group)) %>%
summarise(hours = sum(hours), cost = sum(cost))
} else {
yearly_table() %>%
filter(day_type %in% input$selected_service) %>%
mutate(cost = hours * hourly_cost()) %>%
group_by_at(vars(input$selected_group)) %>%
summarise(hours = sum(hours), cost = sum(cost))
}
})
#Render list of routes
output$routes_selected_dt <- renderUI({
selectInput(inputId = "selected_route",
label = "Filter lines:",
choices = all_routes() ,
selected = '',
multiple = TRUE,
selectize = TRUE)
})
# Daily Information output
output$group_selected_dt <- renderDataTable({
if(is.null(gtfs()))
return ()
datatable(data = group_selected(),
options = list(pageLength = 10, lengthMenu = c(10, 20, 40)),
rownames = FALSE)
})
# Yearaly Information output
output$group_selected_yearly_dt <- renderDataTable({
if(is.null(gtfs()))
return ()
datatable(data = group_selected_yearly(),
options = list(pageLength = 10, lengthMenu = c(10, 20, 40)),
rownames = FALSE)
})
# Downloadable daily csv of selected dataset
output$downloadData <- downloadHandler(
filename = function() {
paste(paste("Daily_Costing", date(),sep = "-"), ".csv", sep = "")
},
content = function(file) {
write.csv(group_selected(), file, row.names = TRUE)
}
)
# Downloadable yearly csv of selected dataset
output$downloadYearlyData <- downloadHandler(
filename = function() {
paste(paste("Yearly_Costing", date(),sep = "-"), ".csv", sep = "")
},
content = function(file) {
write.csv(group_selected_yearly(), file, row.names = TRUE)
}
)
}
# Create Shiny app ----
shinyApp(ui, server)