library(spotifyr)
library(tidyverse)Spotify API Rstudio Tutorial
Starting Your Spotify API
To get started you can go to https://developer.spotify.com/ here you will see a login button at the top right.
Using this to login to your Spotify account (if you have one) and then Spotify has its own tutorial for creating an app and using its API (https://developer.spotify.com/documentation/web-api/tutorials/getting-started#create-an-app).
Once you have created your app and gotten your client_id and client_secret, load the Spotifyr package. The tidyverse package to make things tidy. Install the package as needed.
After the package is loaded you can run this to get your token which will work for 1 hour and you can access Spotify’s API. (copy and past your client information between the “” and don’t forget to get rid of the # once it is in your own Rstudio)
# client_id <- "your_client_id"
# client_secret <- "your_client_secret"
# token <- get_spotify_access_token(client_id, client_secret)Data Library
Now that you have your access to Spotify’s API you can start requesting data Using the Spotify package in R we can easily use their API. In order to find artists you have to have their IDs. Luckily this is easy to find.
Once data is extracted into a Data frame we see some variables:
genres: A list of the genres the artist is associated with. If not yet classified, the array is empty.
href: A link to the Web API endpoint providing full details of the artist.
id: The Spotify ID for the artist.
images: Contains date frames that contain the URL of an artist image, the height of the image, and the width of the image.
name: The name of the artist.
popularity: The popularity of the artist. The value will be between 0 and 100, with 100 being the most popular. The artist’s popularity is calculated from the popularity of all the artist’s tracks.
type: String type showing what the object is (artist, album, track, etc.).
uri: The Spotify URI for the artist.
external_urls.spotify: The Spotify URL for the object.
followers.href: This will always be set to null, as the Web API does not support it at the moment.
followers.total: The total number of followers.
Getting IDs
- Open Spotify
- Search an artist
- click the three dots next
- hover over the share option, and select embed artist
- you can then click show code and the artist ID will be after artist/{id} (unfortunately you cannot just copy the ID but you must copy the entire code so you can just do that and then delete everything but the ID).
In order to find the artists using R you need to have a character vector of the IDs Here I have created a data frame with 3 artists to look at.
artist_ids <- c("2wOqMjp9TyABvtHdOSOTUS","4NJhFmfw43RLBLjQvxDuRS","0YC192cP3KPCRWx8zr8MfZ")
goated_artists <- get_artists(artist_ids,token)Cleaning the Data Frame
You can easily make a data frame of 50 artists (that is the max that the get_artists() allows) and repeat that to get as many artists as you would like by combing data frames. Using the data frame I have created you can select some of the useful information we can use in R: Genres, ID, Name, Popularity, and Followers
goated_artists <-
goated_artists %>%
select(id, name, genres, popularity, followers.total)A Quick Analysis
Now that we have a clean data frame we can run analysis.
goated_artists %>%
ggplot(aes(x = name, y = popularity))+
geom_col()+
scale_y_continuous(limits = c(0,100))+
labs(title = "Artist Popularity",
x = "Artist Name",
y = "Popularity out of 100")From this chart we can see that Hans Zimmer is the most popular of these 3 artists. I would agree with this as his music is fire. Give it a listen if you haven’t before!