INTRODUCTION

Past weekend, I decided to take on a small project. The aim was to play around with two packages that I found interesting but was yet to explore. These were the spotifyr and reactable. The objective was simple, create an interactive table containing my most played songs on Spotify.

SPOTIFYR

The spotifyr package is a wrapper to the Spotify API. Hence, the first step in the process was to create an APP on the Spotify Developer page. Creating an app provides a CLIENT ID, CLIENT SECRET and subsequently an ACCESS TOKEN. Fortunately, the spotifyr package provides a set of authentication functions which enable easy interaction with the Spotify API (see spotifyr::get_spotify_ access_token). The get_my_top_artists_or_tracks function extracts track or artist activity for a specified time range. In my case, I selected a short term time range (approximately 4 weeks) with maximum of 50 tracks. The function returns a dataframe containing artist,track and other details (nearly 29 variables). Some variables are lists with dataframes containing artist information,collaborators,album art etc. For this project, I was only interested in a handful of variables, namely, the album art, artist, album title, song tile and song duration. After a bit of cleaning, I landed on the five variables.

Extract_Artist_Names <- function(x){
  x <- as.data.frame(x)
  number_of_tracks <- 1:length(x[,5])
  Files<- list()
  for(i in seq_along(number_of_tracks)){
    Files[[i]] <- data.frame(artist_name = as.character(list(x[i,1][[1]][,3])))
  }
  Files <- lapply(Files,data.frame) %>% 
    bind_rows()
  Files <- cbind.data.frame(Files,x)
  return(Files)}

Top_Tracks <- Extract_Artist_Names(x=Top_Tracks)

extract_image <- function(x){ 
  number_of_time <- 1:length(x[,3])
  Files <- list()
  for(i in seq_along(number_of_time)){
    Files[[i]] <- data.frame(image = x[i,1][[1]][[2]][[2]])
  }
  Files <- lapply(Files,data.frame) %>% 
    bind_rows()
return(Files)}

Top_Tracks <- extract_image(Top_Tracks)

REACTABLE

The next step was to display an interactive table of my most played songs in recent weeks. Reactable and reactablefmtr packages provided the perfect set of functions to fulfill the objective. In addition, the reactable package is extensively documented with variety of examples and demonstrations. Ultimately, all I needed to do is adapt the theming example to create a custom theme for the table, adjust a few function calls to create the table below.

Sivu_theme <-  reactableTheme(
  color = "hsl(233, 9%, 87%)",
  backgroundColor = "#000000",
  borderColor = "#000000",
  stripedColor = "hsl(233, 12%, 22%)",
  highlightColor = "#00675A",
  inputStyle = list(backgroundColor = "hsl(233, 9%, 25%)"),
  selectStyle = list(backgroundColor = "hsl(233, 9%, 25%)"),
  pageButtonHoverStyle = list(backgroundColor = "hsl(233, 9%, 25%)"),
  pageButtonActiveStyle = list(backgroundColor = "hsl(233, 9%, 28%)")
)

Spotify_Chart <- reactable(Top_Tracks,
          columns =  list(
            image = colDef(name = "album art",
                                  cell = embed_img(height = "90",
                                                   width = "200"),
                           footer = "Source: Spotify"),
            artist_name = colDef(name = "artist",
                                 filterable = TRUE),
            name = colDef(name="song title",
                          filterable = TRUE),
            album.name = colDef(name = "album title",
                                filterable = TRUE),
            duration_ms = colDef(name="song length",
                                 footer = "milliseconds converted to minutes")),
          sortable = TRUE,
          searchable = TRUE,
          highlight = TRUE,bordered = TRUE,
          outlined = TRUE,
          onClick = "select",
          striped = TRUE,
          theme = Sivu_theme)

CONCLUSION

At the end of the project, I discovered that in recent weeks, I have been listening to a great deal of PARTYNEXTDOOR, Willie Nelson and Wale. The spotifyr and reactable package documentation is extensive, making the learning process user-friendly.

Packages

Wickham et al., (2019). Welcome to the tidyverse. Journal of Open Source Software, 4(43), 1686, https://doi.org/10.21105/joss.01686

Joe Cheng, Carson Sievert, Winston Chang, Yihui Xie and Jeff Allen (2021). htmltools: Tools for HTML. R package version 0.5.1.1.https://CRAN.R-project.org/package=htmltools

Greg Lin (2020). reactable: Interactive Data Tables Based on ‘React Table’. R package version 0.2.3. https://CRAN.R-project.org/package=reactable

Kyle Cuilla (2021). reactablefmtr: Simplify Formatting of Tables Made with ‘Reactable’. R package version 0.2.0. https://CRAN.R-project.org/package=reactablefmtr

Charlie Thompson, Josiah Parry, Donal Phipps and Tom Wolff (2021). spotifyr: R Wrapper for the ‘Spotify’ Web API. R package version 2.1.1. http://github.com/charlie86/spotifyr