Skip to content

Commit

Permalink
specify input options explicity and use match.arg to check
Browse files Browse the repository at this point in the history
  • Loading branch information
andypicke committed Jun 21, 2024
1 parent 3333c29 commit 2b54a39
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 17 deletions.
8 changes: 6 additions & 2 deletions R/construct_query_url.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@
#' @returns query_url API query URL for specified parameters
#' @export
#'
construct_query_url <- function(endpoint = "electricity-generation",
temporal_resolution = "yearly",
construct_query_url <- function(endpoint = c("electricity-generation", "power-sector-emissions", "electricity-demand", "carbon-intensity"),
temporal_resolution = c("yearly", "monthly"),
min_date = "2015",
max_date = "2023",
entity = "all",
api_key = Sys.getenv("EMBER_API_KEY")) {
base_url <- "https://api.ember-climate.org/v1/"

# check inputs
endpoint <- match.arg(endpoint)
temporal_resolution <- match.arg(temporal_resolution)

if (entity == "all") {
entity_str <- ""
} else {
Expand Down
5 changes: 4 additions & 1 deletion R/get_electricity_generation.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
#' @export
#' @seealso [get_ember_options()]

get_electricity_generation <- function(temporal_resolution = "yearly",
get_electricity_generation <- function(temporal_resolution = c("yearly", "monthly"),
min_date = "2015",
max_date = "2023",
entity = "all",
api_key = Sys.getenv("EMBER_API_KEY")) {

# check input
temporal_resolution <- match.arg(temporal_resolution)

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

return(df)
Expand Down
8 changes: 6 additions & 2 deletions R/get_ember_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@
#' @export
#' @seealso [get_ember_options()]

get_ember_data <- function(dataset = "electricity-generation",
temporal_resolution = "yearly",
get_ember_data <- function(dataset = c("electricity-generation", "power-sector-emissions", "electricity-demand", "carbon-intensity"),
temporal_resolution = c("yearly", "monthly"),
min_date = "2015",
max_date = "2023",
entity = "all",
api_key = Sys.getenv("EMBER_API_KEY")) {

# check inputs
dataset <- match.arg(dataset)
temporal_resolution <- match.arg(temporal_resolution)

query_url <- construct_query_url(endpoint = dataset,
temporal_resolution = temporal_resolution,
min_date = min_date,
Expand Down
11 changes: 7 additions & 4 deletions R/get_ember_options.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
#' Get API parameter options for Ember dataset
#' @param dataset Default="electricity-generation"
#' @param temporal_resolution default ="yearly"
#' @param filter_name Parameter to get options for (ex: entity, series, date. Default = "entity"
#' @param filter_name Parameter to get options for (ex: entity, series, date. Default = "entity")
#' @param api_key Default is Sys.getenv("EMBER_API_KEY")
#' @returns options List of options for specified parameter
#' @export

get_ember_options <- function(dataset = "electricity-generation",
temporal_resolution = "yearly",
get_ember_options <- function(dataset = c("electricity-generation", "power-sector-emissions", "electricity-demand", "carbon-intensity"),
temporal_resolution = c("yearly", "monthly"),
filter_name = "entity",
api_key = Sys.getenv("EMBER_API_KEY")){

base_url <- "https://api.ember-climate.org/v1/options/"
# check inputs
dataset <- match.arg(dataset)
temporal_resolution <- match.arg(temporal_resolution)

base_url <- "https://api.ember-climate.org/v1/options/"

query_url <- paste0(base_url, dataset, "/", temporal_resolution, "/", filter_name, "?api_key=", api_key)

Expand Down
5 changes: 3 additions & 2 deletions man/construct_query_url.Rd

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

2 changes: 1 addition & 1 deletion man/get_electricity_generation.Rd

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

5 changes: 3 additions & 2 deletions man/get_ember_data.Rd

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

7 changes: 4 additions & 3 deletions man/get_ember_options.Rd

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

0 comments on commit 2b54a39

Please sign in to comment.