-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp1.R
421 lines (322 loc) · 15.2 KB
/
app1.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
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
library(shiny)
library(shinythemes)
library(rgdal)
library(plyr)
library(leaflet)
library(dplyr)
library(data.table)
library(DT)
library(car)
library(tidyverse)
library(MASS)
require(ggplot2)
library(plotly)
library(shinycustomloader)
library(shinydashboard)
library(leaflet.extras)
airport<-read.csv("airports.csv")
airline<-read.csv("airlines.csv")
net_flights<-read.csv("net_flights.csv")
taxiInA<-subset(net_flights,!is.na(net_flights$TAXI_IN),
select=c(TAXI_IN, AIRLINE_NAME)) %>%
group_by(AIRLINE_NAME) %>%
dplyr::summarise(mean_taxiIn=mean(TAXI_IN), count_of_fly=n()) %>%
arrange(AIRLINE_NAME)
server <- function(input, output) {
#-------------------
# DIfferent delays
#-------------------
map<-leaflet() %>%
addProviderTiles("CartoDB.Positron") %>%
addCircleMarkers(~LONGITUDE, ~LATITUDE,
data = airport,
color = "purple",
radius = 2,
weight = 1,
popup = paste("Airport:",
airport$AIRPORT,
"<br>",
"IATA_CODE:",
airport$IATA_CODE,
"<br>",
"CITY:",
airport$CITY))
output$map <- renderLeaflet(map)
#-------------------
# DIfferent delays
#-------------------
airport_src<-reactive({
df_use_2 <- subset(net_flights, DESTINATION_AIRPORT == input$airport_sel2)
df_use_2$status<-'ON TIME'
df_use_2$status[df_use_2$DEPARTURE_DELAY>0] <-'DEPARTURE DELAY'
df_use_2$status[df_use_2$CANCELLED==1] <-'CANCELLED FLIGHTS'
df_use_2$status[df_use_2$ARRIVAL_DELAY>0 & (df_use_2$ARRIVAL_DELAY-df_use_2$DEPARTURE_DELAY)>0 & df_use_2$DEPARTURE_DELAY>0] <-'DEPR & ARVL_DELAY'
df_use_2$status[df_use_2$ARRIVAL_DELAY>0 & df_use_2$DEPARTURE_DELAY<=0] <-'ARRIVAL_DELAY'
df_use_2$status<-factor(df_use_2$status)
return(df_use_2)
})
output$plot_delay_airlines <- renderPlotly({
ggplotly(ggplot(airport_src(),aes(x=AIRLINE,fill=status),binwidth=12) +
geom_bar(position = "fill") +
ggtitle("Flights by Airline According to Flights Status") +
ylab('Percentage Mix'))
})
#-------------------
# GGPlot
#-------------------
pre_sub_flight<-reactive({
df_pre <- net_flights[which(net_flights$DAY_OF_WEEK_F %in% input$day_week), ]
return(df_pre)
})
sub_flight<-reactive({
new_net <- pre_sub_flight()
if (input$fixed == 1) {
df_use <- subset(new_net, DESTINATION_AIRPORT_NAME == input$dest_airport_sel,
select=c(AIRLINE, ARRIVAL_DELAY, DEPARTURE_TIME))
} else {
df_use1 <- subset(new_net, DESTINATION_AIRPORT_NAME == input$dest_airport_sel)
df_use <- subset(df_use1, ORIGIN_AIRPORT_NAME == input$origin_airport_sel,
select=c(AIRLINE, ARRIVAL_DELAY, DEPARTURE_TIME))
}
df_use <- df_use[which(df_use$AIRLINE %in% input$airline_sel), ]
df_use <- df_use %>%
filter(DEPARTURE_TIME >= input$timeofday[1], DEPARTURE_TIME <= input$timeofday[2])
new_col_df <- as.data.frame(table(df_use$AIRLINE))
airline.avg.delay <- 0
keeps <- c("AIRLINE", "ARRIVAL_DELAY")
df_use <- df_use[keeps]
validate(
need(nrow(df_use) > 0, "Please select other options, no data availabe for give arguments! Sorry")
)
airline.avg.delay <- aggregate(df_use$ARRIVAL_DELAY, by=list(df_use$AIRLINE), mean, na.rm=T)
names(airline.avg.delay) <- c("AirlineCode", "Mean.Arrival.Delay")
airline.avg.delay <- merge(airline.avg.delay, airline, by.x="AirlineCode", by.y="IATA_CODE", all.x=TRUE)
airline.avg.delay <- airline.avg.delay[order(airline.avg.delay$Mean.Arrival.Delay), ]
airline.avg.delay <- merge(airline.avg.delay, new_col_df, by.x="AirlineCode", by.y="Var1", all.x=TRUE,
all.y = TRUE)
return(airline.avg.delay)
})
output$plot <- renderPlotly({
ggplotly(ggplot(data=sub_flight()) +
geom_vline(xintercept=0, linetype="dashed", color = "red", size=0.3)+
geom_vline(xintercept = 15, linetype="dashed", color = "red", size=0.3)+
geom_point(mapping = aes(x = Mean.Arrival.Delay, y = Freq, color=AIRLINE,
size = 1.5))+
ylab('Number of Flights')+
xlab('Average Arrival Delay')
)
})
#-------------------
# Taxi In @ Destination
#-------------------
taxinAirport<-reactive({
df_use_2 <- subset(net_flights, DESTINATION_AIRPORT == input$airport_sel1,
select=c(TAXI_IN, AIRLINE_NAME))
taxiIn<-subset(df_use_2,!is.na(df_use_2$TAXI_IN)) %>%
group_by(AIRLINE_NAME) %>%
dplyr::summarise(mean_taxiIn=mean(TAXI_IN), count_of_fly=n()) %>%
arrange(AIRLINE_NAME)
return(taxiIn)
})
output$plotTaxiIn <- renderPlotly({
ggplotly(
ggplot(taxinAirport(),aes(name = AIRLINE_NAME,x=mean_taxiIn,y=count_of_fly/100)) +
geom_point(color='#FA6C00',alpha=0.4) +
coord_cartesian(xlim=c(5,25)) +
ylab('Count of flight(in 100s)') +
xlab('Average Taxi In time') +
geom_smooth(linetype=2, color="#AF0F00", aes(group=1), se=FALSE))
})
output$plotTaxiInAll <- renderPlotly({
ggplotly(
ggplot(taxiInA,aes(name = AIRLINE_NAME, x=mean_taxiIn,y=count_of_fly/100)) +
geom_point(color='#FA6C00',alpha=0.4) +
coord_cartesian(xlim=c(5,25)) +
ylab('Count of flight(in 100s)') +
xlab('Average Taxi In time') +
geom_smooth(linetype=2, color="#AF0F00", aes(group=1), se=FALSE))
})
}
######################################################################################################
ui <- shinyUI(fluidPage(
theme = shinytheme("united"),
# tags$head(
# tags$link(rel = "stylesheet", type = "text/css", href = 'bootstrap.min.css')
# ),
navbarPage(
"US flights delay analysis",
tabPanel(
"Landing Page",
headerPanel(""),
br(),
h1(""),
h1("US flights delay analysis", align = "center"),
h2(""),
h4("The air traffic system is bursting at the seams, with record numbers of passengers and flights clogging U.S. airports, creating bottlenecks on runways and in the air."),
h2(''),
h4('Flight delays cause lot of problems for airline customers in myriad ways and also result in financial loses for airline companies.'),
h2(""),
h4("The dataset provides information about the on-time performance (cancellations and delays) of domestic flights operated by large air carriers in the US. Aim is to analyse various types of delays affecting daily flight operation at airports in the US.", align = 'justify'),
h2(""),
h4("This analysis can help us narrow down to the places in urgent need of decongestion of traffic and people. It gives crucial insight about the places which need infrastructure development , additional runway construction, etc. "),
h2(""),
h5("Here's a fun GIF!"),
withLoader(plotOutput("NOPLOTH"), type="image", loader="buffer.gif"),
# the Image of report front page goes here!
#### # img(src = "report.png", height = 1200, width = 1300, align = 'center'),
# the absolute panel is the widget that allows you to select by party
h2(""),
h2("")
),
tabPanel(
"Analysis",
mainPanel(
tabsetPanel(id = 'tabs1',
tabPanel("Map",
sidebarLayout(
sidebarPanel(
width = 2,
h4('Map'),
helpText("Airports in US")
),
mainPanel(
width = 10,
leafletOutput("map",
width = "100%",
height = "600px"),
h1(''),
h4("United States has arround 5000 public airports and 14000 private airports"),
h4(''),
h4("They handle about 44000 flight operation daily."),
h4(''),
h3("1 out of 3 flights are delayed at departure."),
h4(''),
h3("1 out of 4 flights depart 5 or more minutes earlier than scheduled.")
)),
),
tabPanel("Taxi In",
h1(""),
sidebarLayout(
sidebarPanel(
width = 2,
h4('Select relevent Indicators'),
selectInput(
"airport_sel1",
"Port of Destinantion of Interest: ",
choices = unique(sort(net_flights$DESTINATION_AIRPORT)),
selected = 'PHL'
),
hr(),
helpText("A greater Taxi In time suggests, longer waiting queues, arrival gate being far from runway or
fewer Gates at airport")
),
mainPanel(
width = 10,
h3(
"Shows average Taxi In time for an Airport for different airlines"
),
withLoader(plotlyOutput("plotTaxiIn"), type="html", loader="pacman"),
h2(''),
h3(
"Shows average Taxi In time for all Airport for different airlines"
),
withLoader(plotlyOutput("plotTaxiInAll"), type="html", loader="pacman")
)),
),
tabPanel("Different delays",
sidebarLayout(
sidebarPanel(
width = 2,
selectInput(
"airport_sel2",
"Port of Destinantion of Interest: ",
choices = unique(sort(net_flights$DESTINATION_AIRPORT)),
selected = 'PHL'
),
hr() ),
mainPanel(
width = 10,
withLoader(plotlyOutput("plot_delay_airlines"), type="html", loader="pacman"),
))
)
))
),
tabPanel(
"Main Plot",
headerPanel("Delay Plot"),
h1(""),
# the map is called here
sidebarLayout(
sidebarPanel(
width = 3,
selectInput(
"dest_airport_sel",
"Port of Destination of Interest: ",
choices = unique(sort(net_flights$DESTINATION_AIRPORT_NAME)),
selected = 'Miami International Airport'
),
checkboxInput("fixed", "Remove port of Origin?"),
conditionalPanel(condition = "input.fixed == 0",
selectInput(
"origin_airport_sel",
"Port of Origin of Interest: ",
choices = unique(sort(net_flights$ORIGIN_AIRPORT_NAME)),
selected = 'Seattle-Tacoma International Airport'
)),
sliderInput("timeofday", "Time of Day",
min = 0000, max = 2359, value = c(1200, 1300)),
checkboxGroupInput("day_week", "Select day/s :",
choices = c("Monday" = 1,
"Tuesday" = 2,
"Wednesday" = 3,
"Thursday" = 4,
"Friday" = 5,
"Saturday" = 6,
"Sunday" = 7),
selected = c(1,2,3,4,5,6,7)),
checkboxGroupInput("airline_sel", "Airlines to display in graph:",
choices = c("United Air Lines Inc." = "UA",
"American Airlines Inc." = "AA",
"US Airways Inc." = "US",
"Frontier Airlines Inc." = "F9",
"JetBlue Airways" = "B6",
"Skywest Airlines Inc." = "OO",
"Alaska Airlines Inc." = "AS",
"Spirit Air Lines" = "NK",
"Southwest Airlines Co." = "WN",
"Delta Air Lines Inc." = "DL",
"Atlantic Southeast Airlines" = "EV",
"Hawaiian Airlines Inc." = "HA",
"American Eagle Airlines Inc." = "MQ",
"Virgin America" = "VX"),
selected = c("UA","AA", "US", "F9", "B6",
"OO", "AS", "NK", "WN", "DL", "EV", "HA",
"MQ", "VX")),
hr(),
h4('Select relevent Indicators')
),
mainPanel(
width = 9,
withLoader(plotlyOutput('plot'), type="html", loader="pacman"),
h4(
strong("*** "),
" The Average delay of 0 to 15 minutes is not considered as a Delayed Trip, i.e shown by two dotted red lines."
),
h4(
strong("1: "),
"Choose an Airport to analyse."
),
h4(
strong("2: "),
"Choose a range for time-of-day (24 hour scale)."
),
h4(
strong("3: "),
"Select an Airline/ Airlines to Compare"
)
)
)
)
)
))
shinyApp(ui = ui, server = server)