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.

94 line
2.2KB

  1. ---
  2. title: GraphTV
  3. runtime: shiny
  4. output:
  5. html_document
  6. ---
  7. ```{r setup, message = F, warning = F, echo = F}
  8. library(tidyverse)
  9. library(httr)
  10. library(rvest)
  11. library(plotly)
  12. library(jsonlite)
  13. library(knitr)
  14. opts_chunk$set(message = F,
  15. echo = F,
  16. warning = F)
  17. key <- "?api_key=32e0ed1416c58777f986f3f33751fc22"
  18. tmdb_url <- "http://api.themoviedb.org/3/"
  19. ```
  20. ```{r input}
  21. inputPanel(
  22. textInput("show_search", "Search show"),
  23. actionButton("button_search", "Search show"),
  24. uiOutput("show_choice")
  25. )
  26. shows <- eventReactive(input$button_search, {
  27. req(input$show_search)
  28. serie <- input$show_search %>% url_escape
  29. GET(str_c(tmdb_url, "search/tv", key, "&query=", serie)) %>%
  30. content(as = "text") %>%
  31. fromJSON -> res
  32. #res$results$poster_path
  33. res$results$id %>%
  34. as.list %>%
  35. setNames(res$results$original_name)
  36. })
  37. output$show_choice <- renderUI({
  38. req(shows())
  39. selectizeInput("show", "Please choose…", shows())
  40. })
  41. ```
  42. ```{r graph}
  43. renderPlotly({
  44. req(input$show)
  45. show_id <- input$show
  46. GET(str_c(tmdb_url, "tv/", show_id, key)) %>%
  47. content(as = "text") %>%
  48. fromJSON -> res
  49. res$backdrop_path
  50. 1:res$number_of_seasons -> seasons
  51. res$vote_average
  52. seasons %>%
  53. map(~ GET(str_c(tmdb_url, "tv/", show_id, "/season/", ., key)) %>%
  54. content(as = "text") %>%
  55. fromJSON) -> res
  56. res %>%
  57. map("episodes") %>%
  58. reduce(bind_rows) %>%
  59. select(season_number, episode_number, name, still_path, vote_average) -> episodes
  60. episodes %>%
  61. by(.$season_number, function(x){
  62. fit <- lm(vote_average ~ episode_number, data = x)
  63. x %>%
  64. plot_ly(hoverinfo = "text",
  65. text = ~str_c("S", season_number, "E", episode_number, " - ", name, "<br>",
  66. "Rating: ", vote_average, "<br>")) %>%
  67. add_trace(data = x, x = ~episode_number, y = ~vote_average, mode = "markers") %>%
  68. add_trace(data = x, x = ~episode_number, y = fitted(fit), mode = "lines") %>%
  69. layout(xaxis = list(tick0 = 0, dtick = 1),
  70. yaxis = list(title = "Rating"))
  71. }) %>%
  72. subplot(shareY = T, nrows = 1) %>%
  73. hide_legend
  74. })
  75. ```