-
Notifications
You must be signed in to change notification settings - Fork 1
/
netflex.Rmd
518 lines (442 loc) · 15.5 KB
/
netflex.Rmd
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
---
title: "NETFLEX"
runtime: shiny
output:
flexdashboard::flex_dashboard:
favicon: "src/flavicon.jpeg"
source_code: embed
theme: spacelab
css: src/netflex_lightened2.css
social: "menu"
vertical_layout: fill
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(message = FALSE, warning = FALSE)
# Packages loading.
library(shiny)
library(shinythemes)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyjs)
library(shinyBS)
library(shinyLP)
library(shinycssloaders)
library(shinybusy)
library(shinyjs)
library(flexdashboard)
library(leaflet)
library(viridis)
library(shinyWidgets)
library(waiter)
library(hrbrthemes)
library(htmltools)
library(tidyverse)
library(FactoMineR)
library(factoextra)
library(GGally)
library(rgdal)
library(sp)
library(rgeos)
library(scales)
library(data.table)
library(DT)
library(dygraphs)
library(plotly)
library(xts)
library(magrittr)
library(ggridges)
library(gplots)
library(wordcloud)
library(vov)
library(adegraphics)
library(hpackedbubble)
```
```{r global, message=FALSE, warning=FALSE, include=FALSE, paged.print=FALSE}
# Data loading
## Main dataset
mydata <- read.csv("src/data/netflex5.csv")
mydata$Show_type[is.na(mydata$Show_type)] <- 'undefined'
mydata$Genre[is.na(mydata$Genre)] <- 'undefined'
## Matrix of genres
mat <- read.csv("src/data/mat.csv",row.names = 1, encoding = "utf8")
## Shapefile data disabled
# countriesborders <- readOGR("src/TM_WORLD_BORDERS_SIMPL-0.3.shp")
```
# Home Page {data-orientation=rows}
Row
-------------------------------------
```{r}
# Busy bar for UX enhancement
add_busy_bar(timeout = 400, color = "#c7636a", centered = FALSE, height = "6px")
# Background image adding. Origin : https://hcdevilsadvocate.com/ae/2019/01/25/best-new-netflix-releases-to-binge-during-the-cold-weather/
tags$img(
src = "src/bckgrnd0.jpeg",
style = 'position: fixed'
)
```
### ValBox {data-height=800}
```{r}
# Disclaimer : The following jumbotron function is an adpatation of Jumbotron() from the shinyLP package (Copyright holder : Jasmine Dumas // https://github.com/cran/shinyLP). We adapted it to receive a btn link and enhanced the rendering.
# Jumbotron
## Jumbotron function for Landing page
my_jumbotron <- function(header , content, button = TRUE, button_link, ...){
button_label = c(...)
if (button){
div(class = "jumbotron",
h1(header), p(content), p(a(href = button_link ,target="_blank",
class = "btn btn-primary btn-lg button", role= 'button', button_label)))
} else {
div(class = "jumbotron", h1(header), p(content))
}
}
## Fixed landing page fading in, with a btn link to our github reporsitory
fixedPage(
use_vov(),
fade_in_up(
my_jumbotron(header="Welcome to NETFLEX", content="Please call attention to important features of the app", button = "True", button_link="https://github.com/Glastos/Projet-Shiny", button_Label = "Check our repo'"))
)
```
Row {data-height=150}
-------------------------------------
Row
-------------------------------------
```{r}
# Empty row like this help better organize the landing page
```
### Profiles
```{r}
# ValueBox displaying the number of unique profiles used in our app
renderValueBox({
valueBox(
tags$p(length(unique(mydata$Profile)),
style = "font-size: 200%; color: #FFFFFF;"),
tags$q("Netflix Profiles",style = "font-size: 150%; color: #FFFFFF;"),
icon ="fa-users",color = "green")
})
```
### Movies seen
```{r}
# ValueBox displaying the number of movies seen by profiles in our app
renderValueBox({
valueBox(
tags$p(table(mydata$Show_type=="movie")[2],
style = "font-size: 200%; color: #FFFFFF;"),
tags$q("Movies seen",style = "font-size:150%; color: #FFFFFF;"),
icon ="fa-video", color = "orange")
})
```
### Series seen
```{r}
# ValueBox displaying the number of series seen by profiles in our app
renderValueBox({
valueBox(
tags$p(table(mydata$Show_type=="series")[2],
style = "font-size: 200%; color:#FFFFFF;"),
tags$q("Series seen",style = "font-size: 150%; color: #FFFFFF;"),
icon ="fa-film", color="red")
})
```
Row
-------------------------------------
```{r}
```
# Explanatory visualization {data-orientation=column}
## Input {.sidebar}
```{r echo=FALSE}
# This sidebar able the user to choose whether to see visualizations for all the profiles or a specific profile. It is also possible to choose to see yearly, monthly, weekly or daily visualizations
selectInput("profile", "Choose a profile", choices = c("All",unique(mydata$Profile)), selected = "All")
radioButtons("time_range_1", "Time range", choices = list("All time" = 1, "Monthly" = 2, "Weekly" = 3, "Daily" = 4), selected = 1)
```
## Column {.tabset}
### Temporal viz
```{r echo=FALSE}
## Reactively filtering the dataset
mydata_p <- reactive({
if (input$profile != "All") {
mydata_p <- filter(mydata, Profile == input$profile)
} else {
mydata_p <- mydata
}
})
## All time plot
output$dygraphAllTime <- renderDygraph({
data_d <- mydata_p()[,c("Show_type", "Start_time")] %>%
mutate(Date = as.Date(Start_time))%>%
group_by(Date, Show_type)%>%
tally()%>%
pivot_wider(names_from = Show_type, values_from = n)%>%
mutate_at(c('movie','series','undefined'),~replace(., is.na(.), 0))
data_t <- xts(data_d[2:4], data_d$Date)
dygraph(data_t)%>%
dyRangeSelector(dateWindow = c(min(mydata_p()$Start_time), max(mydata_p()$Start_time)))
})
```
```{r echo=FALSE}
## Yearly plot
output$plotYearly <- renderPlotly({
mydata_d <- mydata_p()[,c("Show_type", "Month")] %>%
group_by(Month, Show_type)%>%
tally()%>%
pivot_wider(names_from = Show_type, values_from = n)%>%
mutate_at(c('movie','series','undefined'),~replace(., is.na(.), 0))
mydata_d$Month <- factor(mydata_d$Month, levels = month.name)
plot_ly(mydata_d, x = ~Month, y = ~movie, name = "Movies", type = "bar", colors = viridis_pal(option = "D")(3))%>%
add_trace(y = ~series, name = 'Series')%>%
add_trace(y = ~undefined, name = 'Undefined')%>%
layout(yaxis = list(title = 'Count'), barmode = 'group')
})
```
```{r echo=FALSE}
## Montly plot
output$plotWeekly <- renderPlotly({
mydata_d <- mydata_p()[,c("Show_type", "Day")] %>%
group_by(Day, Show_type)%>%
tally()%>%
pivot_wider(names_from = Show_type, values_from = n)%>%
mutate_at(c('movie','series','undefined'),~replace(., is.na(.), 0))
mydata_d$Day <- factor(mydata_d$Day, levels = c('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'))
plot_ly(mydata_d, x = ~Day, y = ~movie, name = "Movies", type = "bar", colors = viridis_pal(option = "D")(3))%>%
add_trace(y = ~series, name = 'Series')%>%
add_trace(y = ~undefined, name = 'Undefined')%>%
layout(yaxis = list(title = 'Count'), barmode = 'group')
})
```
```{r echo=FALSE}
## Daily plot
output$plotHourly <- renderPlotly({
mydata_d <- mydata_p()[,c("Show_type", "Hours")] %>%
group_by(Hours, Show_type)%>%
tally()%>%
pivot_wider(names_from = Show_type, values_from = n)%>%
mutate_at(c('movie','series','undefined'),~replace(., is.na(.), 0))
plot_ly(mydata_d, x = ~Hours, y = ~movie, name = "Movies", type = "bar", colors = viridis_pal(option = "D")(3))%>%
add_trace(y = ~series, name = 'Series')%>%
add_trace(y = ~undefined, name = 'Undefined')%>%
layout(yaxis = list(title = 'Count'), barmode = 'group')
})
```
```{r echo=FALSE}
## Reactive outputting
output$graph1 <- renderUI({
if (input$time_range_1 == 1) {
dygraphOutput("dygraphAllTime")
} else if (input$time_range_1 == 2) {
plotlyOutput("plotYearly")
} else if (input$time_range_1 == 3) {
plotlyOutput("plotWeekly")
} else {
plotlyOutput("plotHourly")
}
})
uiOutput("graph1")
```
### Top watched
```{r}
## Top 5 most watched shows
freqfunc <- function(x, n){
tail(
sort(table(
unlist(strsplit(as.character(x), ", "))
)), n)
}
output$plotPref <- renderPlot({
if (input$profile != "All") {
freq_data <- as.data.frame(freqfunc(mydata[which(mydata$Profile==input$profile),]$Title, 5))
} else {
validate(
need(input$profile != "All", "Please choose a single profile."))
}
ggplot(freq_data, aes(x=Var1, y=Freq)) +
geom_segment( aes(xend=Var1, yend=0)) +
geom_point( size=4, color="orange") +
theme_bw() +
xlab("Shows") +
ylab("Count") +
ggtitle(paste0("Top 5 most watched shows for ",as.character(input$profile))) +
theme(plot.title = element_text(hjust = 0.5))
})
plotOutput("plotPref")
```
### Worldcloud
```{r}
## Worldcloud of most seen genres reactive with the sidebar input
genres <- reactive({
if (input$profile != "All") {
genres <- data.frame(t(mat[as.character(input$profile),]))
colnames(genres) <- "Count"
genres <- tibble::rownames_to_column(genres, var="Names")
return(genres)
} else {
genres <- data.frame("Count" = colSums(mat))
genres <- tibble::rownames_to_column(genres, var="Names")
return(genres)
}
})
renderPlot({
wordcloud(words = as.character(colnames(mat)), freq = c(genres()[,2]), min.freq = 1, max.words=25, random.order=FALSE, rot.per=0.35,
colors=brewer.pal(8, "Dark2"))
})
```
### Mapping
```{r}
renderText({
(("Mapping:
Display a map of the world with the different countries where people have watched
NetflixNote: Mapping is not implemented in the basic version due to is huge loading time, you can see it by uncommenting the corresponding code"))
})
```
<!-- # ```{r} -->
<!-- # # Mapping -->
<!-- # names(countriesborders@data)[5] <- "Country" -->
<!-- # Countries <- unique(dat$Country) -->
<!-- # mySHPdata <- countriesborders[countriesborders$Country %in% Countries,] -->
<!-- # newdf <- merge(mySHPdata, dat, by="Country",duplicateGeoms = TRUE) -->
<!-- # otherSHP <- countriesborders[!countriesborders$Country %in% Countries,]%>% -->
<!-- # merge(dat, by="Country",duplicateGeoms = TRUE ) -->
<!-- # -->
<!-- # ``` -->
<!-- # -->
<!-- # -->
<!-- # ```{r} -->
<!-- # # # Leaflet map -->
<!-- # options(viewer = NULL) -->
<!-- # -->
<!-- # renderLeaflet({ -->
<!-- # # Color palette -->
<!-- # newdfx <- newdf[newdf$Account==as.character(input$account),] -->
<!-- # factpal2 <- colorFactor(viridis(2), newdfx@data$Country, reverse = T) -->
<!-- # -->
<!-- # -->
<!-- # leaflet()%>% -->
<!-- # addProviderTiles(providers$CartoDB.DarkMatterNoLabels, -->
<!-- # options = providerTileOptions(noWrap = TRUE)) %>% -->
<!-- # setView(1, 15, 2.5) %>% -->
<!-- # addPolygons(data=newdfx, -->
<!-- # stroke=TRUE, -->
<!-- # fillColor = ~factpal2(newdfx@data$Country), -->
<!-- # color = "black", -->
<!-- # weight = 0.5, smoothFactor = 0.5, -->
<!-- # opacity = 1.0, fillOpacity = 0.5, -->
<!-- # label = newdfx@data$Profile) %>% -->
<!-- # addPolygons(data=AutreSHP, -->
<!-- # fillColor = "black", -->
<!-- # color = "black", -->
<!-- # weight = 0.2, smoothFactor = 0.5, -->
<!-- # opacity = 0.5, fillOpacity = 0.8) %>% -->
<!-- # leaflet::addLegend(pal=factpal2, values=newdfx@data$Country, opacity=1) -->
<!-- # }) -->
<!-- # ``` -->
# Analysis {data-orientation=rows}
## Input {.sidebar}
```{r echo=FALSE}
## Sidebar for dropping a special profile from the analysis
br()
materialSwitch(inputId = "leaveVal", label = "Drop Valentin!", status = "warning")
```
Row {.tabset}
-------------------------------------
### Balloon
```{r}
## Reactively splitting the dataset
reactive_df <- reactive({
if (input$leaveVal){
return(mat[-26,-c(3,25)])
} else {
return(mat[,-c(3,25)])}
})
##Ballonplot of the contingency table for explanatory visualization
renderPlot({
dt <- as.table(as.matrix(reactive_df()[,-c(23:28)]))
balloonplot(t(dt), main = "Most seen genres for each profile",
xlab = "", ylab = "",
dotsize = 4, dotcolor = "#E50914",
label = FALSE, show.margins = FALSE,repel = TRUE)
})
```
### CA Biplot
```{r}
## CA biplot displaying
renderPlot({
res.ca <- CA(reactive_df(), graph=F)
fviz_ca_biplot(res.ca, repel = TRUE)
})
```
### Biplot_col
```{r}
## CA biplot displaying only genres
renderPlot({
res.ca <- CA(reactive_df(), graph=F)
fviz_ca_col(res.ca, col.col = "cos2",
gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),
repel = TRUE)
})
```
### Biplot_row
```{r}
## CA biplot displaying only profiles
renderPlot({
res.ca <- CA(reactive_df(), graph=F)
fviz_ca_row(res.ca, col.row = "cos2",
gradient.cols = c ("#00AFBB", "#E7B800", "#FC4E07"),
repel = TRUE)
})
```
### HCPC
```{r}
## CA biplot displaying a dendogram
renderPlot({
res.ca <- CA(reactive_df(), graph=F)
res.hcpc <- HCPC(res.ca, graph = F)
fviz_dend(res.hcpc,
k=2,
cex = 0.7,
palette = "jco",
rect = TRUE, rect_fill = TRUE,
rect_border = "jco",
labels_track_height = 0.8,
show_labels = TRUE,
color_labels_by_k=T)
})
```
### Clusters
```{r}
## HCPC clusteres
renderPlot({
res.ca <- CA(reactive_df(), graph=F)
res.hcpc <- HCPC(res.ca, graph = F)
fviz_cluster(res.hcpc, frame.type = "norm",
frame.level = 0.68,outlier.color = "black",
repel=T, show.clust.cent=TRUE,
ggtheme = theme_bw())
})
```
# Data source
Row {.tabset}
-------------------------------------
### Main data source
```{r}
# Main data table exploration
tags$style(HTML("
.mydataaTables_wrapper .mydataaTables_length, .mydataaTables_wrapper .mydataaTables_filter, .mydataaTables_wrapper .mydataaTables_info, .mydataaTables_wrapper .mydataaTables_processing, .mydataaTables_wrapper .mydataaTables_paginate, .mydataaTables_wrapper .mydataaTables_paginate .paginate_button.current:hover {
color: #000000; }
.mydataaTables_wrapper .mydataaTables_paginate .paginate_button{box-sizing:border-box;display:inline-block;min-width:1.5em;padding:0.5em 1em;margin-left:2px;text-align:center;text-decoration:none !important;cursor:pointer;*cursor:hand;color:#ffffff !important;border:1px solid transparent;border-radius:2px}
.mydataaTables_length select {
color: #ffffff;
background-color: #ffffff
}
.mydataaTables_filter input {
color: #ffffff;
background-color: #ffffff}thead {
color: #ffffff;
}tbody {
color: #000000;
}"))
renderDataTable({DT::datatable(mydata[,-c(1,2)], options = list(scrollX = TRUE,
sScrollY = '75vh', scrollCollapse = TRUE), extensions = list("Scroller"))})
```
### Genres contingency table
```{r}
# Contingency table exploration
renderDataTable({DT::datatable(mat, options = list(scrollX = TRUE,
sScrollY = '75vh', scrollCollapse = TRUE), extensions = list("Scroller"))})
```