|
- library(tidyverse)
- library(plotly)
-
- readRDS("imdb.rds") -> imdb
-
- imdb %>%
- distinct(id, .keep_all = T) %>%
- mutate(title = str_c(seriesTitle, " (", startYear, ")")) %>%
- select(id, title) -> shows
-
- shows$id %>%
- setNames(shows$title) -> shows
-
- if (shows %>% names %>% is.na %>% which %>% length > 0)
- shows <- shows[- (names(shows) %>% is.na %>% which)]
-
- ui <- fluidPage(
- inputPanel(width = "20%", height = "100%",
- h1("GraphTV"),
- selectizeInput("show", "Search show", shows, selected = "tt0944947", width = "600px")
- ),
- plotlyOutput("show_graph", width = "100%", height = "800px")
- )
-
- server <- function(input, output, session)
- {
- output$show_graph <- renderPlotly({
- req(input$show)
-
- show_id <- input$show
-
- imdb %>%
- filter(id == show_id) %>%
- by(.$season, function(x){
- fit <- lm(averageRating ~ episode, data = x)
- x %>%
- plot_ly(hoverinfo = "text",
- text = ~str_c("S", season, "E", episode, " - ", episodeTitle, "<br>",
- "Rating: ", averageRating, " (", numVotes, " votes)<br>")) %>%
- add_trace(data = x, x = ~episode, y = ~averageRating, mode = "markers") %>%
- add_trace(data = x, x = ~episode, y = fitted(fit), mode = "lines") %>%
- layout(xaxis = list(tick0 = 0, dtick = 1),
- yaxis = list(title = "Rating"))
- }) %>%
- subplot(shareY = T, nrows = 1, margin = 0) %>%
- hide_legend
- })
- }
-
- shinyApp(ui = ui, server = server)
|