0

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?

alistaire
  • 42,459
  • 4
  • 77
  • 117
Ben
  • 1,113
  • 10
  • 26

1 Answers1

0

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.

JeffR
  • 524
  • 3
  • 10
  • thanks! the code works with your changes, but i'm getting a `NULL` result. Any idea why? I used `jazz4<-for (j in 1:nrow(artistdf)){ a(artistdf[j, 2])` removing the double brackets (double brackets didn't work either) – Ben Jul 08 '19 at 13:02
  • Try this. First define jazz4 as an empty list: jazz4 <- list(). Then, in your for loop, use jazz4[[ j ]] <- a(artistdf[[j, 2]]). You should get a jazz4 object that consists of a list of lists that you can process. – JeffR Jul 10 '19 at 04:16
  • That works! Do you want to include this as the final answer? `jazzdata<-list();` `for (j in 1:nrow(artistdf)){ jazzdata[[ j ]] <- a(artistdf[[j, 2]]) }` `jazzdata[2] # data are here` – Ben Jul 10 '19 at 14:43