Skip to content

Commit

Permalink
add option to specify single year
Browse files Browse the repository at this point in the history
  • Loading branch information
andypicke committed Sep 19, 2024
1 parent 2b54a39 commit b44daf0
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 1 deletion.
13 changes: 13 additions & 0 deletions R/construct_query_url.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#' Construct query URL for Ember data API
#' @param endpoint Default = "electricity-generation"
#' @param temporal_resolution "yearly" (default) or "monthly"
#' @param year (optional) If specified, retrieve data for a single year
#' @param min_date Mininum date to retrieve data for. YYYY or YYYY-MM format. Default=2015
#' @param max_date Maximum date to retrieve data for. YYYY or YYYY-MM format. Default=2023
#' @param entity List of comma-separated country(s) or region(s) to return data for. Default is all (no filter)
Expand All @@ -10,6 +11,7 @@
#'
construct_query_url <- function(endpoint = c("electricity-generation", "power-sector-emissions", "electricity-demand", "carbon-intensity"),
temporal_resolution = c("yearly", "monthly"),
year = NA,
min_date = "2015",
max_date = "2023",
entity = "all",
Expand All @@ -20,6 +22,17 @@ construct_query_url <- function(endpoint = c("electricity-generation", "power-se
endpoint <- match.arg(endpoint)
temporal_resolution <- match.arg(temporal_resolution)

# if year supplied, use that
if (!is.na(year)) {
if (temporal_resolution == "yearly") {
min_date <- year
max_date <- year
} else {
min_date <- year
max_date <- year + 1
}
}

if (entity == "all") {
entity_str <- ""
} else {
Expand Down
4 changes: 3 additions & 1 deletion R/get_electricity_generation.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#' Get Ember electricity-generation data from API
#' @param temporal_resolution "yearly" (default) or "monthly"
#' @param year (optional) If specified, retrieve data for a single year
#' @param min_date Mininum date to retrieve data for. YYYY or YYYY-MM format. Default=2015
#' @param max_date Maximum date to retrieve data for. YYYY or YYYY-MM format. Default=2023
#' @param entity List of comma-separated country(s) or region(s) to return data for. Default is all (no filter)
Expand All @@ -9,6 +10,7 @@
#' @seealso [get_ember_options()]

get_electricity_generation <- function(temporal_resolution = c("yearly", "monthly"),
year = NA,
min_date = "2015",
max_date = "2023",
entity = "all",
Expand All @@ -17,7 +19,7 @@ get_electricity_generation <- function(temporal_resolution = c("yearly", "monthl
# check input
temporal_resolution <- match.arg(temporal_resolution)

df <- get_ember_data(dataset = "electricity-generation", temporal_resolution, min_date, max_date, entity, api_key)
df <- get_ember_data(dataset = "electricity-generation", temporal_resolution, year, min_date, max_date, entity, api_key)

return(df)
}
3 changes: 3 additions & 0 deletions R/get_ember_data.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#' Get Ember-climate data from API
#' @param dataset Dataset to retrieve. Default = "electricity-generation". Other options are "power-sector-emissions", "electricity-demand", and "carbon-intensity"
#' @param temporal_resolution "yearly" (default) or "monthly"
#' @param year (optional) If specified, retrieve data for a single year
#' @param min_date Minimum date to retrieve data for. YYYY or YYYY-MM format. Default=2015
#' @param max_date Maximum date to retrieve data for. YYYY or YYYY-MM format. Default=2023
#' @param entity List of comma-separated country(s) or region(s) to return data for. Default is all (no filter)
Expand All @@ -11,6 +12,7 @@

get_ember_data <- function(dataset = c("electricity-generation", "power-sector-emissions", "electricity-demand", "carbon-intensity"),
temporal_resolution = c("yearly", "monthly"),
year = NA,
min_date = "2015",
max_date = "2023",
entity = "all",
Expand All @@ -22,6 +24,7 @@ get_ember_data <- function(dataset = c("electricity-generation", "power-sector-e

query_url <- construct_query_url(endpoint = dataset,
temporal_resolution = temporal_resolution,
year = year,
min_date = min_date,
max_date = max_date,
entity = entity,
Expand Down
3 changes: 3 additions & 0 deletions man/construct_query_url.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/get_electricity_generation.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/get_ember_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b44daf0

Please sign in to comment.