-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.R
290 lines (262 loc) · 9.61 KB
/
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
library(shiny)
library(dplyr)
library(ggplot2)
library(DT)
library(plotly)
library(bslib)
library(leaflet)
library(thematic)
library(shinycssloaders)
library(shinytest2)
thematic::thematic_shiny()
custom_theme <- bs_theme(
version = 5,
bg = "#FFFFFF",
fg = "#FF1D8E",
primary = "#FF1D8E",
secondary = "#FF374B",
heading_font = font_google("Lobster"),
base_font = font_google("Montserrat")
)
rupaulIcon <- makeIcon(
iconUrl = "rupaul.png",
iconWidth = 60, iconHeight = 70,
iconAnchorX = 22, iconAnchorY = 70
)
# read in data
drag_df <- read.csv("data/drag.csv")
ui <- fluidPage(
theme = custom_theme,
titlePanel(title = div(img(src ="logo.png", height = 100), 'Drag Race Visualizer', style = "font-size:55px;")),
sidebarLayout(
# sidebar (filters)
sidebarPanel(
width = 2,
h3('Filters'),
selectizeInput(inputId = "season", label = "Season",
choices = unique(sort(drag_df$season)),
multiple = TRUE,
options = list(plugins = list('remove_button'))),
selectizeInput(
inputId = 'queens',
label = "Queens",
choices = sort(unique(drag_df$contestant)),
selected = c('Jinkx Monsoon'),
multiple = TRUE,
options = list(plugins = list('remove_button'))
),
# Other Categories filter
checkboxGroupInput(inputId = "other_categories", label = "Other Categories",
choices = c(
Finalist = 'finalist',
`Miss Congeniality` = 'missc',
`Winner` = 'winner',
`First Eliminated` = 'first_eliminated'
),
selected = NULL),
# Age slider filter
sliderInput(inputId = "age", label = "Age",
min = min(drag_df$age, na.rm = TRUE),
max = max(drag_df$age, na.rm = TRUE),
value = c(min(drag_df$age, na.rm = TRUE), max(drag_df$age, na.rm = TRUE)),
step = 1),
# Reset button
actionButton(inputId = "reset", label = "Reset Filters")
),
# main body (graphs)
mainPanel(
width = 10,
fluidRow(
column(6,
h3("Hometown Map"),
'Click on markers for more information on the queens',
withSpinner(leafletOutput("hometown"),
color = "#FF1D8E")
),
column(6,
h3("Queen Performance"),
'Queen performance over the episodes (limit selection to 10)',
withSpinner(plotlyOutput("queen_challenge"),
color = "#FF1D8E")
),
fluidRow(
column(6,
h3('Relative Rankings'),
'Ranking of the queen and how many challenges they participated in.',
withSpinner(dataTableOutput('ranking'),
color = "#FF1D8E")
),
# Outcome tally table
column(width=6,
h3('Outcome Tallies'),
'Total counts of each outcome over the season.',
withSpinner(DT::DTOutput(outputId = 'outcome_table'),
color = "#FF1D8E")
)
)
)
)
))
server <- function(input, output, session) {
# reactively changes selectable queens based on chosen season
observe({
# filter data based on chosen season and get unique names
if (!is.null(input$season)) {
seasoned <- drag_df |>
dplyr::filter(season %in% input$season)
filtered_names <- seasoned |>
dplyr::select(contestant) |>
unique() |>
dplyr::arrange(contestant)
selected_names <- seasoned |>
dplyr::select(contestant, rank) |>
unique() |>
dplyr::arrange(rank) |>
dplyr::select(contestant) |>
dplyr::slice(1:2) |>
dplyr::pull()
} else {
filtered_names <- sort(unique(drag_df$contestant))
selected_names <- 'Jinkx Monsoon'
}
updateSelectizeInput(
inputId = 'queens',
choices = filtered_names,
selected = selected_names
)
}) |> bindEvent(input$season, ignoreNULL = FALSE)
# reactive expression to filter data based on user selections
filtered_data <- reactive({
drag_filtered <- drag_df
# optional season filter
if (!is.null(input$season)) {
drag_filtered <- drag_filtered |>
dplyr::filter(season %in% input$season)
}
# optional categories (union of groups instead of intersection)
if (!is.null(input$other_categories)) {
drag_filtered <- drag_filtered |>
filter(if_any(input$other_categories, function(x) x == 1))
}
# age filter always applies
drag_filtered <- drag_filtered |>
dplyr::filter(age >= input$age[1] & age <= input$age[2])
# update the age slider with the min and max values of the filtered data
updateSliderInput(session = session,
inputId = "age",
min = min(drag_df$age, na.rm = TRUE),
max = max(drag_df$age, na.rm = TRUE),
value = c(min(drag_filtered$age, na.rm = TRUE), max(drag_filtered$age, na.rm = TRUE)))
# name filter separate
if (!is.null(input$queens)) {
drag_filtered <- drag_df |>
dplyr::filter(contestant %in% input$queens)
}
drag_filtered
})
# return filters to default with reset button
observeEvent(input$reset, {
updateSelectizeInput(session, "queens", selected = 'Jinkx Monsoon')
updateSelectInput(session, "season", selected = NA)
updateSliderInput(session, "age", min=21, max=52, value = c(21, 52))
updateCheckboxGroupInput(session, "other_categories", selected = NA)
})
# ranking table
output$ranking <- renderDT({
if (nrow(filtered_data()) != 0) {
clean_data <- filtered_data() |>
dplyr::filter(participant == 1)
} else {
clean_data <- filtered_data()
}
clean_data |>
dplyr::group_by(season, rank, contestant) |>
dplyr::summarise(Challenges = n(), .groups = 'drop') |>
dplyr::arrange(rank) |>
dplyr::rename(Queen = contestant,
Season = season,
Rank = rank) |>
datatable(extensions = 'Scroller',
#caption = 'Ranking of the queen on their season and how many challenges they participated in on their season.',
options = list(deferRender = TRUE,
scrollX = 350,
scrollY = 350,
scroller = TRUE,
searching = FALSE
))
})
output$hometown <- renderLeaflet({
if (nrow(filtered_data()) > 0){
map_blank <- leaflet(data = filtered_data() |>
distinct(lng, lat, contestant, .keep_all = TRUE)) |>
leaflet::setView(lng = -95.7129, lat = 37.0902, zoom = 3) |>
addTiles() |>
addMarkers(
~lng,
~lat,
icon = rupaulIcon,
popup = ~paste(contestant,
"<br>Hometown:", city, ",", state,
"<br>Age on Season:", age),
label = ~as.character(contestant),
clusterOptions = markerClusterOptions())
} else {
map_blank <- leaflet() |>
addTiles()
}
map_blank
})
output$queen_challenge <- renderPlotly({
# Filter the data to only include the top performers
plot_data <- filtered_data()
plot_data %>%
dplyr::group_by(contestant, season, episode) %>%
dplyr::summarise(outcome = if_else(outcome == "ELIM", "ELIMINATED", outcome)) %>%
dplyr::arrange(season) %>%
dplyr::top_n(10, contestant) %>%
plot_ly(x = ~episode,
y = ~factor(outcome, levels= c("BTM", "LOW", "SAFE", "HIGH", "WIN")),
color = ~contestant,
type = "scatter",
mode = "lines+markers") %>%
layout(xaxis = list(title = ""),
yaxis = list(title = "",
tickfont=list(size=10,
family=font_google("Signika Negative"),
color="#FF1D8E")),
legend = list(orientation = "h",
xanchor = "center",
x = 0.5,
y = -0.1,
itemsizing = "constant",
itemwidth = 40,
itemclick = "toggle",
itemdoubleclick = "toggleothers",
font=list(size=8,
family=font_google("Signika Negative"))))
})
# Outcome tally table
output$outcome_table <- renderDataTable({
data <- filtered_data()
data |>
dplyr::group_by(contestant) |>
dplyr::summarize(WIN = sum(outcome == "WIN", na.rm = TRUE),
HIGH = sum(outcome == "HIGH", na.rm = TRUE),
SAFE = sum(outcome == "SAFE", na.rm = TRUE),
LOW = sum(outcome == "LOW", na.rm = TRUE),
BOTTOM = sum(outcome == "BTM", na.rm = TRUE),
.groups = 'drop') |>
dplyr::rename(Queen = contestant) |>
datatable(rownames = FALSE,
#caption = 'Total counts of each outcome over the season.',
extensions = 'Scroller',
options = list(deferRender = TRUE,
scrollX = 350,
scrollY = 350,
scroller = TRUE,
searching = FALSE
)
)
})
}
shinyApp(ui, server)