-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.R
462 lines (423 loc) · 15.7 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
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
library(shiny)
library(bslib)
library(shinyWidgets)
library(shinydashboard)
library(shinycssloaders)
library(ggplot2)
library(forcats)
library(plotly)
library(thematic)
library(scales)
library(rlang)
library(stringr)
library(showtext)
library(sysfonts)
library(packcircles)
library(lubridate)
library(rsconnect)
library(shinytest2)
library(rmarkdown)
options(shiny.autoreload = TRUE)
options(max.print = 25)
data <- read.csv("data/processed/CA_youtube_trending_data_processed.csv")
boxplot_options <- c(
"Comments" = "comment_count",
"Dislikes" = "dislikes",
"Likes" = "likes",
"Views" = "view_count"
)
interval_choices <- c("Day of Week" = "publish_wday",
"Month of Year" = "publish_month",
"Time of Day" = "publish_hour")
boxplot_colours <- setNames(
c("#35618f", "#2dadb8", "#2a6a45", "#0df38f", "#93c680",
"#21a708", "#bce333", "#7e2b19", "#de592e", "#fcd107",
"#b08965", "#d4d4d4", "#5c51b1", "#cc99d9", "#a53bb7"),
unique(data$categoryId)
)
barplot_colours <- setNames(
unique(data$categoryId),
c("#35618f", "#2dadb8", "#2a6a45", "#0df38f", "#93c680",
"#21a708", "#bce333", "#7e2b19", "#de592e", "#fcd107",
"#b08965", "#d4d4d4", "#5c51b1", "#cc99d9", "#a53bb7")
)
light_theme <- bslib::bs_theme(
bootswatch = "journal",
base_font = bslib::font_google("Assistant"))
dark_theme <- bslib::bs_theme(
bootswatch = "journal",
bg = "#232323",
fg = "white",
base_font = bslib::font_google("Assistant"))
# Add fonts from Google
sysfonts::font_add_google("Assistant")
# Automatically use showtext to render text
showtext::showtext_auto()
# Let thematic know to update the plot fonts too
thematic::thematic_shiny(font = "auto")
ui <- fluidPage(theme = light_theme,
navbarPage(
# Import Font Awesome icons
tags$style("@import url(https://use.fontawesome.com/releases/v5.7.2/css/all.css);"),
theme = light_theme,
title = span(icon("youtube", style = "color: #D80808"),
"YouTube Trend Visualizer", style = 'font-size: 30px'),
sidebarLayout(
sidebarPanel(
width = 2,
dateRangeInput(
inputId = "daterange",
label = span("Trending Date Range:", style = 'font-size: 20px'),
start = min(data$trending_date),
end = "2020-08-19",
min = min(data$trending_date),
max = max(data$trending_date),
format = "yyyy-mm-dd"
),
shinydashboard::valueBoxOutput("video_count_box", width = "100%"),
shinydashboard::valueBoxOutput("channel_count_box", width = "100%"),
shinyWidgets::materialSwitch(
inputId = "toggle_theme",
label = span(icon("moon"), "Dark Mode"),
value = FALSE,
status = "info"
),
downloadButton("report", "Generate report"),
),
mainPanel(
width = 10,
layout_column_wrap(
width = 1/2,
card(
full_screen = TRUE,
card_header(
class = "bg-dark",
span(icon("arrow-trend-up"), "Distribution Boxplots", style = 'font-size: 20px')),
card_body_fill(
fluidRow(
column(6,
selectInput(
inputId = "boxplotdist",
label = "Distribution Metric:",
choices = boxplot_options,
selected = "Comments"
)
),
column(6,
shinyWidgets::materialSwitch(
inputId = "rm_outliers",
label = "Exclude Outliers (> 0.9 Quantile)",
status = "primary"
)
)
),
shinycssloaders::withSpinner(plotlyOutput(outputId = "boxplot"), color="#D80808")
)
),
card(
full_screen = TRUE,
card_header(
class = "bg-dark",
span(icon("users"), "Trending Videos by Channel", style = 'font-size: 20px')),
card_body_fill(
selectInput(
inputId = "barplotcat",
label = "Category:",
choices = unique(data$categoryId),
selected = "Music"
),
shinycssloaders::withSpinner(plotlyOutput(outputId = "barplot"), color="#D80808")
)
),
card(
full_screen = TRUE,
card_header(
class="bg-dark",
span(icon("hashtag"), "Common Tags by Category", style = 'font-size: 20px')),
card_body_fill(
fluidRow(
column(6,
shinyWidgets::pickerInput(
inputId = "bubbleCats",
label = "Category:",
choices = sort(unique(data$categoryId)),
selected = unique(data$categoryId),
multiple = TRUE,
options = shinyWidgets::pickerOptions(
actionsBox = TRUE,
countSelectedText = "{0} categories",
selectedTextFormat = "count > 2",
width = 'fit')
)
),
column(6,
sliderInput(
inputId = "num_tags",
label = "Number of Tags:",
min = 12, max = 36, value = 24
)
)
),
shinycssloaders::withSpinner(plotlyOutput('bubble'), color="#D80808")
)
),
card(
full_screen = TRUE,
card_header(
class="bg-dark",
span(icon("clock"), "Popular Publishing Times", style = 'font-size: 20px')),
card_body_fill(
fluidRow(
column(6,
selectInput(
inputId = "representation_format",
label = "Format:",
choices = interval_choices,
selected = "Day of Week"
)
),
column(6,
selectInput(
inputId = "vid_category",
label = "Category:",
choices = unique(data$categoryId),
selected = "Music"
)
)
),
shinycssloaders::withSpinner(plotOutput('polar_coor', width = "100%"), color="#D80808")
)
)
)
)
),
footer = tags$div(
class = "footer",
p(
hr(),
column(4, p()),
column(4, p()),
column(4, p())
),
p("2023 © D. Cairns, N. Cho, L. Zung")
)
)
)
server <- function(input, output, session) {
# Dark Mode
observe({
session$setCurrentTheme(
if (isTRUE(input$toggle_theme)) {
dark_theme
} else { light_theme }
)
})
# Filter data by date universally
data_by_date <- reactive({
# Catch if dates are set correctly
validate(
need(input$daterange[1] <= input$daterange[2], "End date must be after start date.")
)
data <- data |>
dplyr::select(-title, -comments_disabled, -ratings_disabled) |>
dplyr::filter(trending_date >= input$daterange[1] & trending_date <= input$daterange[2])
return(data)
})
# Video Counter
output$video_count_box <- renderValueBox({
shinydashboard::valueBox(
span(icon("video"), length(unique(data_by_date()$video_id))),
subtitle = "Total Video Count"
)
})
# Channel Counter
output$channel_count_box <- renderValueBox({
shinydashboard::valueBox(
span(icon("user"), length(unique(data_by_date()$channelId))),
subtitle = "Total Channel Count"
)
})
# Download report
output$report <- downloadHandler(
filename = "report.html",
content = function(file) {
tempReport <- file.path(tempdir(), "report.Rmd")
file.copy("report.Rmd", tempReport, overwrite = TRUE)
params <- list(daterange = input$daterange,
rm_outliers = input$rm_outliers,
boxplotdist = input$boxplotdist,
barplotcat = input$barplotcat,
bubbleCats = input$bubbleCats,
num_tags = input$num_tags,
vid_category = input$vid_category,
representation_format = input$representation_format)
id <- showNotification(
"Rendering report...",
duration = NULL,
closeButton = FALSE
)
on.exit(removeNotification(id), add = TRUE)
rmarkdown::render("report.Rmd",
output_file = file,
params = params,
envir = new.env(parent = globalenv())
)
}
)
# Filter out outliers if toggled
boxplot_data <- reactive({
if (isTRUE(input$rm_outliers)) {
data <- data_by_date() |>
dplyr::filter(!!rlang::sym(input$boxplotdist) < quantile(!!rlang::sym(input$boxplotdist), 0.9))
return(data)
} else {
return(data_by_date())
}
})
# Category Boxplot
output$boxplot <- plotly::renderPlotly({
#Create plot
box_plot <- boxplot_data() |>
dplyr::arrange(trending_date) |>
dplyr::distinct(video_id, .keep_all = TRUE) |> # keep most recent data point for accurate tracking (no aggregating the same video)
ggplot2::ggplot() +
ggplot2::geom_boxplot(
aes(
x = forcats::fct_reorder(categoryId, !!rlang::sym(input$boxplotdist)),
y = !!rlang::sym(input$boxplotdist),
fill = categoryId
)
) +
ggplot2::labs(
y = names(boxplot_options[which(boxplot_options == input$boxplotdist)]),
x = 'Category'
) +
ggplot2::scale_y_continuous(labels = scales::label_number(scale_cut = cut_short_scale()), breaks = scales::breaks_pretty(n = 5)) +
ggplot2::scale_fill_manual(values = boxplot_colours) +
ggplot2::guides(fill = FALSE) +
ggplot2::theme(axis.title.x = element_text(size = 14, face = "bold"),
axis.title.y = element_text(size = 14, face = "bold")) +
ggplot2::coord_flip()
# Display the plot
plotly::ggplotly(box_plot, tooltip = "text")
})
# Channel Barplot
output$barplot <- plotly::renderPlotly({
# Check if data exists for the filter
validate(
need(input$barplotcat %in% data_by_date()$categoryId, "Category is not present in subset. Select a different category.")
)
# Create plot
bar_plot <- data_by_date() |>
dplyr::filter(categoryId == input$barplotcat) |>
dplyr::group_by(channelId, channelTitle) |>
dplyr::summarise(video_count = length(unique(video_id))) |>
dplyr::arrange(dplyr::desc(video_count)) |>
dplyr::ungroup() |>
dplyr::slice(1:10) |>
ggplot2::ggplot(
aes(
x = video_count,
y = reorder(channelTitle, video_count),
text = paste(
"Count: ", video_count)
)
) +
ggplot2::geom_bar(
stat = "identity",
fill = names(barplot_colours[which(barplot_colours == input$barplotcat)])) +
ggplot2::labs(
x = 'Count of Videos',
y = 'Channel Name'
) +
ggplot2::scale_y_discrete(labels = function(x) {
stringr::str_wrap(x, width = 20)
}) +
ggplot2::scale_x_continuous(breaks = scales::pretty_breaks()) +
ggplot2::theme(axis.title.x = element_text(size = 14, face = "bold"),
axis.title.y = element_text(size = 14, face = "bold"))
# Display the plot
plotly::ggplotly(bar_plot, tooltip = "text")
})
# Bubble Tags Plot
output$bubble <- plotly::renderPlotly({
# Check if data exists for the filter
validate(
need(input$bubbleCats != "", "Please select a category."),
)
validate(
need(data_by_date()$categoryId %in% input$bubbleCats, "Category is not present in subset. Select a different category.")
)
filtered_tag_counts <- data_by_date() |>
# Filter date and categories
dplyr::filter(categoryId %in% input$bubbleCats) |>
# Lowercase, count and sort remaining tags
dplyr::mutate(tags = tolower(tags)) |>
dplyr::pull(tags) |>
stringr::str_split(fixed("|")) |>
unlist() |>
table(dnn = c("tag")) |>
sort(decreasing = TRUE) |>
as.data.frame() |>
subset(tag != "[none]")
# Functions to "pack" the circles in a nice layout
packing <- packcircles::circleProgressiveLayout(filtered_tag_counts$Freq[1:input$num_tags]) |>
mutate(radius = 0.95* radius,
id = dplyr::row_number())
packing$counts <- filtered_tag_counts$Freq[1:input$num_tags]
bubbleplot_data <- packcircles::circleLayoutVertices(packing) |>
merge(y = select(packing, id, radius, counts), by = "id")
bubble_labels <- stringr::str_wrap(filtered_tag_counts$tag[1:input$num_tags], 10)
# Create the plot
bubble_plot <- ggplot2::ggplot(bubbleplot_data, aes(x, y, text = paste("Rank: ", id))) +
ggplot2::geom_polygon(aes(group = id, fill = counts),
colour = "black", show.legend = TRUE) +
ggplot2::geom_text(data = packing,
aes(x, y, text = paste("Tag: ", filtered_tag_counts$tag[1:input$num_tags], "\nNumber of Videos: ", counts)),
label = bubble_labels, size = 3, color = "white") +
ggplot2::scale_fill_gradient(name = "Num. of\nVideos", high = "#FF0000", low = "#440000") +
ggplot2::theme(
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank()
)
# Display the plot
plotly::ggplotly(bubble_plot, tooltip = "text")
})
# Polar Coordinates
# There is currently an open issue regarding the integration of coord_polar in plotly
# (https://github.com/plotly/plotly.R/issues/878)
output$polar_coor <- renderPlot({
# Check if data exists for the filter
validate(
need(input$vid_category %in% data_by_date()$categoryId, "Category is not present in subset. Select a different category.")
)
data_filtered <- data_by_date() |>
# Filtering dataset by category
dplyr::filter(categoryId == input$vid_category) |>
# Creating new columns for date components
dplyr::mutate(publishedAt = lubridate::ymd_hms(publishedAt)) |>
dplyr::mutate(publish_date = lubridate::date(publishedAt),
publish_month = lubridate::month(publishedAt, label = TRUE),
publish_wday = lubridate::wday(publishedAt, label = TRUE),
publish_hour = lubridate::hour(publishedAt)) |>
dplyr::group_by(!!rlang::sym(input$representation_format)) |>
dplyr::summarise(video_count = length(unique(video_id)))
# Render plot
ggplot2::ggplot(data_filtered,
aes(x = .data[[input$representation_format]], y = video_count, fill = video_count)) +
ggplot2::geom_bar(stat = "identity") +
ggplot2::xlab(names(interval_choices[which(interval_choices == input$representation_format)])) +
ggplot2::scale_fill_distiller(palette = "YlGnBu", direction = 1, name = "Number of Videos") +
ggplot2::coord_polar() +
ggplot2::theme(axis.title.x = element_text(size = 22, face = "bold"),
axis.text.x = element_text(size = 26),
axis.title.y = element_blank(),
axis.ticks.y = element_blank(),
axis.text.y = element_blank(),
legend.text = element_text(size = 18),
legend.title = element_text(size = 20, face = "bold"),
legend.box.margin = margin(12, 12, 12, 12))
})
}
shinyApp(ui, server)