rloopsdiscogs-api

Why does my R loop return an out of bounds error?


I'm trying to write a looped function that extracts data from the discogs api. I've started with writing a function 'a' and it works:

releases <- list()
artists <- list()
artistURL <- "https://api.discogs.com/artists/"

a <- function(artistcode){
  for(i in 0:3){

  artistset <- fromJSON(paste0(artistURL, artistcode, "/releases?page=", i))
  message("Retrieving page ", i)

  releases[[i+1]] <- (as.data.frame(artistset$releases.main_release))
  artists[[i+1]] <- (as.data.frame(artistset$releases.artist ))
}
  return(artistset)
  message("Total rows=", dim(artistset[[2]])[1] )
}
x <- a(135872)

Next, I'd like to now add this function into a loop the grabs data for a set of artists who are included in a dataframe, like this:

artistdf <- structure(list(
    name = c("Hank Mobley", "Benny Green", "Oscar Peterson", "Art Tatum"), 
    artistcode = c(135872,96442, 254394, 265634)
), .Names = c("name", "artistcode"), row.names = c(NA,-5L), class = c("tbl_df", "tbl", "data.frame"))

for (j in 0:nrow(artistdf)){
    a(artistdf[[j+1, 2]])
}

This is where I get an 'out of bounds' error. I've used some debugging advice, but to no avail. Can anyone offer a solution?


Solution

  • There appear to be two issues.

    First, where you create the artistdf tibble, you're passing "row.names = c(NA,-5L)," which is creating an object with 5 rows although you only have 4 rows of data. Change this to c(NA, -4L).

    Second, starting your final for loop at 0 is creating an issue. I don't know why that is, but change it as follows:

    jazzdata <- list()
    for (j in 1:nrow(artistdf)){
        jazzdata[[j]] <- a(artistdf[[j, 2]])
    }
    jazzdata[2] # data are here
    

    With these two changes, I was able to get your code to work.