This post will show you how to connect to the Spotify API using R and the spotifyR package. This API enables you to extract data for particular artists or songs from the Spotify database.
The first step is registering in the Spotify Developer App: https://developer.spotify.com/. Once you are registered, you should create a new app. In that new window you only need to give the app a name, and app description and set some redirect URIs. All the other entries you can leave them empty. For example:
Example of fields entered in the spotify dashboard.
Then you, just need to open R, load the libraries we are going to use.
and then copy the client id and client secret into R and get the access_token.
Then you need to read the artist and album database or any other data frame. In this case, I am going to use a small example with some albums I like.
Then we are going to use mutate to extract the genre associated with each artist. Since, not all artists have an associated genre (or can be found in spotify), it is advisable to use possibly to avoid errors if no genre was find in the Spotify database. Then use this function and finally, do some data wrangling to obtain each genre associated with an artist in a single row.
Finally, you might want to summarise this information into a plot of the most common genres in your df. So we are going to use a circular plot to show this.