jorandradefig

hallazgosmusicales-2.R

Jul 11th, 2021 (edited)
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 4.81 KB | None | 0 0
  1. #########
  2. # Date: 4/6/21
  3. # Author: JA
  4. #########
  5.  
  6. pacman::p_load(
  7.   #dataframe
  8.   tidyverse,
  9.   ggrepel,
  10.   #viz
  11.   plotly,
  12.   #webapps
  13.   shiny,
  14.   #api
  15.   spotifyr
  16. )
  17.  
  18.  
  19. # Application
  20. # Platform
  21.  
  22. # API - Application Public Interface
  23. # REST - Resource State Transfer
  24. # JSON - JavaScript Object Notation
  25.  
  26. # Authentication
  27. # Authorization
  28.  
  29. readRenviron(".env")
  30.  
  31. # Dashboard
  32. # Tablero
  33.  
  34. # (Graphical) User Interface
  35. ui <- pageWithSidebar(
  36.   titlePanel("Hallazgos Musicales"),
  37.   sidebarPanel(
  38.     htmlOutput(
  39.       outputId="selecta"
  40.     ),
  41.     htmlOutput(
  42.       outputId="selectb"
  43.     ),
  44.     selectInput(
  45.       inputId="x",
  46.       label="X:",
  47.       choices=c("acousticness","danceability","duration_ms","energy","instrumentalness","key","key_name","key_mode","mode","mode_name","liveness","loudness","speechiness","tempo","time_signature","valence"),
  48.       selected="valence"
  49.     ),
  50.     selectInput(
  51.       inputId="y",
  52.       label="Y:",
  53.       choices=c("acousticness","danceability","duration_ms","energy","instrumentalness","key","key_name","key_mode","mode","mode_name","liveness","loudness","speechiness","tempo","time_signature","valence"),
  54.       selected="key_name"
  55.     ),
  56.   ),
  57.   mainPanel(
  58.     plotOutput(
  59.       outputId="plot"
  60.     )
  61.   )
  62. )
  63.  
  64. # Server
  65. # Reactivity
  66.  
  67. server <- function(input,output) {
  68.  
  69.   access_token <- reactive({
  70.     get_spotify_access_token(
  71.       client_id=Sys.getenv("SPOTIFY_CLIENT_ID"),
  72.       client_secret=Sys.getenv("SPOTIFY_CLIENT_SECRET")
  73.     )
  74.   })
  75.  
  76.   artists <- reactive({
  77.     toptoday <- get_playlist_tracks(
  78.       playlist_id="37i9dQZF1DXcBWIGoYBM5M",
  79.       authorization=access_token()
  80.     )
  81.    
  82.     unique(
  83.       unlist(
  84.         lapply(toptoday$track.artists, function(a) { as.data.frame(a)$name })
  85.       )
  86.     )
  87.   })
  88.  
  89.   output$selecta <-renderUI({
  90.     selectInput(
  91.       inputId="a",
  92.       label="Artista A:",
  93.       choices=artists(),
  94.       selected="The XX"
  95.     )
  96.   })
  97.  
  98.   output$selectb <- renderUI({
  99.     selectInput(
  100.       inputId="b",
  101.       label="Artista B:",
  102.       choices=artists(),
  103.       selected="Buena Vista Social Club"
  104.     )
  105.   })
  106.  
  107.   a <- reactive({
  108.     get_artist_audio_features(
  109.       artist=input$a,
  110.       authorization=access_token()
  111.     )
  112.   })
  113.  
  114.   b <- reactive({
  115.     get_artist_audio_features(
  116.       artist=input$b,
  117.       authorization=access_token()
  118.     )
  119.   })
  120.  
  121.   x <- reactive({
  122.     input$x
  123.   })
  124.  
  125.   y <- reactive({
  126.     input$y
  127.   })
  128.  
  129.   output$plot <- renderPlot({
  130.     plot <- ggplot() +
  131.       geom_point(a(),mapping=aes_string(x=x(),y=y()),color="blue") +
  132.       geom_smooth(a(),mapping=aes_string(x=x(),y=y()),color="blue") +
  133.       geom_point(b(),mapping=aes_string(x=x(),y=y()),color="red") +
  134.       geom_smooth(b(),mapping=aes_string(x=x(),y=y()),color="red")
  135.     plot
  136.   })
  137. }
  138.  
  139. shinyApp(ui,server)
  140.  
  141. # Acousticness/Instrumentalness
  142. # Danceability/Valence -> Lyrics
  143.  
  144. # Structural Programming
  145. # Procedural Programming
  146.  
  147. #temp = []
  148. #forEach(toptoday, s => {
  149. #  if (s.track.name == "Motley Crew") {
  150. #    temp.push(s);
  151. #  }
  152. #})
  153.  
  154. # Declarative Programming
  155. # Reactive Programming
  156. # Functional Programming
  157.  
  158. # Pipeline
  159. # Tuberías
  160.  
  161. #toptoday %>%
  162. #  filter(track.name == "Motley Crew") %>%
  163. #  select(track.name) %>%
  164. #  paste0(" - Canción")
  165.  
  166. #toptoday %>%
  167. #  select(track.artists) %>%
  168. #  select(name) %>%
  169. #  unlist()
  170.  
  171. #artists <- lapply(toptoday$track.artists, function(a) { as.data.frame(a)$name })
  172. #artists <- unlist(artists)
  173.  
  174. #playlists <- get_featured_playlists()
  175. #get_categories()
  176. #get_category_playlists(category_id = "toplists")
  177. #toptoday <- get_playlist_tracks(playlist_id="37i9dQZF1DXcBWIGoYBM5M")
  178. #topglobal <- get_playlist_tracks(playlist_id="37i9dQZEVXbMDoHDwVN2tF")
  179. #viralglobal <- get_playlist_tracks(playlist_id="37i9dQZEVXbLiRSasKsNU9")
  180.  
  181. access_token <- get_spotify_access_token(
  182.   client_id=Sys.getenv("SPOTIFY_CLIENT_ID"),
  183.   client_secret=Sys.getenv("SPOTIFY_CLIENT_SECRET")
  184. )
  185.  
  186. artists <- c("BTS","The Killers","Post Malone","The XX","Justin Bieber")
  187.  
  188. audio_features <- lapply(
  189.   artists,
  190.   function(a) {
  191.     get_artist_audio_features(
  192.       artist=a,
  193.       authorization=access_token
  194.     )
  195.   }
  196. ) %>%
  197. bind_rows()
  198.  
  199. ggplot(audio_features, aes(valence,danceability,color=artist_name)) +
  200.   geom_point() +
  201.   geom_text_repel(
  202.     audio_features %>%
  203.       filter(valence > 0.95 & danceability > 0.95),
  204.     mapping=aes(x=valence,y=danceability,label=track_name)
  205.   ) +
  206.   geom_smooth()
  207.  
  208. #
  209. # killers <- get_artist_audio_features(
  210. #   artist="The Killers",
  211. #   authorization=access_token
  212. # )
  213. #
  214. # willie <- get_artist_audio_features(
  215. #   artist="Willie Colón",
  216. #   authorization=access_token
  217. # )
  218. #
  219. # ggplot() +
  220. #   geom_point(willie,mapping=aes(x=valence,y=key_name),color="red") +
  221. #   geom_point(killers,mapping=aes(x=valence,y=key_name),color="blue")
  222.  
Add Comment
Please, Sign In to add comment