[SOLVED] using a for loop to utilize spotifyr get_artist_audio_features function in R, skip errors in the loop


I downloaded my personal Spotify data from the Spotify website.
I converted these data from JSON to a regular R dataframe for further analysis. This personal dataframe has 4 columns:

Endtime   artistName   trackName   Msplayed

However, Spotify has many variables coupled to songs from an artist, that you can only retrieve using the function get_artist_audio_features from the spotifyr package. I want to join these variables to my personal dataframe. The package allows data retrieval for only one artist at a time and it would be very time consuming to write a line of code for all 3000+ artists in my dataframe.

I used a for loop to try and collect the metadata for the artists:

empty_list <- vector(mode = "list")
  for(i in df$artistName){
    empty_list[[i]] <- get_artist_audio_features(i)

My dataframe also has podcasts, for which non of this meta-data is available. When i try using the function on a podcast i get the error message:

Error in get_artist_audio_features(i) : 
  No artist found with artist_id=''.
In addition: Warning messages:
1: Unknown or uninitialised column: `id`. 
2: Unknown or uninitialised column: `name`. 

When i use the for loop, it stops as soon as the first error (podcast) in the dataframe occurs. When i feed it a vector of only artists and no podcasts, it works perfectly.

I checked stack for possible answers (most notably: Skipping error in for-loop) but i cant get the loop to work.

My question: how can i use the function spotifyr::get_artist_audio_features in a for loop and skip the errors, storing the results in a list. Unfortunately, it is very difficult to post a reproducable example, since you need to active a developer account on spotify to use the spotifyr package.


It looks like your issue is in artist_id = '', so try the below code to see if it helps get you started (since I don’t have reproducible data, not sure if it will help). In this case it should just skip the podcasts, but I’m sure some more codesmithing will allow you to put relevant data in the given list position.

for(i in df$artistName){
  if(artist_id = ''){
    empty_list[[i]] <- NA
  } else {
  empty_list[[i]] <- get_artist_audio_features(i)

You could also use a while loop conditioning on an incremental i to restart the loop, but I can’t do that without the data.

Answered By – jpsmith

Answer Checked By – Marie Seifert (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *