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.

50 lines
1.5KB

  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. ui <- fluidPage(
  11. inputPanel(width = "20%", height = "100%",
  12. h1("GraphTV"),
  13. selectizeInput("show", "Search show", shows, selected = "tt0944947", width = "600px")
  14. ),
  15. plotlyOutput("show_graph", width = "100%", height = "800px")
  16. )
  17. server <- function(input, output, session)
  18. {
  19. output$show_graph <- renderPlotly({
  20. req(input$show)
  21. show_id <- input$show
  22. imdb %>%
  23. filter(id == show_id) %>%
  24. collect %>%
  25. mutate_at(vars(season, episode, averageRating, numVotes), as.numeric) %>%
  26. by(.$season, function(x){
  27. fit <- lm(averageRating ~ episode, data = x)
  28. x %>%
  29. plot_ly(hoverinfo = "text",
  30. text = ~str_c("S", season, "E", episode, " - ", episodeTitle, "<br>",
  31. "Rating: ", averageRating, " (", numVotes, " votes)<br>")) %>%
  32. add_trace(data = x, x = ~episode, y = ~averageRating, mode = "markers") %>%
  33. add_trace(data = x, x = ~episode, y = fitted(fit), mode = "lines") %>%
  34. layout(xaxis = list(tick0 = 0, dtick = 1),
  35. yaxis = list(title = "Rating"))
  36. }) %>%
  37. subplot(shareY = T, nrows = 1, margin = 0) %>%
  38. hide_legend
  39. })
  40. }
  41. shinyApp(ui = ui, server = server)