-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.R
More file actions
448 lines (357 loc) · 15.1 KB
/
app.R
File metadata and controls
448 lines (357 loc) · 15.1 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
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
# National College of Ireland
# Data Visualization Project
# Interactive Visualization - Dashboard 1
# Title: Overview of Flights in Brazil by date
# Author: Douglas Zickuhr
# Loading libraries
library(lubridate)
library(tidyverse)
library(dplyr)
library(leaflet)
library(sp)
library(geosphere)
library(shiny)
library(leaflet.extras)
library(DT)
library(data.table)
library(plotly)
# Function to load and return the dataframe
loadTotalByDay <- function(file){
# Read the file using fread from data.table library - Much faster!
df <- fread(file = file,
colClasses = c("factor","factor","double","double","factor","factor","factor","double","double",
"factor","factor","character","character","numeric","numeric","numeric"))
# Converting the DepartureYMD variable to Date
df$DepartureYMD <- ymd(df$DepartureYMD)
# Rouding the Average Deplay - We don't want the user to see a lot of decimal places, right?
df <- df %>%
mutate(AverageDepartureDelay = round(AverageDepartureDelay,2),
AverageArrivalDelay = round(AverageArrivalDelay,2))
# Return the dataframe
return(df)
}
# Load the dataframe totalByDay - previously manipulated to be aggregated by date and route.
totalByDay <- loadTotalByDay(file = 'TotalByDay.csv')
# Shiny User interface
ui <- fluidPage(
# Title of panel
titlePanel(paste("Brazilian Flight Overview for one day"),
windowTitle = "Data Visualization Final Project - Dashboard 1"),
helpText("The purpose on this dashboard is to visualize what happen on the Brazilian Air-trafic in one day."),
sidebarLayout(
# The panel has the select input
sidebarPanel(
width = 3,
# Well panel keep things tidy - Flight selection is the first
wellPanel(h3("Flight Selection"),
# The helptext output is used to give the user hints of how to use the dashboard
helpText("Use these options to filter the records."),
# Select Flight date input
dateInput(inputId = "date",
label = "Date",
min = min(totalByDay$DepartureYMD),
max = max(totalByDay$DepartureYMD),
value = max(totalByDay$DepartureYMD)-1
),
# Select Flight Type input
checkboxGroupInput(inputId = "type",
label = "Flight Type",
choices = levels(totalByDay$Flight.Type),
selected = levels(totalByDay$Flight.Type)
)
),
wellPanel(
h5(tags$a(img(src = "https://www.ncirl.ie/Portals/0/nciLogo.png",
height = "30px"),
href = "https://www.ncirl.ie"
),
br(),
tags$a("Student: Douglas Zickuhr",
href="https://www.linkedin.com/in/douglas-zickuhr/"),
br(),
"Student Number: 17111781"),
tags$a(h5("Data extracted from Kaggle"),
href = "https://www.kaggle.com/ramirobentes/exploring-civil-aviation-in-brazil/data")
),
wellPanel(h5("Built with",
tags$a(img(src = "https://www.rstudio.com/wp-content/uploads/2014/04/shiny.png",
height = "30px"),
href="https://shiny.rstudio.com/"),
"by",
tags$a(img(src = "https://www.rstudio.com/wp-content/uploads/2014/07/RStudio-Logo-Blue-Gray.png",
height = "30px"),
href="https://www.rstudio.com"),
"."))
),
# Output panel:
mainPanel(
# Tabset to create different tabs
tabsetPanel(
#First tab for Map
tabPanel("Map",
# Well panel to organise the output
wellPanel(
# Description of the plot
div(h3(HTML(paste("Route Map From/To Brazil on",
textOutput(outputId = "selectedDate1"))))),
hr(),
# Hint about how the map works.
helpText("Click on the lines to see the route. Click on the red circle to see airport information."),
br(),
# Outputting the leaflet map.
leafletOutput(outputId = "mymap"),
# Another hint below the map
helpText("Red lines means heavy flow. Green line means light flow"),
helpText("The thickest the line is, highest is the flow on that route")
)),
# Second tab for Sankey Diagram
tabPanel("Sankey Diagram",
#Well panel to organise the output
wellPanel(
# Description of the plot
div(style="display: inline-block;vertical-align:top; width: 60%;",h3(HTML(paste("Sankey Diagram - Top busiest routes on", textOutput(outputId = "selectedDate2"))))),
# Number of top routes for Sankey
div(style="display: inline-block;horizontal-align:right;vertical-align:top; width: 300px;",sliderInput(inputId = "sankey_rows",
label = "Number of Top Routes",
min = 1,
max = 50,
step = 5,
value = 20,
width = "50%",
animate = T)),
helpText("It is possible to change the number of top routes to be plot on the slider above"),
helpText("If clicked on the play button, an animation will be started and the plot will be automatically updated"),
hr(),
# A hint about the Sankey Diagram
helpText("Click over the boxes to see flow information."),
# Outputting the sankey diagram
plotlyOutput(outputId = "sankey")
)),
# Third tab - Showing the data and allowing the user to download it
tabPanel("Data",
# Well panel for tidying the visualization
wellPanel(
#Description of the tab
h3("Detailed data"),
hr(),
# A hint about the Data Table
helpText("It's possible to change the data that is being listed using the filter options."),
# Outputting the table
dataTableOutput(outputId = "datatable"),
# A hint about the Data Table
helpText("Click on the button to download the data"),
# Option to download the correspondent data
downloadButton("downloadData", "Download")
)
)
),
# Listing the total of records found.
uiOutput(outputId = "n"),
helpText("The raw dataset contains over 2M flight observations from 31/Dec/2014 to 31/Jul/2017")
)
)
)
# Server - Shinny
server <- function(input, output, session) {
# Reactive function to filter the data based on the input
oneDay <- reactive({
# Requesting input$data and input$type
req(input$date)
req(input$type)
# Returning the data filtered
filter(totalByDay, DepartureYMD == input$date &
Flight.Type %in% input$type)
})
output$selectedDate1 <- renderText({
req(input$date)
format.Date(input$date,"%d/%b/%Y")
})
output$selectedDate2 <- renderText({
req(input$date)
format.Date(input$date,"%d/%b/%Y")
})
# Reactive function to create the airport points based on the filtered data
airportsPoints <- reactive({
# Request input$date and input$type to go ahead
req(input$date)
req(input$type)
# Create a dataframe with the filtered oneDay()
aiportsPointsDF <- oneDay()
# Aggregating the data related to the Airport.From
aiportsPointsDF1 <- aiportsPointsDF %>%
group_by(City.From,Latitude.From,Longitude.From) %>%
summarise() %>%
ungroup() %>%
mutate(City = City.From,
Lat = Latitude.From,
Long = Longitude.From) %>%
select(City,Lat,Long)
# Aggregating the data related to the Airport.To
aiportsPointsDF2 <- aiportsPointsDF %>%
group_by(City.To,Latitude.To,Longitude.To) %>%
summarise() %>%
ungroup() %>%
mutate(City = City.To,
Lat = Latitude.To,
Long = Longitude.To) %>%
select(City,Lat,Long) %>%
ungroup()
# Creating a dataframe to output
airportsOutput <- rbind(aiportsPointsDF1,aiportsPointsDF1) %>%
distinct(City,Lat,Long)
# Returning the dataframe to be plot
return(airportsOutput)
})
# Reactive function to create the points to be plot on the map
points <- reactive({
# Requesting input$date and input$type
req(input$date)
req(input$type)
# Function to generate the lines betwen the points
linesGeneration <- function(x){
# gcIntermediate function from geosphere package
return <- gcIntermediate(c(oneDay$Longitude.To[x],
oneDay$Latitude.To[x]),
c(oneDay$Longitude.From[x],
oneDay$Latitude.From[x]),
n = 50, addStartEnd = TRUE,
breakAtDateLine = TRUE)
# Create line from return
return <- Line(return)
# Create list of lines
return <- Lines(list(return), ID = x)
# Returning the generated Lines
return
}
# Creating another Dataframe with the filtered DF oneDay()
oneDay <- oneDay()
# Interating the dataframe calling the linesGeneration function
Sl <- map(1:nrow(oneDay), linesGeneration)
# SpatialLines function from geosphere package
Sl <- SpatialLines(Sl)
# Creating SpatialLinesDataFrame
spatdf <- SpatialLinesDataFrame(Sl, oneDay, match.ID = TRUE)
# Returning the spatial data frame
spatdf
})
# Reactive function to generate the Sankey Diagram
sankey <- reactive({
# Requesting input$date, input$type and input$sankey_rows
req(input$date)
req(input$type)
req(input$sankey_rows)
# Generating a dataframe to format the data
df <- oneDay() %>%
select(City.From,City.To,Total) %>%
arrange(desc(Total)) %>%
distinct() %>%
head(input$sankey_rows)
# Creating nodes for Sankey
nodes1 <- data.frame(name=c(unique(levels(factor(df[,1])))))
nodes2 <- data.frame(name=c(unique(levels(factor(df[,2])))))
# Creating names for Sankey
nam1 <- seq_along(nodes1[,1])-1
nam2 <- seq(length(nam1),length(nam1) + length(nodes2[,1])-1)
# Assigning names to nam1 and nam2
names(nam1) <- nodes1[,1]
names(nam2) <- nodes2[,1]
# Generating links for nodes
links <- data.frame(source = nam1[as.character(df[,1])],
target = nam2[as.character(df[,2])],
value = df[,3])
# Creating the plot - plotly library
p <- plot_ly(
# Fixed options
type = "sankey",
orientation = "h",
# Generating nodes
node = list(
label = c(names(nam1),names(nam2)),
pad = 15,
thickness = 20,
line = list(
color = "black",
width = 0.5
)
),
# Generating links
link = list(
source = links$source,
target = links$target,
value = links$value
)
)
# Returning the plot
p
})
# Rendering the Map
output$mymap <- renderLeaflet({
# Generating a pallete of colours based on the total of flights
factpal <- colorFactor(palette = c('green','yellow','orange','red'), points()$Total)
# Plotting the map
leaflet(data = points()) %>%
addTiles() %>%
# Adding the polylines
addPolylines(fillOpacity = 0.8,
color = ~factpal(points()$Total),
weight = oneDay()$Total,
highlight = highlightOptions(
color = "black",
fillOpacity = 1,
bringToFront = TRUE),
label = paste(oneDay()$Route," - ",oneDay()$Total, "flight")) %>%
# Adding the circles
addCircleMarkers(airportsPoints()$Long,
airportsPoints()$Lat,
radius=3,
color = "red",
fillOpacity = 1,
fill = 1,
fillColor = 1,
label = airportsPoints()$City) %>%
suspendScroll()
})
# Rendering the Sankey Diagram
output$sankey <- renderPlotly({
sankey()
})
# Rendering the Data Table
output$datatable <- renderDataTable({
table <- datatable(oneDay()[,c("Route","Total")])
return(table)
})
# Rendering the information about the filtered data
output$n <- renderUI({
# Requesting the type
req(input$type)
# Creating a new dataframe to list the data
types <- oneDay()
# Aggregating the data to generate the total by type
types <- types %>%
filter(Flight.Type %in% input$type) %>%
group_by(Flight.Type) %>%
summarise(Total = sum(Total))
# Generating a matrix with the same length of the dataframe
n <- matrix("",length(types$Flight.Type))
# Iterating the dataframe and populating the matrix to output
for (i in 1:length(types$Flight.Type)){
desc <- levels(types$Flight.Type)[as.numeric(types[i,1])]
n[i] <- paste(types[i,2],desc, "flights were found at this date.<br>")
}
# Returning the matrix as HTML
return(HTML(n))
})
# Rendering the option to download the file
output$downloadData <- downloadHandler(
# Function to return the filename
filename = function() {
paste("flights",format.Date(input$date,"%Y-%m-%d") , ".csv", sep = "")
},
# Function to return the data
content = function(file) {
write.csv(oneDay()[,c("Route","Total")], file, row.names = FALSE)
}
)
}
# Create a Shiny app object
shinyApp(ui = ui, server = server)