0

I've created a data frame with a list of IDs.

myIDs <- (c(45655,45656,45657,45658,45659))
myIDs <- as.data.frame(myIDs)

And I have to call as many API queries per ID. I wanted to create a loop this way:

apiP1 <- "http://api.mydomain.com/2.0/facts/"
apiP2 <- "?Authorization=mytoken"
for (i in myIDs$myIDs[1:5]){
  mydata <- fromJSON(file = paste0(apiP1,i,apiP2))
  }

I'm wondering which way I should take to get all rows from the APIs in 1 single data frame. I was thinking about creating 1 object per request and then use a RBIND rule.

In order to do that, how do I automatically create objects like:

mydata45655 which should contain the data from the API with ID 45655

mydata45656 which should contain the data from the API with ID 45656

Etc.

shrgm
  • 1,315
  • 1
  • 10
  • 20
  • 1
    Save the downloads into a list: `myList <- list(); myList[[paste0("dat", i)]]<- fromJSON(file = paste0(apiP1,i,apiP2))`. This is the best way to accomplish such tasks in R as it keeps your data organize. There are also good function like `lapply` that are designed to work with lists. See [this post](http://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames/24376207) for a longer discussion. – lmo May 22 '16 at 12:53

1 Answers1

0

I absolutely agree with @lmo's advice. Here is the code that might help you:

myIDs <- c(45655,45656,45657,45658,45659)

apiP1 <- "http://api.mydomain.com/2.0/facts/"
apiP2 <- "?Authorization=mytoken"

for (i in myIDs){
  eval(parse(text = paste0('mydata',i,' <- fromJSON(file = paste0(apiP1,i,apiP2))')))
}
Kunal Puri
  • 3,419
  • 1
  • 10
  • 22