All the packages we need are:

library(spotifyr)
library(tidyverse)
library(knitr)

Before the start

Let R knows who you are! Just like the offical cilent, you should login before start working. However, this works will take some minutes more than login to Spotify cilent as usual, but don’t worry, I can guarantee it is easy case.

You should apply CLIENT_ID and CLIENT_SECRET from HERE. We don’t suppose to make any commercial activity right? The non-commercial application will be easier. After that, you may can get CLIENT_ID and CLIENT_SECRET. As easy as walking in a park, isn’t it?. Then, analysis works start!

Let’s set authorize staffs

All the authorize staffs contain two main parameter, as I said CLIENT_ID and CLIENT_SECRET, in spotifyr package, they are SPOTIFY_CLIENT_ID and SPOTIFY_CLIENT_SECRET. There are many wasy to set these two parameters, but here, I highly recommand you to save these two parameters to a .R file, and then source this file in other script files. Here, I name this file as setToekn.R, and the content looks like:

Once, we should set these two parameters, just run this code: source('Scripts/setToken.R')(Here, Scripts is the directory where I save my scripts).

So let’s try to authorize our R to access spotify!

But I guess something wrong happened to you, some error like invalid CLIENT_ID here! What happend? Let’s check how Charlie86, the author of spotifyr, said:

For certain functions and applications, you’ll need to log in as a Spotify user. To do this, your Spotify Developer application needs to have a callback url. You can set this to whatever you want that will work with your application, but a good default option is http://localhost:1410/ (see image below). For more information on authorization, visit the offical Spotify Developer Guide.

The little issue has been solved, time to get all my loved tracks!

What’s my favorite?

There is no doubt, the recommendation algorithm is the most valuable staff for Spotify, and this is why we can’t leave this platform, even the Apple Music offer a more competitive price (in China, less than 1 dollar per month for student, where Spotify doesn’t provide service). So how can we found all ours favorite tracks and albums?

Easy thing! Just run get_my_saved_tracks, till current moment, this function is not available yet. But I have pull a request to spotifyr in Github. Before a merge happen install spotifyr from my own repo by devtools::install_github('womeimingzi11/spotifyr') will work. Let’s keep moving. Spotify sets a limit, you can get up to 50 items per request, and the default value is 20, not only for loved tracks, this is also exist almost every functions in spotfiyr. How to get more? offset, as the name says, set offset to jump items and get more. Here is the code.

## Observations: 1,353
## Variables: 30
## $ added_at                           <chr> "2019-10-31T03:42:08Z", "201…
## $ track.artists                      <list> [<data.frame[1 x 6]>, <data…
## $ track.available_markets            <list> [<"AD", "AE", "AR", "AT", "…
## $ track.disc_number                  <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
## $ track.duration_ms                  <int> 158899, 214240, 219200, 2392…
## $ track.explicit                     <lgl> FALSE, FALSE, FALSE, FALSE, …
## $ track.href                         <chr> "https://api.spotify.com/v1/…
## $ track.id                           <chr> "0mjAU3yKR1QnXnHtjGJqTM", "6…
## $ track.is_local                     <lgl> FALSE, FALSE, FALSE, FALSE, …
## $ track.name                         <chr> "Rescue Me", "Baby", "Shake …
## $ track.popularity                   <int> 88, 77, 67, 57, 94, 64, 70, …
## $ track.preview_url                  <chr> "https://p.scdn.co/mp3-previ…
## $ track.track_number                 <int> 1, 1, 12, 1, 1, 1, 3, 1, 5, …
## $ track.type                         <chr> "track", "track", "track", "…
## $ track.uri                          <chr> "spotify:track:0mjAU3yKR1QnX…
## $ track.album.album_type             <chr> "single", "album", "album", …
## $ track.album.artists                <list> [<data.frame[1 x 6]>, <data…
## $ track.album.available_markets      <list> [<"AD", "AE", "AR", "AT", "…
## $ track.album.href                   <chr> "https://api.spotify.com/v1/…
## $ track.album.id                     <chr> "57BhVBJbVPfIbwLANO5fSe", "3…
## $ track.album.images                 <list> [<data.frame[3 x 3]>, <data…
## $ track.album.name                   <chr> "Rescue Me", "My World 2.0",…
## $ track.album.release_date           <chr> "2019-05-17", "2010-01-01", …
## $ track.album.release_date_precision <chr> "day", "day", "day", "day", …
## $ track.album.total_tracks           <int> 1, 10, 26, 1, 1, 1, 11, 1, 1…
## $ track.album.type                   <chr> "album", "album", "album", "…
## $ track.album.uri                    <chr> "spotify:album:57BhVBJbVPfIb…
## $ track.album.external_urls.spotify  <chr> "https://open.spotify.com/al…
## $ track.external_ids.isrc            <chr> "USUM71907507", "USUM7091926…
## $ track.external_urls.spotify        <chr> "https://open.spotify.com/tr…

