A graphTV clone
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.8KB

  1. library(tidyverse)
  2. library(plotly)
  3. readRDS("imdb.rds") -> imdb
  4. imdb %>%
  5. distinct(id, .keep_all = T) %>%
  6. mutate(title = str_c(seriesTitle, " (", startYear, ")")) %>%
  7. select(id, title) -> shows
  8. shows$id %>%
  9. setNames(shows$title) -> shows
  10. if (shows %>% names %>% is.na %>% which %>% length > 0)
  11. shows <- shows[- (names(shows) %>% is.na %>% which)]
  12. ui <- fluidPage(
  13. inputPanel(width = "20%", height = "100%",
  14. h1("GraphTV"),
  15. selectizeInput("show", "Search show", NULL, width = "600px")
  16. ),
  17. plotlyOutput("show_graph", width = "100%", height = "800px")
  18. )
  19. server <- function(input, output, session)
  20. {
  21. observe({
  22. queryString <- getQueryString(session)
  23. selected <- ifelse(queryString$show %>% is.null, "tt0944947", queryString$show)
  24. updateSelectizeInput(session, "show", choices = shows, selected = selected, server = T)
  25. })
  26. observe({
  27. updateQueryString(str_c("?show=", input$show), "replace", session)
  28. })
  29. output$show_graph <- renderPlotly({
  30. req(input$show)
  31. show_id <- input$show
  32. imdb %>%
  33. filter(id == show_id) %>%
  34. by(.$season, function(x){
  35. fit <- lm(averageRating ~ episode, data = x)
  36. x %>%
  37. plot_ly(hoverinfo = "text",
  38. text = ~str_c("S", season, "E", episode, " - ", episodeTitle, "<br>",
  39. "Rating: ", averageRating, " (", numVotes, " votes)<br>")) %>%
  40. add_trace(data = x, x = ~episode, y = ~averageRating, mode = "markers") %>%
  41. add_trace(data = x, x = ~episode, y = fitted(fit), mode = "lines") %>%
  42. layout(xaxis = list(tick0 = 0, dtick = 1),
  43. yaxis = list(title = "Rating"))
  44. }) %>%
  45. subplot(shareY = T, nrows = 1, margin = 0) %>%
  46. hide_legend
  47. })
  48. }
  49. shinyApp(ui = ui, server = server)