My 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