How to get a day weather forecast for a list of cities using R?

I tried to use this function to get weather forecast data to return a data frame containing 5-day weather forecasts for a list of cities but I do not know what is missing. Here I using openweathermap.org for the weather data but I can not get it right.
cities <- c("Seoul", "Washington, D.C.", "Paris", "Suzhou")

# Get forecast data for a given city list
get_weather_forecaset_by_cities <- function(city_names){
    df <- data.frame()
    for (city_name in city_names){
        # Forecast API URL
        forecast_url <- 'https://api.openweathermap.org/data/2.5/forecast'
        # Create query parameters
        forecast_query <- list(q = city_name, appid = "{your_api_key}", units="metric")
        # Make HTTP GET call for the given city
        
        # Note that the 5-day forecast JSON result is a list of lists. You can print the reponse to check the results
        #results <- json_list$list
        
        # Loop the json result
        for(result in results) {
            city <- c(city, city_name)
            
        }
        
        # Add the R Lists into a data frame
    }
    
    # Return a data frame
    return(df)

Hi,
What is the error code or incorrect format you get at your end?
Also can you attach the data for variable "forecast_query" since I cannot get access to the API w/o the key.

Most likely the API is not being called correctly with correct values or the "forecast_query" results to json is being messed up.

API: b0847c4a1554d3c63d46d0e9249500f0
as I cant complete the last part .

 # Note that the 5-day forecast JSON result is a list of lists. You can print the reponse to check the results
        #results <- json_list$list
        
        # Loop the json result
        for(result in results) {
            city <- c(city, city_name)
            
        }
        
        # Add the R Lists into a data frame
    }

You can use this to start and tweak as you process your dataframe. Some columns of the dataframe are lists with nested dataframe which you can extract as per your needs.

library(httr)
library(dplyr)
library(stringr)
library(jsonlite)
> city_names <- c("Seoul", "Washington, D.C.", "Paris", "Suzhou")
> api_id<-'b0847c4a1554d3c63d46d0e9249500f0'
> main_df<-data.frame()
> for (city_name in city_names){
>   url<-str_glue("http://api.openweathermap.org/data/2.5/forecast?id=524901&appid={api_id}&q={city_name}")
>   main_df<- bind_rows(main_df,get_weather_forecaset_by_cities(url))
> }
> 
> 
> get_weather_forecaset_by_cities <- function(url){
>   web_content <- httr::GET(url)
>   web_content <- content(web_content,"text")
>   json_data <- fromJSON(web_content, flatten = TRUE)
>   df <- as.data.frame(json_data)
>   return(df)
> }

if I may ask. why using str_glue?

I normally use str_glue to build dynamic strings that need data from other variables.

For your use case you can also use

url<-paste0("http://api.openweathermap.org/data/2.5/forecast?id=524901&appid=",api_id,"&q=",city_name)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.