library(shiny)
## Warning: package 'shiny' was built under R version 4.5.3
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.2.0 ✔ readr 2.2.0
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ ggplot2 4.0.2 ✔ tibble 3.3.1
## ✔ lubridate 1.9.5 ✔ tidyr 1.3.2
## ✔ purrr 1.2.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(plotly)
##
## Attaching package: 'plotly'
##
## The following object is masked from 'package:ggplot2':
##
## last_plot
##
## The following object is masked from 'package:stats':
##
## filter
##
## The following object is masked from 'package:graphics':
##
## layout
library(DT)
##
## Attaching package: 'DT'
##
## The following objects are masked from 'package:shiny':
##
## dataTableOutput, renderDataTable
library(lubridate)
library(stringr)
# Load + clean data
netflix_raw <- read_csv("netflix_titles.csv")
## Rows: 8807 Columns: 12
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (11): show_id, type, title, director, cast, country, date_added, rating,...
## dbl (1): release_year
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
netflix_clean <- netflix_raw %>%
filter(!is.na(release_year), !is.na(country)) %>%
separate_rows(country, sep = ", ") %>%
mutate(
type = as.factor(type),
rating = as.factor(rating),
release_year = as.numeric(release_year),
date_added = mdy(date_added),
month_added = month(date_added, label = TRUE)
)
# ---------------- UI ----------------
ui <- fluidPage(
tags$head(
tags$style(HTML("
body { background-color:#141414; color:white; font-family:Arial; }
.card { background:#1f1f1f; padding:20px; border-radius:12px; margin-bottom:20px; }
.kpi { font-size:32px; color:#E50914; font-weight:bold; }
.header { color:#E50914; font-size:36px; font-weight:bold; }
table.dataTable {
background-color: #1f1f1f !important;
color: white !important;
}
table.dataTable thead {
background-color: #E50914 !important;
color: white !important;
}
table.dataTable tbody tr {
background-color: #1f1f1f !important;
}
table.dataTable tbody tr:nth-child(even) {
background-color: #181818 !important;
}
table.dataTable tbody tr:hover {
background-color: #333333 !important;
}
.dataTables_wrapper {
color: white !important;
}
.dataTables_length,
.dataTables_filter,
.dataTables_info,
.dataTables_paginate {
color: white !important;
}
"))
),
fluidRow(
column(12,
div(class = "header", "NETFLIX INTELLIGENCE"),
p("Interactive Content Strategy Dashboard", style="color:#aaa;")
)
),
fluidRow(
column(3, selectInput("country","Country",choices=c("All", sort(unique(netflix_clean$country))))),
column(3, selectInput("type","Type",choices=c("All", levels(netflix_clean$type)))) ,
column(3, selectInput("rating","Rating",choices=c("All", levels(netflix_clean$rating)))) ,
column(3, sliderInput("year","Release Year",
min=min(netflix_clean$release_year),
max=max(netflix_clean$release_year),
value=range(netflix_clean$release_year)))
),
fluidRow(
column(12, textInput("search","Search Title", placeholder="Type a movie or show..."))
),
fluidRow(
column(4, div(class="card", div(class="kpi", textOutput("total_titles")), "Total Titles")),
column(4, div(class="card", div(class="kpi", textOutput("countries")), "Countries")),
column(4, div(class="card", div(class="kpi", textOutput("year_range")), "Year Range"))
),
fluidRow(
column(6, div(class="card", plotlyOutput("growth_plot"))),
column(6, div(class="card", plotlyOutput("country_plot")))
),
fluidRow(
column(6, div(class="card", plotlyOutput("type_plot"))),
column(6, div(class="card", plotlyOutput("season_plot")))
),
fluidRow(
column(6, div(class="card", plotlyOutput("genre_plot"))),
column(6, div(class="card", plotlyOutput("rating_plot")))
),
fluidRow(
column(12, div(class="card", DTOutput("table")))
)
)
# ---------------- SERVER ----------------
server <- function(input, output) {
filtered <- reactive({
data <- netflix_clean
if (input$country != "All")
data <- data %>% filter(country == input$country)
if (input$type != "All")
data <- data %>% filter(type == input$type)
if (input$rating != "All")
data <- data %>% filter(rating == input$rating)
data <- data %>%
filter(release_year >= input$year[1],
release_year <= input$year[2])
if (input$search != "")
data <- data %>% filter(str_detect(title, regex(input$search, ignore_case = TRUE)))
data
})
output$total_titles <- renderText(nrow(filtered()))
output$countries <- renderText(n_distinct(filtered()$country))
output$year_range <- renderText(
paste(min(filtered()$release_year), "-", max(filtered()$release_year))
)
output$growth_plot <- renderPlotly({
req(nrow(filtered()) > 1)
ggplotly(
filtered() %>%
count(release_year) %>%
ggplot(aes(release_year, n)) +
geom_line(color="#E50914") +
geom_point(color="white")
)
})
output$country_plot <- renderPlotly({
req(nrow(filtered()) > 0)
ggplotly(
filtered() %>%
count(country, sort=TRUE) %>%
slice_head(n=10) %>%
ggplot(aes(reorder(country,n), n)) +
geom_col(fill="#E50914") +
coord_flip()
)
})
output$type_plot <- renderPlotly({
ggplotly(ggplot(filtered(), aes(type, fill=type)) + geom_bar())
})
output$season_plot <- renderPlotly({
ggplotly(ggplot(filtered(), aes(month_added)) + geom_bar(fill="#E50914"))
})
output$genre_plot <- renderPlotly({
ggplotly(
filtered() %>%
mutate(genre = str_extract(listed_in, "^[^,]+")) %>%
count(genre, sort=TRUE) %>%
slice_head(n=10) %>%
ggplot(aes(reorder(genre,n), n)) +
geom_col(fill="#b20710") +
coord_flip()
)
})
output$rating_plot <- renderPlotly({
ggplotly(ggplot(filtered(), aes(rating)) + geom_bar(fill="#E50914"))
})
output$table <- renderDT({
datatable(
filtered() %>%
select(title, country, type, rating, release_year),
options = list(
pageLength = 10,
scrollX = TRUE
),
class = "display nowrap compact"
) %>%
formatStyle(
columns = c("title", "country", "type", "rating", "release_year"),
backgroundColor = "#1f1f1f",
color = "white"
)
})
}
shinyApp(ui, server)
Shiny applications not supported in static R Markdown documents