With how much music has become apart of our daily lives, sometimes we can ask ourselfs deeper thoughs such as what somes give of a more energertic vibe or whihc songs just wann amake you move to the music. Well now you can as this turtorial will show you how to access Sportify data on all your favorate songs and artists on Spotify.
In order to access the Spotify API you must first go to developer.spotify.com, and log in to your spotify account or create a new one. Once logged in this will link your account to have developer access, then on your dashboard you should now have access to your personal client ID and client secret. Remember to save both of these values as they are required later on to access any of the spotify data.
R can now be opened and you will have to download and install the spotifyr package. Once this is done you must first authorize your API codes within R to proceed any farther. This can be done by using the following code:
Sys.setenv(SPOTIFY_CLIENT_ID = 'CLIENT ID HERE')
Sys.setenv(SPOTIFY_CLIENT_SECRET = 'CLIENT SECRET HERE')
access_token <- get_spotify_access_token()
Once this is complete you will then be able to start working with the API in R.
With the previous steps complete you now have the ability to work with the data. Such as looking into traits of an artist and all their songs and albums. While this may sound complicated its actually very simple by just using the code: get_artist_audio_features(INSERT ARTIST HERE). This will then show you a wide range of data stored in the API such as the length of the song to the energy and danceability. In total their are 39 different variables to choose from and work with.
Lets dive into using the danceability value for the artist AJR. As this is an artist I am quite fond of, I thought it would be interesting to see how their different albums compare in Spotifys ability to score their danceability. I will be using a bar group to display AJR’s five main albums and their danceability.
AJR <- get_artist_audio_features('ajr')
AJR %>%
group_by(album_name)%>%
filter(album_name %in% c('Living Room','The Click','Neotheater','OK ORCHESTRA'
,'The Click (Deluxe Edition)')) %>%
summarise(`Danceability bi Album` = mean(`danceability`)) %>%
ggplot(aes(x = album_name, `Danceability bi Album`))+ geom_col()+
labs(y = "Average Dancability by Album", x ="Album Name")+
labs(title = "AJR Albums by Average Danceability")+
coord_flip()
Now that’s a graph, I suspected with most of AJR’s songs being upbeat it can also be seen that the danceability of these albums is also quite high all around. If I was interested in looking even deeper into this artist I could see what type of Major the songs are in to predict which songs are happy or sad, and even look into the liveness of the songs.