0
votes

Apologies for the newbie question but I'm struggling with the following;

I'm using the tiagomendesdantas/Rspotify function to access the Spotify API via R. When using this to access information on single artists, playlists or tracks it works fine but I want to run this for multiple artists at once.

I tried creating lists etc. but that doesn't work.

#Getting the songs of Spotify's RapCaviar playlists
rapcaviar <- getPlaylistSongs(ownerid = "Spotify",
                              "37i9dQZF1DX0XUsuxWHRQd",
                              token = keys) 
rapcaviar

What should I do to include multiple ID's for either playlists or artists?

I often get the error:

Error: length(url) == 1 is not TRUE

Ideally you'd use the outcome of the getartistinfo function for other options like getfeatures to retrieve the acoustic elements of songs.

Help is much appreciated (and hopefully/probably rather easy? ;-))

Link to function: https://github.com/tiagomendesdantas/Rspotify/blob/master/DESCRIPTION

1
have you tried a loop or an iteration over a list? - Colin FAY
I haven’t and to be honest, I’m not too familiar with these (yet). Would you be so kind to share an example? :) - BJSP

1 Answers

0
votes

Here's a way to do it with {purrr}

library(Rspotify)
keys <- spotifyOAuth("rstats","***","***")


library(purrr)
# Create a vector of owners id 
owners_id <- c('11152045841', 'spotify', 'spotify')
# Create a vector of the corresponding playlist id
playlists_id <- c('0FJiPbIDGcxw6hx9dLVQPl', '37i9dQZF1DX5wiMM8SSK44', '37i9dQZF1DWT9SRKhOEUYj')

# Use purrr::map2 to retrieve a list of dataframe of the playlists
playlists <- map2(owners_id, playlists_id, ~ getPlaylistSongs(.x, .y, token = keys))

# You can also use purrr::map2_df if you want to bind if all in a unique data.frame
playlists <- map2_df(owners_id, playlists_id, ~ getPlaylistSongs(.x, .y, token = keys))