Spotifyr is an R wrapper for pulling track audio features and other information from Spotify’s Web API. You can retrieve an artist’s entire discography just by entering in their name! Spotifyr also allows you pull song and playlist information from active Spotify users.
I looked into my favorite band with the help of Spotifyr, The Rolling Stones. Now, I will walk you through the process of setting up the Spotifyr package and show you how to pull up The Rolling Stones’ favorite musical key and valence metrics.
First, you have to go to https://developer.spotify.com/dashboard/login to set up a dev account for Spotify in order to access its Web API. After setting up a developer account, you will be given a Client ID and Client Secret. The ID and Secret are then used to pull your access token into R with the function below:
get_spotify_access_token()
To properly set this all up in R, you have to set your new credentials to the SPOTIFY_CLIENT_ID and SPOTIFY_CLIENT_SECRET system environment variables. These serve as the default arguments for the get_spotify_access_token() and all of the other functions in the Spotifyr package.
Now for the set up in R, you have to install the devtools package first. This is followed by installing the development version of github devtools::install_github(‘charlie86/spotifyr’). After this step, you can load in Spotifyr and proceed to load in your ID and Secret for your access token.
The setup in R will look like this:
Sys.setenv(SPOTIFY_CLIENT_ID = 'xxxxxxxxxxxxxxxxxxxxxxxxxx')
Sys.setenv(SPOTIFY_CLIENT_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxx')
access_token <- get_spotify_access_token()
Now you are ready to begin using the Spotifyr functions to learn more about your favorite artists!!
The get_artist_audio_features() is a popular call for the Spotify API. This function allows you to pull up a dataframe of your favorite band’s audio features and other information by inserting their name within the parentheses.
After inputting ‘the rolling stones’ into the get_artist_audio_features() function, we can utilize the keymode variable to identify the favorite key of The Rolling Stones.
Here is what the R setup would look like:
rollingstones <- get_artist_audio_features('the rolling stones')
rollingstones %>%
count(key_mode, sort = TRUE) %>%
head(5) %>%
kable()
| key_mode | n |
|---|---|
| C major | 112 |
| A major | 88 |
| G major | 85 |
| D major | 82 |
| E major | 62 |
The chart indicates that the favorite key of the Rolling Stones was C Major!!
The valence of a song,in Spotify, is measured on a 0.0 to 1.0 scale capturing how positive the song is. Since you already have The Rolling Stones’ dataframe after pulling it up with the get_artist_audio_features() function earlier, you can now use the valence variable to figure out which song is considered to the happiest song of The Rolling Stones.
The setup in R will look like this:
rollingstones %>%
arrange(-valence) %>%
select(track_name, valence) %>%
head(5) %>%
kable()
| track_name | valence |
|---|---|
| Oh Baby (We Got A Good Thing Goin’) - Remastered 2002 | 0.971 |
| Susie Q | 0.970 |
| Not Fade Away - Live / Remastered 2009 | 0.969 |
| Not Fade Away - Live In Lisbon, Portugal / 1995 | 0.969 |
| Carol | 0.969 |
The 2002 remastered song title, "Oh Baby (We Got A Good Thing Goin’) is The Rolling Stones’ happiest song with a valence of 0.971!!!
I hope you enjoyed this Spotifyr Introduction!