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", NULL, width = "600px") ), plotlyOutput("show_graph", width = "100%", height = "800px") ) server <- function(input, output, session) { observe({ queryString <- getQueryString(session) selected <- ifelse(queryString$show %>% is.null, "tt0944947", queryString$show) updateSelectizeInput(session, "show", choices = shows, selected = selected, server = T) }) observe({ updateQueryString(str_c("?show=", input$show), "replace", 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, "
", "Rating: ", averageRating, " (", numVotes, " votes)
")) %>% 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)