diff --git a/DESCRIPTION b/DESCRIPTION index 9dacf34..8bb9720 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -13,4 +13,5 @@ URL: https://github.com/andypicke/emberr BugReports: https://github.com/andypicke/emberr/issues Imports: httr, - jsonlite + jsonlite, + stringr diff --git a/R/get_electricity_generation.R b/R/get_electricity_generation.R index 6dd52db..cedd35a 100644 --- a/R/get_electricity_generation.R +++ b/R/get_electricity_generation.R @@ -1,7 +1,8 @@ #' Get Ember electricity-generation data from API #' @param temporal_resolution "yearly" (default) or "monthly" -#' @param min_date default=2015 -#' @param max_date default=2023 +#' @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) #' @param api_key Default is Sys.getenv("EMBER_API_KEY") #' @returns df Dataframe of requested data #' @export @@ -9,14 +10,23 @@ get_electricity_generation <- function(temporal_resolution = "yearly", min_date = "2015", max_date = "2023", + entity = "all", api_key = Sys.getenv("EMBER_API_KEY")) { base_url <- "https://api.ember-climate.org/v1/" endpoint <- "electricity-generation/" + if (entity == "all") { + entity_str <- "" + } else { + entity_str <- paste0("entity=", stringr::str_replace(entity, " ", "%20"), "&") # '%20' replaces space in URL + } + query_url <- paste0(base_url, endpoint, temporal_resolution, "?", + entity_str, "start_date=", min_date, "&end_date=", max_date, + entity_str, "&api_key=", api_key) df <- get_api_request(query_url) diff --git a/man/get_electricity_generation.Rd b/man/get_electricity_generation.Rd index f7e018d..c720ddc 100644 --- a/man/get_electricity_generation.Rd +++ b/man/get_electricity_generation.Rd @@ -8,15 +8,18 @@ get_electricity_generation( temporal_resolution = "yearly", min_date = "2015", max_date = "2023", + entity = "all", api_key = Sys.getenv("EMBER_API_KEY") ) } \arguments{ \item{temporal_resolution}{"yearly" (default) or "monthly"} -\item{min_date}{default=2015} +\item{min_date}{Mininum date to retrieve data for. YYYY or YYYY-MM format. Default=2015} -\item{max_date}{default=2023} +\item{max_date}{Maximum date to retrieve data for. YYYY or YYYY-MM format. Default=2023} + +\item{entity}{List of comma-separated country(s) or region(s) to return data for. Default is all (no filter)} \item{api_key}{Default is Sys.getenv("EMBER_API_KEY")} }