December 6, 2017

Introducing geniusr

I made a new R package (🔥📦🔥) this week. My sophomore post on this site (from way back in March) was a text-based analysis of Kanye West records, which exposed me to the Genius API1 once I wanted to get hold of some lyrical content. In the process, I wrote some functions for playing nicely with this API in R that I knew would probably be handy for others dabbling/wanting to dabble in similar matters.

Since then, I’ve found a window of time to build on and consolidate this work into a well-rounded package. geniusr is now available on CRAN. This is the tour.2

Searchin’

The Genius API’s search capabilities allows for users to scan all hosted content on the site (i.e. all songs). A couple of quick implementations exist in the package for retrieving artist (search_artist()) and song (search_song()) matches for search terms. Let’s start by looking for songs brought back by a simple search for “Versace”.

versace_search <- search_song(search_term = "Versace") %>% glimpse
## Observations: 10
## Variables: 5
## $ song_id         <int> 176872, 2898687, 182654, 176951, 183346, 20050...
## $ song_name       <chr> "Versace (Remix) (Ft. Drake)", "Versace on the...
## $ song_lyrics_url <chr> "https://genius.com/Migos-versace-remix-lyrics...
## $ artist_id       <int> 44080, 500, 1319, 44080, 554, 32397, 2236, 358...
## $ artist_name     <chr> "Migos", "Bruno Mars", "Meek Mill", "Migos", "...

Goin’ Meta

Wanna bring back some fuller information about a particular song (get_song_meta()), artist (get_artist_meta()) or album (get_album_meta())? Well, now you can. Let’s test one out - how about investigating a classic Versace-themed track from our last search, ‘Versace’ by Migos.

versace_meta <- get_song_meta(song_id = versace_search$song_id[4]) %>% glimpse
## Observations: 1
## Variables: 13
## $ song_id            <int> 176951
## $ song_name          <chr> "Versace"
## $ song_lyrics_url    <chr> "https://genius.com/Migos-versace-lyrics"
## $ song_art_image_url <chr> "https://images.genius.com/4fe960be500117d6...
## $ release_date       <chr> "2013-07-09"
## $ pageviews          <int> 118683
## $ annotation_count   <int> 24
## $ artist_id          <int> 44080
## $ artist_name        <chr> "Migos"
## $ artist_url         <chr> "https://genius.com/artists/Migos"
## $ album_id           <int> 36011
## $ album_name         <chr> "Young Rich Niggas"
## $ album_url          <chr> "https://genius.com/albums/Migos/Young-rich...

With this metadata, we can easily pull up the song’s artwork, for example. Why not.

artwork <- versace_meta$song_art_image_url

knitr::include_graphics(path = artwork)

Gettin’ Lyrical

The package allows you to grab lyrics for tracks real quick, using the scrape_lyrics_id() (or scrape_lyrics_url()) function.3 While we’re at it, let’s call on tidytext and answer a question for the people - just how much were the Migos chatting about “Versace” in ‘Versace’?

# get words proportion representation
versace_prop <- scrape_lyrics_url(versace_meta$song_lyrics_url) %>%
  unnest_tokens(word, line) %>%
  count(word) %>%
  mutate(proportion = n / sum(n)) %>%
  # was the word versace or the rest
  mutate(versace_or_the_rest=if_else(word == "versace", "Versace", "Not Versace")) %>%
  group_by(versace_or_the_rest) %>%
  summarise(proportion=sum(proportion)*100)

# waffle plot
parts <- c("Not Versace" = versace_prop$proportion[[1]],
           "Versace" = versace_prop$proportion[[2]])

waffle(parts, rows = 8, size = 1, colors = c("#3C3A32", "#E3BD08"), use_glyph = "volume-up", 
       glyph_size = 10, title = "How much Versace is in Migos - 'Versace'?") +
  labs(caption="@ewen_") +
  theme_void(base_family = "Work Sans", base_size = 12) +
  theme(legend.position = "bottom")

The word “Versace” represents 32.5% of the lyrics in Migos - Versace. Phew.

That’s all for now, enjoy the package and keep up with future developments over at the GitHub repo.


  1. Genius’ documentation pages are helpful if you want to know more about the API.

  2. For installation & authentication guidance, head for the README.

  3. As well as API helper functions, the package includes things that are scraping the Genius site (Genius’ API doesn’t include lyrics, for example). Be sure to read the terms of service in case of any concerns.