-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.R
268 lines (234 loc) · 5.57 KB
/
ui.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
library(tidyverse)
library(plotly)
library(lubridate)
library(bslib)
library(markdown)
library(thematic)
# Load and clean up data ----
# Load original data
collisions_raw_df <- read.csv("seattle-collisions.csv", stringsAsFactors = F)
# Remove unnecessary columns
collisions_df <- collisions_raw_df %>%
select(
-INCKEY:-INTKEY, -EXCEPTRSNCODE, -EXCEPTRSNDESC, -INATTENTIONIND,
-PEDROWNOTGRNT:-HITPARKEDCAR
)
# Add NA to blank cells
collisions_df <- collisions_df %>%
mutate_all(na_if, "")
# Save dates
dates <- as.Date(collisions_df$INCDATE)
# Remove the year 2003 and 2022
collisions_df <- collisions_df %>%
mutate(YEAR = year(INCDATE)) %>%
filter(YEAR != 2003, YEAR != 2022)
# Update dates
dates <- as.Date(collisions_df$INCDATE)
# Collision types
collision_types <- collisions_df %>%
group_by(COLLISIONTYPE) %>%
tally() %>%
na.omit()
# Introduction ----
# Introduction tab panel
intro_tab <- tabPanel(
"Home Page",
HTML(
'<center><img src = "https://media.istockphoto.com/vectors/vector-of-two-car-accident-top-view-of-vehicle-collision-on-blue-vector-id1151971103?k=20&m=1151971103&s=612x612&w=0&h=QOsIoWB-41HXm-EuR6i5C2-7cGZSJfBJ9lxAvGtQErk=" width = "600"></center>'
),
hr(),
tabsetPanel(
type = "tabs",
tabPanel(
"About the Project",
includeMarkdown("text-files/about-the-project.md")
),
tabPanel(
"Dataset",
includeMarkdown("text-files/dataset.md")
),
tabPanel(
"Limitations and Challenges",
includeMarkdown("text-files/limit-challenge.md")
),
tabPanel(
"About Us",
includeMarkdown("text-files/about-us.md")
)
)
)
# Chart 1 ----
# Widget
chart1_widget <- sidebarPanel(
radioButtons(
inputId = "trend_selection",
label = h6("Select a trend"),
choices = list(
"Total Collisions" = 1,
"People Involved" = 2,
"Vehicles Involved" = 3
),
selected = 1
),
selectizeInput(
inputId = "year1_selection",
label = h6("Select up to 4 years to compare"),
choices = sort(unique(collisions_df$YEAR)),
options = list(maxItems = 4, placeholder = "Select a year"),
selected = "2004",
multiple = T
)
)
# Plot
chart1_plot <- mainPanel(
plotlyOutput(outputId = "chart1")
)
# Chart 2 ----
# Widget
chart2_widget <- sidebarPanel(
checkboxGroupInput(
inputId = "casualty_selection",
label = h6("Select a casualty type(s)"),
choices = list(
"Injuries" = "Injuries",
"Serious Injuries" = "Serious Injuries",
"Fatalities" = "Fatalities"
),
selected = "Injuries"
)
)
# Plot
chart2_plot <- mainPanel(
plotlyOutput(outputId = "chart2")
)
# Chart 3 ----
# Widget
chart3_widget <- sidebarPanel(
radioButtons(
inputId = "condition_selection",
label = h6("Select a condition"),
choices = list(
"Weather" = 1,
"Road" = 2,
"Light" = 3
),
selected = 1
)
)
# Plot
chart3_plot <- mainPanel(
plotlyOutput(outputId = "chart3")
)
# Chart 4 ----
# Widget
chart4_widget <- sidebarPanel(
sliderInput(
inputId = "year4_selection",
label = h6("Select a year"),
min = min(collisions_df$YEAR),
max = max(collisions_df$YEAR),
value = 2010,
sep = ""
),
checkboxGroupInput(
inputId = "coltype_selection",
label = h6("Select a collision type(s)"),
choices = collision_types$COLLISIONTYPE,
selected = collision_types$COLLISIONTYPE[1:5]
)
)
# Plot
chart4_plot <- mainPanel(
plotlyOutput(outputId = "chart4")
)
# Visualization tab ----
viz_tab <- navbarMenu(
"Visualization",
tabPanel(
title = "Collision Trends Over Time",
h1(em("Collision Trends Over Time")),
hr(),
sidebarLayout(chart1_widget, chart1_plot),
hr(),
includeMarkdown("text-files/collision-trends.md")
),
tabPanel(
title = "Casualties from Collisions",
h1(em("Casualties from Collisions")),
hr(),
sidebarLayout(chart2_widget, chart2_plot),
hr(),
includeMarkdown("text-files/casualties.md")
),
tabPanel(
title = "Conditions during Accidents",
h1(em("Conditions during Accidents")),
hr(),
sidebarLayout(chart3_widget, chart3_plot),
hr(),
includeMarkdown("text-files/conditions.md")
),
tabPanel(
title = "Collision Types Comparison",
h1(em("Collision Types Comparison")),
hr(),
sidebarLayout(chart4_widget, chart4_plot),
hr(),
includeMarkdown("text-files/collision-types.md")
)
)
# Conclusion ----
# Table panel
table_panel <- sidebarPanel(
tableOutput(outputId = "table"),
width = 5
)
# Table summary
summary_panel <- mainPanel(
includeMarkdown("text-files/table-summary.md"),
width = 7
)
# Conclusion tab panel
conclusion_tab <- tabPanel(
"Insights",
HTML(
'<center><img src = "https://kennyperezlaw.com/wp-content/uploads/2021/09/Head-on-Car-Collisions%E2%80%94The-Most-Dangerous-Crashes.jpg" width = "600"></center>'
),
hr(),
tabsetPanel(
type = "tabs",
tabPanel(
"Key Takeaways",
includeMarkdown("text-files/insights.md")
),
tabPanel(
"Table Summary",
sidebarLayout(
summary_panel,
table_panel,
position = c("left", "right"),
fluid = T
)
)
)
)
# 6. UI ----
# Theme
my_theme <- bs_theme(
bootswatch = "cerulean",
base_font = font_google("Tiro Gurmukhi"),
heading_font = font_google("EB Garamond"),
font_scale = 1.1
)
# Enable thematic for every plot inside the app
thematic_shiny(font = "auto")
# Change all the plots to reflect RStudio theme
thematic::thematic_on()
# User interface
ui <- navbarPage(
theme = my_theme,
"Seattle Collisions",
intro_tab,
viz_tab,
conclusion_tab
)