jorandradefig icon

hallazgosmusicales-2.R

jorandradefig | PRO | 07/11/21 06:35:12 PM UTC (Edited) | 0 ⭐ | 728 👁️ | Never ⏰ | []
R |

4.81 KB

|

None

|

0 👍

/

0 👎

#########
# Date: 4/6/21
# Author: JA
#########
 
pacman::p_load(
  #dataframe
  tidyverse,
  ggrepel,
  #viz
  plotly,
  #webapps
  shiny,
  #api
  spotifyr
)
 
 
# Application
# Platform
 
# API - Application Public Interface
# REST - Resource State Transfer
# JSON - JavaScript Object Notation
 
# Authentication
# Authorization
 
readRenviron(".env")
 
# Dashboard
# Tablero
 
# (Graphical) User Interface
ui <- pageWithSidebar(
  titlePanel("Hallazgos Musicales"),
  sidebarPanel(
    htmlOutput(
      outputId="selecta"
    ),
    htmlOutput(
      outputId="selectb"
    ),
    selectInput(
      inputId="x",
      label="X:",
      choices=c("acousticness","danceability","duration_ms","energy","instrumentalness","key","key_name","key_mode","mode","mode_name","liveness","loudness","speechiness","tempo","time_signature","valence"),
      selected="valence"
    ),
    selectInput(
      inputId="y",
      label="Y:",
      choices=c("acousticness","danceability","duration_ms","energy","instrumentalness","key","key_name","key_mode","mode","mode_name","liveness","loudness","speechiness","tempo","time_signature","valence"),
      selected="key_name"
    ),
  ),
  mainPanel(
    plotOutput(
      outputId="plot"
    )
  )
)
 
# Server
# Reactivity
 
server <- function(input,output) {
  
  access_token <- reactive({
    get_spotify_access_token(
      client_id=Sys.getenv("SPOTIFY_CLIENT_ID"),
      client_secret=Sys.getenv("SPOTIFY_CLIENT_SECRET")
    )
  })
  
  artists <- reactive({
    toptoday <- get_playlist_tracks(
      playlist_id="37i9dQZF1DXcBWIGoYBM5M",
      authorization=access_token()
    )
    
    unique(
      unlist(
        lapply(toptoday$track.artists, function(a) { as.data.frame(a)$name })
      )
    )
  })
  
  output$selecta <-renderUI({
    selectInput(
      inputId="a",
      label="Artista A:",
      choices=artists(),
      selected="The XX"
    )
  })
  
  output$selectb <- renderUI({
    selectInput(
      inputId="b",
      label="Artista B:",
      choices=artists(),
      selected="Buena Vista Social Club"
    )
  })
  
  a <- reactive({
    get_artist_audio_features(
      artist=input$a,
      authorization=access_token()
    )
  })
  
  b <- reactive({
    get_artist_audio_features(
      artist=input$b,
      authorization=access_token()
    )
  })
  
  x <- reactive({
    input$x
  })
  
  y <- reactive({
    input$y
  })
  
  output$plot <- renderPlot({
    plot <- ggplot() +
      geom_point(a(),mapping=aes_string(x=x(),y=y()),color="blue") +
      geom_smooth(a(),mapping=aes_string(x=x(),y=y()),color="blue") +
      geom_point(b(),mapping=aes_string(x=x(),y=y()),color="red") +
      geom_smooth(b(),mapping=aes_string(x=x(),y=y()),color="red")
    plot
  })
}
 
shinyApp(ui,server)
 
# Acousticness/Instrumentalness
# Danceability/Valence -> Lyrics
 
# Structural Programming
# Procedural Programming
 
#temp = []
#forEach(toptoday, s => {
#  if (s.track.name == "Motley Crew") {
#    temp.push(s);
#  }
#})
 
# Declarative Programming
# Reactive Programming
# Functional Programming
 
# Pipeline
# Tuberías
 
#toptoday %>%
#  filter(track.name == "Motley Crew") %>%
#  select(track.name) %>%
#  paste0(" - Canción")
 
#toptoday %>%
#  select(track.artists) %>%
#  select(name) %>%
#  unlist()
 
#artists <- lapply(toptoday$track.artists, function(a) { as.data.frame(a)$name })
#artists <- unlist(artists)
 
#playlists <- get_featured_playlists()
#get_categories()
#get_category_playlists(category_id = "toplists")
#toptoday <- get_playlist_tracks(playlist_id="37i9dQZF1DXcBWIGoYBM5M")
#topglobal <- get_playlist_tracks(playlist_id="37i9dQZEVXbMDoHDwVN2tF")
#viralglobal <- get_playlist_tracks(playlist_id="37i9dQZEVXbLiRSasKsNU9")
 
access_token <- get_spotify_access_token(
  client_id=Sys.getenv("SPOTIFY_CLIENT_ID"),
  client_secret=Sys.getenv("SPOTIFY_CLIENT_SECRET")
)
 
artists <- c("BTS","The Killers","Post Malone","The XX","Justin Bieber")
 
audio_features <- lapply(
  artists,
  function(a) {
    get_artist_audio_features(
      artist=a,
      authorization=access_token
    )
  }
) %>%
bind_rows()
 
ggplot(audio_features, aes(valence,danceability,color=artist_name)) +
  geom_point() +
  geom_text_repel(
    audio_features %>%
      filter(valence > 0.95 & danceability > 0.95),
    mapping=aes(x=valence,y=danceability,label=track_name)
  ) +
  geom_smooth()
 
# 
# killers <- get_artist_audio_features(
#   artist="The Killers",
#   authorization=access_token
# )
#
# willie <- get_artist_audio_features(
#   artist="Willie Colón",
#   authorization=access_token
# )
# 
# ggplot() +
#   geom_point(willie,mapping=aes(x=valence,y=key_name),color="red") +
#   geom_point(killers,mapping=aes(x=valence,y=key_name),color="blue")
 

Comments