diff --git a/R/construct_query_url.R b/R/construct_query_url.R index 9164db5..0f89daf 100644 --- a/R/construct_query_url.R +++ b/R/construct_query_url.R @@ -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 { diff --git a/R/get_electricity_generation.R b/R/get_electricity_generation.R index 6739e92..76bdf49 100644 --- a/R/get_electricity_generation.R +++ b/R/get_electricity_generation.R @@ -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) diff --git a/R/get_ember_data.R b/R/get_ember_data.R index e2b7ae5..0780f5e 100644 --- a/R/get_ember_data.R +++ b/R/get_ember_data.R @@ -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, diff --git a/R/get_ember_options.R b/R/get_ember_options.R index e4bab70..4f0e7b5 100644 --- a/R/get_ember_options.R +++ b/R/get_ember_options.R @@ -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) diff --git a/man/construct_query_url.Rd b/man/construct_query_url.Rd index 58899c5..8951176 100644 --- a/man/construct_query_url.Rd +++ b/man/construct_query_url.Rd @@ -5,8 +5,9 @@ \title{Construct query URL for Ember data API} \usage{ construct_query_url( - endpoint = "electricity-generation", - temporal_resolution = "yearly", + endpoint = c("electricity-generation", "power-sector-emissions", "electricity-demand", + "carbon-intensity"), + temporal_resolution = c("yearly", "monthly"), min_date = "2015", max_date = "2023", entity = "all", diff --git a/man/get_electricity_generation.Rd b/man/get_electricity_generation.Rd index 43ba94e..f55e8d7 100644 --- a/man/get_electricity_generation.Rd +++ b/man/get_electricity_generation.Rd @@ -5,7 +5,7 @@ \title{Get Ember electricity-generation data from API} \usage{ get_electricity_generation( - temporal_resolution = "yearly", + temporal_resolution = c("yearly", "monthly"), min_date = "2015", max_date = "2023", entity = "all", diff --git a/man/get_ember_data.Rd b/man/get_ember_data.Rd index ce6c6ec..fa982e1 100644 --- a/man/get_ember_data.Rd +++ b/man/get_ember_data.Rd @@ -5,8 +5,9 @@ \title{Get Ember-climate data from API} \usage{ get_ember_data( - dataset = "electricity-generation", - temporal_resolution = "yearly", + dataset = c("electricity-generation", "power-sector-emissions", "electricity-demand", + "carbon-intensity"), + temporal_resolution = c("yearly", "monthly"), min_date = "2015", max_date = "2023", entity = "all", diff --git a/man/get_ember_options.Rd b/man/get_ember_options.Rd index 2398a29..c6fa71e 100644 --- a/man/get_ember_options.Rd +++ b/man/get_ember_options.Rd @@ -5,8 +5,9 @@ \title{Get API parameter options for Ember dataset} \usage{ get_ember_options( - dataset = "electricity-generation", - temporal_resolution = "yearly", + 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") ) @@ -16,7 +17,7 @@ get_ember_options( \item{temporal_resolution}{default ="yearly"} -\item{filter_name}{Parameter to get options for (ex: entity, series, date. Default = "entity"} +\item{filter_name}{Parameter to get options for (ex: entity, series, date. Default = "entity")} \item{api_key}{Default is Sys.getenv("EMBER_API_KEY")} }