KKulma / intensegRid

R wrapper for National Grid Carbon Intensity API
https://carbonintensity.org.uk/
Creative Commons Zero v1.0 Universal
9 stars 2 forks source link

Add wrappers to call longer time periods #33

Open dr-harper opened 2 years ago

dr-harper commented 2 years ago

The way it is currently written means that the code can only return 14 days at a time. If you specify more than 14 days, it will return an API error.

I've basic wrapper for your code which splits longer date strings into chunks of 14, then loops through each one separately and merges the results. Useful if you want time periods.

Just an idea for an enhancement :) Here was my 5 minuets of code:

get_british_ci_wrapper <- function(start = '2019-09-07', end = '2021-10-24'){

  dates <- seq(ymd(start), ymd(end), by = '1 day')

  df_ci <-
    purrr::map_df(split(dates, ceiling(seq_along(dates) / 14)), function(i){
      start = min(i)
      end = max(i)

      results = get_british_ci(start, end)
      return(results)

    })

  return(df_ci)

}
KKulma commented 2 years ago

great idea, @dr-harper. Feel free to open a PR with your contribution!

dr-harper commented 2 years ago

Sure thing!

I will also make sure I proofread my code more than I did my writing in the previous message 😂

dr-harper commented 2 years ago

Added a PR: https://github.com/KKulma/intensegRid/pull/34, let's continue the discussion there

KKulma commented 2 years ago

I'll keep this issue open until we merge the changes that fix it into master, if that's ok 👍