0

enter image description hereMy code takes the two destination airports (JFK and then Las Vegas), passes them through a URL to return flight information in the For Loop, which I'm trying to add to a data frame. However, it only is including the results from the last element, Las Vegas. Should I use something other than a list for this?

library (httr)
library (jsonlite)

des <- c("JFK", "LAS")

flights = NULL 
flights = list()

  
  for (x in 1 : length(des))
  {
    url <- paste0("https://travelpayouts-travelpayouts-flight-data-v1.p.rapidapi.com/v1/prices/direct/?destination=", des[x], "&origin=BOS")
    
    r<-GET(url, add_headers("X-RapidAPI-Host" = "travelpayouts-travelpayouts-flight-data-v1.p.rapidapi.com",
                            "X-RapidAPI-Key" =   " MY KEY HERE ",
                            "X-Access-Token" = " MY TOKEN HERE"))
    
    jsonResponseParsed<-content(r,as="text")
    f <- fromJSON(jsonResponseParsed, flatten = TRUE)
    flights[[x]] <- data.frame(f$data)
  }

data = do.call(rbind, flights)
#price will be in rubles will need to convert to USD  
Pat
  • 11
  • 4
  • Are you sure the list is your issue? What's the length of `flights`? And if it's length 1, then have you tested that you can get JFK data by setting `x = 1`? – heds1 Jul 14 '20 at 20:18
  • If you run with the code `des <- c("JFK")` do you get a result? It's very hard to help without a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) we can actually run and test with. – MrFlick Jul 14 '20 at 20:20
  • Running it with just JFK will result in a length error Error in names(x) <- paste("V", seq_along(x), sep = "") : 'names' attribute [1] must be the same length as the vector [0] – Pat Jul 14 '20 at 20:25
  • Well it sounds like you are just not getting the data you expect from the server for that value. What is the `traceback()` after that error. It's unclear to me what line is generating that error. But it sounds like a data problem rather than a code problem. – MrFlick Jul 14 '20 at 20:28
  • When I took it out of the For Loop, I got expected results for JFK. Perhaps it is an issue with the server. Maybe limited API calls. It's not liking consecutive calls in that loop it seems – Pat Jul 14 '20 at 20:41

0 Answers0