forked from ialok00001/Flight_Analysis_Application
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shiny.R
195 lines (161 loc) · 7.02 KB
/
Shiny.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
library(shiny)
library(shinydashboard)
library(ggplot2)
library(dplyr)
load("data/Flights_Data_Clean.RData", verbose = TRUE)
# Define UI for the app
ui <- dashboardPage(
skin = "green",
dashboardHeader(title = "Flight Delays Analysis Dashboard"),
dashboardSidebar(
sidebarMenu(
menuItem("Flight Delays", tabName = "flight_delays", icon = icon("plane")),
menuItem("Airline Frequency", tabName = "airline_frequency", icon = icon("list-ul")),
menuItem("Airline Delays", tabName = "airline_delays", icon = icon("chart-line"))
)
),
dashboardBody(
tabItems(
# Page 1 content: Flight Delays
tabItem(tabName = "flight_delays",
fluidRow(
column(12,
tags$div(
h4("Flight delays"),
p("The graph below displays the relationship between departure times and departure delays for flights based on the selected month and day."),
p("Adjust the month and day using the inputs on the left to view different data points."),
hr()
)
)
),
fluidRow(
column(12,
selectInput("month", "Select Month",
choices = month.name,
selected = month.name[1])),
column(12,
uiOutput("day_selector")),
column(12,
align = "center",
plotOutput("flight_plot", width = "80%", height = "400px"))
)
),
# Page 2 content: Airline Frequency
tabItem(tabName = "airline_frequency",
fluidRow(
column(12,
tags$div(
h4("Famous Flights"),
p("The graph below displays the frequencies of flights for different selected airlines."),
p("Check the box for the airlines for which you want to have the comparison."),
hr()
)
)
),
fluidRow(
column(12,
align = "center",
plotOutput("airline_plot", width = "80%", height = "400px"))
),
fluidRow(
column(12,
checkboxGroupInput("airlines", "Select Airlines",
choices = unique(data$name),
selected = unique(data$name)[1:4]),
checkboxInput("select_all", "Select All", FALSE) )
)
),
tabItem(tabName = "airline_delays",
fluidRow(
column(12,
tags$div(
h4("Airline mean delays"),
p("The graph below displays the mean of delays of flights over the months for different selected airlines."),
p("Check the box for the airlines for which you want to have the comparison."),
hr()
)
)
),
fluidRow(
column(12,
align = "center",
plotOutput("airline_mean_delay_plot", width = "80%", height = "400px"))
),
fluidRow(
column(12,
checkboxGroupInput("airlines_2", "Select Airlines",
choices = unique(data$name),
selected = unique(data$name)[1]))
)
)
)
)
)
# Define server logic
server <- function(input, output, session) {
# Render day selection based on selected month
output$day_selector <- renderUI({
max_days <- ifelse(input$month %in% c("January", "March", "May", "July", "August", "October", "December"), 31,
ifelse(input$month %in% c("April", "June", "September", "November"), 30, 28)) # Account for February in a non-leap year
sliderInput("day", "Select Day", min = 1, max = max_days, value = 1)
})
output$flight_plot <- renderPlot({
# Map selected month name to its numeric representation
month_num <- match(input$month, month.name)
# Filter data based on selected month (using numeric representation) and day
filtered_data <- subset(data, month == month_num & day == input$day)
# Extract month name and day number
month_name <- input$month
day_number <- input$day
# Create ggplot for departure times vs departure delays
ggplot(filtered_data, aes(x = dep_time, y = dep_delay)) +
geom_point(color = "#CC79A7", alpha = 0.7, size = 2) +
labs(x = "Departure Time",
y = "Departure Delay (minutes)",
title = paste(month_name, day_number)) +
theme_minimal() +
theme(plot.title = element_text(hjust = 0.5))
})
output$airline_plot <- renderPlot({
# Filter data based on selected airlines
selected_airlines_data <- subset(data, name %in% input$airlines)
# Create frequency table for selected airlines
frequency_table <- table(selected_airlines_data$name)
frequency_df <- as.data.frame(frequency_table)
frequency_df <- frequency_df[order(frequency_df$Freq, decreasing = TRUE), ]
custom_colors <- rainbow(length(input$airlines))
# Create ggplot for frequency of flights by airline
ggplot(frequency_df, aes(x = reorder(Var1, -Freq), y = Freq, fill = Var1)) +
geom_bar(stat = "identity") +
labs(title = "Frequency of Flights by Airline",
x = "Airline",
y = "Frequency") +
theme_minimal() +
coord_flip() +
scale_fill_manual(values = custom_colors)
})
observeEvent(input$select_all, {
if(input$select_all) {
updateCheckboxGroupInput(session, "airlines", selected = unique(data$name))
} else {
updateCheckboxGroupInput(session, "airlines", selected = NULL)
}
})
output$airline_mean_delay_plot <- renderPlot({
# Filter data based on selected airlines
selected_airlines_data <- subset(data, name %in% input$airlines_2)
summary_data <- selected_airlines_data %>%
group_by(month, name) %>%
summarise(mean_delay = mean(dep_delay), .groups = 'drop')
month_names <- month.abb
ggplot(summary_data, aes(x = factor(month, levels = 1:12, labels = month_names), y = mean_delay, color = name)) +
geom_line(aes(group = name), linewidth = 1, alpha = 0.9) +
labs(title = "Flight Delay Analysis by Month and Airline",
x = "Month",
y = "Mean Delay",
color = "Airline") +
theme_minimal()
})
}
# Run the application
shinyApp(ui = ui, server = server)