Search code examples
rweatherdarksky

How can we extract London weather in 2000 via Darksky API?


#install.packages("darksky")
library(darksky)
library(tidyverse)

# current verison
packageVersion("darksky")

Sys.setenv("DARKSKY_API_KEY" = # Secret API Key
             )
Sys.getenv("DARKSKY_API_KEY")

#This coordinates works 
now <- get_current_forecast(43.2672, -70.8617)
print(now)

Trying to extract temperature info for London ...

seq(as.Date("2000-01-01"), as.Date("2001-01-01"), "day") %>% 
  map(~get_forecast_for(51.5074, 0.1278, .x)) %>% 
  map_df("daily") %>% 
  ggplot(aes(x=time, y=temperature)) +
  geom_line()´

But got access forbidden HTTP 403 error


Solution

  • HTTP 403 error states, that you have not access to the called website. So this means your key is wrong. First of you need to create an account on darksky. Please be aware: you just get 1000 api calls per day for free (check the faq), if you want to call the whole year you already got 365 api calls, since you are doing one call per day. So try with smaller dates first.

    You can enter the API Key by using darksky_api_key() which gives you the chance to enter your key in the console. Otherwise you can create an .Renviron file and add the key there (for this check the documentary of the darksky package).

    So after adding the key in the console, here a code working example from github (which i guess, you also used). Adapt the date and the plot for your case, i used just one month of data because of the call limit.

    library(darksky)
    library(tidyverse)
    library(ggplot2)
    
    # current verison
    
    
    darksky_api_key()
    
    #This coordinates works 
    now <- get_current_forecast(43.2672, -70.8617)
    print(now)
    
    df <- seq(as.Date("2000-01-01"), as.Date("2000-02-02"), "day") %>% 
      map(~get_forecast_for(51.5074, 0.1278, .x)) 
    
    df %>%
      map_df("hourly") %>%
      ggplot(aes(x=time, y=temperature)) +
      geom_line()
    

    Output: Output plot