I'm trying to map a function to obtain album data from Spotify's API for a data frame of album titles and respective artists, but it seems that my loop works better when I limit the number of albums the function searches for to 2.
Is there a way -- perhaps something in purrr
-- to automate my loop to only search for album data in batches/chunks of 2 (or any number)?
Here's an example:
library(dplyr)
library(spotifyr)
library(purrr)
# from Spotify's developer page
Sys.setenv(SPOTIFY_CLIENT_ID = "xxx")
Sys.setenv(SPOTIFY_CLIENT_SECRET = "xxx")
access_token <- get_spotify_access_token()
Artist <- c("Spiritualized", "Fleet Foxes", "The Avalanches", "Ween")
Album <- c("Sweet Heart, Sweet Light", "Helplessness Blues", "Wildflower", "The Mollusk")
mydata <- data_frame(Artist, Album)
get_album_data <- function(x) {
get_artist_audio_features(mydata[["Artist"]][x], return_closest_artist = TRUE) %>%
filter(agrepl(mydata[["Album"]][x], album_name, ignore.case = TRUE)) %>%
mutate(mydata[["Artist"]][x])
}
# will work here but is inconsistent
album_data <- map_df(seq(1, 4), get_album_data)
# what I want to be done automatically
album_data_1 <- map_df(seq(1, 2), get_album_data)
album_data_2 <- map_df(seq(3, 4), get_album_data)
Created on 2018-02-21 by the reprex package (v0.2.0).