jorandradefig

hallazgosmusicales.R

Jul 4th, 2021
1,044
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #########
  2. # Date: 4/6/21
  3. # Author: JA
  4. #########
  5.  
  6. pacman::p_load(
  7.   #dataframe
  8.   tidyverse,
  9.   #viz
  10.   plotly,
  11.   #webapps
  12.   shiny,
  13.   #api
  14.   spotifyr
  15. )
  16.  
  17. # Authentication
  18. # Authorization
  19.  
  20. readRenviron(".env")
  21.  
  22. # Dashboard
  23. # Tablero
  24.  
  25. # (Graphical) User Interface
  26. ui <- pageWithSidebar(
  27.   titlePanel("Hallazgos Musicales"),
  28.   sidebarPanel(
  29.     selectInput(
  30.       inputId="a",
  31.       label="Artista A:",
  32.       choices=c("The XX","Billie Eilish","Adele","The Killers"),
  33.       selected="The XX"
  34.     ),
  35.     selectInput(
  36.       inputId="b",
  37.       label="Artista B:",
  38.       choices=c("Willie Colón","Bad Bunny","J Balvin","Buena Vista Social Club"),
  39.       selected="Buena Vista Social Club"
  40.     ),
  41.     selectInput(
  42.       inputId="x",
  43.       label="X:",
  44.       choices=c("valence","danceability","instrumentalness","key_name"),
  45.       selected="valence"
  46.     ),
  47.     selectInput(
  48.       inputId="y",
  49.       label="Y:",
  50.       choices=c("valence","danceability","instrumentalness","key_name"),
  51.       selected="key_name"
  52.     ),
  53.   ),
  54.   mainPanel(
  55.     plotlyOutput(
  56.       outputId="plot"
  57.     )
  58.   )
  59. )
  60.  
  61. # Server
  62. # Reactivity
  63.  
  64. server <- function(input,output) {
  65.  
  66.   access_token <- reactive({
  67.     get_spotify_access_token(
  68.       client_id=Sys.getenv("SPOTIFY_CLIENT_ID"),
  69.       client_secret=Sys.getenv("SPOTIFY_CLIENT_SECRET")
  70.     )
  71.   })
  72.  
  73.   a <- reactive({
  74.     get_artist_audio_features(
  75.       artist=input$a,
  76.       authorization=access_token()
  77.     )
  78.   })
  79.  
  80.   b <- reactive({
  81.     get_artist_audio_features(
  82.       artist=input$b,
  83.       authorization=access_token()
  84.     )
  85.   })
  86.  
  87.   x <- reactive({
  88.     input$x
  89.   })
  90.  
  91.   y <- reactive({
  92.     input$y
  93.   })
  94.  
  95.   output$plot <- renderPlotly({
  96.     plot <- ggplot() +
  97.       geom_point(a(),mapping=aes_string(x=x(),y=y()),color="blue") +
  98.       geom_point(b(),mapping=aes_string(x=x(),y=y()),color="red")
  99.     ggplotly(plot)
  100.   })
  101. }
  102.  
  103. shinyApp(ui,server)
  104.  
  105. # killers <- get_artist_audio_features(
  106. #   artist="The Killers",
  107. #   authorization=access_token
  108. # )
  109. #
  110. # willie <- get_artist_audio_features(
  111. #   artist="Willie Colón",
  112. #   authorization=access_token
  113. # )
  114. #
  115. # ggplot() +
  116. #   geom_point(willie,mapping=aes(x=valence,y=key_name),color="red") +
  117. #   geom_point(killers,mapping=aes(x=valence,y=key_name),color="blue")
  118.  
RAW Paste Data