Now on CRAN: spotifyr 1.0.0

My first R package is now on CRAN! Here’s a quick overview of spotifyr and some example usage.

Overview

spotifyr is a quick and easy wrapper for pulling track audio features from Spotify’s Web API in bulk. By batching API requests, it allows you to enter an artist’s name and retrieve their entire discography in seconds, along with Spotify’s audio features and track/album popularity metrics. You can also pull song and playlist information for a given Spotify User (including yourself!).

Installation

Stable version 1.0.0 on CRAN

install.packages('spotifyr')

Development version

devtools::install_github('charlie86/spotifyr')

Authenication

You’ll have to set up a Dev account with Spotify to access their Web API here. This will give you your Client ID and Client Secret. Once you have those, you can pull your access token into R with get_spotify_access_token.

The easiest way to authenticate is to set your credentials to the System Environment variables SPOTIFY_CLIENT_ID and SPOTIFY_CLIENT_SECRET. The default arguments to get_spotify_access_token (and all other functions in this package) will refer to those. Alternatively, you can set them manually and make sure to explicitly refer to your access token in each subsequent function call.

Sys.setenv(SPOTIFY_CLIENT_ID = '[YOUR SPOTIFY CLIENT ID]')
Sys.setenv(SPOTIFY_CLIENT_SECRET = '[YOUR SPOTIFY CLIENT SECRET]')

access_token <- get_spotify_access_token()

Usage

What was The Beatles’ favorite key?

library(spotifyr)
beatles <- get_artist_audio_features('the beatles')

library(tidyverse)
library(knitr)

beatles %>%
    count(key_mode, sort = T) %>%
    rename(num_songs = n) %>%
    head(10) %>%
    kable
key_mode num_songs
C major 46
D major 41
G major 38
A major 36
E major 22
F major 19
A minor 10
B minor 10
E minor 10
C# minor 8

What’s the most danceable song on Barack Obama’s Spotify playlists?

obama <- get_user_audio_features('barackobama', show_progress = FALSE)

obama %>%
    arrange(-danceability) %>% 
    select(track_name, artist_name, album_name, playlist_name, danceability) %>% 
    head(10) %>%
    kable
track_name artist_name album_name playlist_name danceability
Treat ’Em Right Chubb Rock The One Michelle’s Workout Mix 0.932
Treat ’Em Right Chubb Rock 100 Disco Hits of The ’70s & ’80s (Re-Recorded Versions) 2012 Campaign Playlist 0.895
My Mic Nick Cannon Nick Cannon Official Inauguration Playlist 0.880
Rie y Llora Celia Cruz Mis Favoritas 2012 Campaign Playlist 0.823
Thank You Ledisi It’s Christmas Michelle’s Workout Mix 0.822
Love You I Do Jennifer Hudson Dreamgirls (Music from the Motion Picture) [Deluxe Edition] 2012 Campaign Playlist 0.822
Thank You Ledisi It’s Christmas 2012 Campaign Playlist 0.822
Respect Aretha Franklin Rhino Hi-Five: Various Artists: Mother’s Day Songs Supporter Picks 0.821
Something Special Usher Here I Stand Official Inauguration Playlist 0.818
Green Onions Booker T. & the M.G.’s Green Onions 2012 Campaign Playlist 0.816

What’s the most joyful Joy Division song?

My favorite audio feature has to be “valence,” a measure of musical positivity.

joy <- get_artist_audio_features('joy division')

joy %>% 
    arrange(-valence) %>% 
    select(track_name, valence) %>% 
    head(10) %>%
    kable
track_name valence
These Days 0.949
Passover - 2007 Remastered Version 0.941
Colony - 2007 Remastered Version 0.808
Atrocity Exhibition - 2007 Remastered Version 0.787
Wilderness 0.775
Twenty Four Hours 0.773
A Means To An End - 2007 Remastered Version 0.752
Interzone - 2007 Remastered Version 0.746
She’s Lost Control - 2007 Remastered Version 0.743
Disorder - 2007 Remastered Version 0.740

Now if only there was some way to plot joy…

Joyplot of the emotional rollercoasters that are Joy Division’s albums

library(ggjoy)

ggplot(joy, aes(x = valence, y = album_name)) + 
    geom_joy() + 
    theme_joy() +
    ggtitle("Joyplot of Joy Division's joy distributions", subtitle = paste0("Based on valence pulled from Spotify's Web API with spotifyr"))

Sentify: A Shiny app

This app, powered by spotifyr, allows you to visualize the energy and valence (musical positivity) of all of Spotify’s artists and playlists.

Related

comments powered by Disqus