我正在尝试编写一个从 discogs api 中提取数据的循环函数。我开始编写一个函数'a'并且它有效:
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)
接下来,我现在想将此函数添加到一个循环中,以获取包含在数据框中的一组艺术家的数据,如下所示:
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]])
}
这是我得到“越界”错误的地方。我使用了一些调试建议,但无济于事。任何人都可以提供解决方案吗?