Which music is my first love?

Before further exploration, here is a question: What’s your first loved song? I bet you guy can not remember that! Luckily, Spotify remembers every thing! First, we use the lubridate package to handle the date, it can convert the date in character class into date class, and the most important thing is, lubridate is really easy to use. Here, the spotify record the date we loved a song with the format as Year-Mon-Day H-M-S, and lubridate exactly provide a functin ymd_hms to convert the date. For more help, try ?lubridate anytime.

track.name added_at
Eyes Open 2016-12-01 06:15:37

Okay, let’s back to our journey. First, let’s found out which artist is my favorite singer. I guess, it’s Taylor Swift! Once I am a freshman in University, I started to enjoy her music, and one decade almost past. No matterhow, let’s verify my hypothesis. First, Let’s get all the artist of my loved songs! Remember, the track.artists is stored as list, it’s easy to understand! Not every song is made by only one artist, is it? So we need to extract from lists in lists. Twice reduce operations are necessary here.

After above operation, I extracted all the artists from all my loved songs. Time to count their appear times! Although, the names of artists are well-know, but we select the id as the mark of artist in this case. WHY? To avoid dulicated name. Then, we add n, as tracks number column, back to artist data.frame, and omit the duplicated records. After that, we remove id column, because it’s really meanless for human eyes, at least, in my opinion.

name n
OneRepublic 18
Taylor Swift 166
Camila Cabello 25
Sia 15
Charlie Puth 22
S.H.E 52
The Chainsmokers 32
Ed Sheeran 29
Kesha 34
Maroon 5 55
Avril Lavigne 60
James Blunt 21
Daya 15
Kelly Clarkson 16
Kygo 16
Ellie Goulding 46
The Weeknd 29
Britney Spears 36
Katy Perry 20
Shakira 23

Bingo! Taylor is definitely my favorite artist. Following are Avril and Maroon 5, which are exactly in line with my prediction! However, tables are weak, graphics are stronger! Let’s check how the column plot looks like! BTW, ggplot2 package has been loaded with tidyverse packages bundle, we don’t need to load (ggplot2) again.

There is no doubt that I can explore more, for instance, we can consider how my taste changes along the time gradient. But in case, I consider every thing in the global dimension.

Do you agree with what you listen reveals how your feel? Spoify has defined an index: valence. It is considered as a measure of musical positivity. Here is the definition.

A measure from 0.0 to 1.0 describing the musical positiveness conveyed by a track. Tracks with high valence sound more positive (e.g. happy, cheerful, euphoric), while tracks with low valence sound more negative (e.g. sad, depressed, angry).

It looks cool, but if it is really meaningful for everyone, at least for me? I will find it out. What about my first 20 favorite artist? Although it’s ONLY 20 artist (actully it’s 22, becasue some artist has the same number of musics I loved), it still cost minutes to request data from server of Spotify. I highly recommend you to save the object as RDS file. And don’t request data everytime! In this plot, the emotional quadrants are consistent with Sentify.