Spotifyr is an R package designed to streamline the retrieval of data from Spotify’s Web API. A few of its features include enabling users to input an artist’s name and quickly access their albums, songs, records, etc. Additionally, users can extract data like recently or top played songs from their individual accounts.
Setting up the API in R
Installing Required Packages
You first need to load the spotifyr and devtools packages using the code below to get started.
library(devtools)
Loading required package: usethis
devtools::install_github('charlie86/spotifyr')
Skipping install of 'spotifyr' from a github remote, the SHA1 (45ffa4ee) has not changed since last install.
Use `force = TRUE` to force installation
library(spotifyr)
Accessing/Pulling Personal Spotify Information
Should you want to analyze some of your personal Spotify account stats/trends/etc., you will need to setup a Spotify Developer Account.
Next, you will want to login to your Spotify account (or create one if you do not already have one). Then, go to your dashboard and fill out the necessary criteria to create a new app: app name, app description, and a redirect url.
Once you create a new app, you will have access to your personal Client ID and Client Secret by clicking on the new app you just created and then going into settings. These credentials will allow you to request and refer to personal information from your Spotify account.
API Call
However, I will focus on using generic Spotify information that is available to all users. You have the ability to search for a wide range of Spotify data that interests you, which does not have to relate to your personal account. Below, I included an example of ways you might want to use the data, or interesting things you can pull.
Example:
# How many songs are in each Matchbox Twenty album?library(knitr)library(dplyr)
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
`summarise()` has grouped output by 'album_release_year'. You can override
using the `.groups` argument.
# A tibble: 7 × 3
# Groups: album_release_year [5]
album_release_year album_name num_songs
<dbl> <chr> <int>
1 1996 Yourself or Someone Like You 12
2 2002 More Than You Think You Are 12
3 2012 North 12
4 2023 Where The Light Goes 12
5 2000 Mad Season 13
6 2012 North (Deluxe Edition) 14
7 2000 Mad Season (Deluxe Edition) 17
#kable()
The code above asks the question: How many songs are in each Matchbox Twenty album?
From the results, we see that the group has released 5 albums (7 if you count the deluxe editions as separate) in the years: 1996, 2000, 2002, 2012, and 2023. The albums Mad Season and North have Deluxe Editions.