Spotify API

The API that I chose to learn is Spotify. I chose to use Spotify because I am a big fan of music and really like Spotify’s data collection and year in review for your profile. There are many reasons why someone would want to use the Spotify API. For example, you could use it to create your own end of the year in review by analyzing your own profiles data.

How to set up the API

In order to get access to the API you need a Spotify account. Once you have created an account you have to go on the Spotify developer website. On the developer website you must create in app. By creating an app you will get your credentials that you need to use the API. Once you have your credentials you will need to install the spotifyr package and load it in. Then you will use the 2 sets of code below to set your systems values to your personalized keys. This way when you load R you won’t have to manual type these in and they will be set on your computer. These are located on your dashboard and can be copy and pasted into R.

# Sys.setenv(SPOTIFY_CLIENT_ID = "xxxxxxxxxxx")
# Sys.setenv(SPOTIFY_CLIENT_SECRET = "xxxxxxxxxxx")

After setting the keys on your computer you will now have to set your access token as a value in your global environment. To do this use the code listed below

access_token <- get_spotify_access_token()

Demonstration of the Spotify API and spotifyr package

There are many different things you can do with this API and the spotifyr package. One thing you can do is use get_artist_audio_features to get all the audio features for every song by an artists. The artist I chose is Drake. With this command you can create a data frame that shows you information on how danceability a song or how loud it is. There are 39 different variables for each song in the data frame. Other variables include tempo, song release date, and acousticness.

drake <- get_artist_audio_features("Drake")

Drake albums ranked by danceability

Now let’s use the drake data frame that we just created to analyze the danceability of Drake’s albums. To do this I am going to filter the data frame for only my top 5 favorite albums, group by the album, and then create a bar graph to visually display this information.

drake %>% 
  filter(album_name == "Take Care" | album_name == "Nothing Was The Same" | 
           album_name == "Certified Lover Boy" | album_name == "Scorpion" | 
           album_name == "Views") %>% 
  group_by(album_name) %>% 
  summarize(`Average Danceability` = mean(danceability)) %>% 
  ggplot(aes(x = album_name, y = `Average Danceability`)) +
  geom_bar(stat = "identity") +
  xlab("Album Name") +
  ylab("Average Danceability Rating") +
  ggtitle("Average Danceability of My Top 5 Drake Albums")

I chose to only analyze my top 5 favorite Drake albums. My all time favorite Drake album is Take Care. I really like music that is danceable, but my favorite album has the lowest average danceability score. The Scorpion album has the highest average danceability rating. This does not surprise me because when this album released there were dance trends that went viral for songs from this album.