Skip to content

Commit

Permalink
Merge pull request #2 from andypicke/1-add-option-to-specify-entity-i…
Browse files Browse the repository at this point in the history
…n-data-request

add option to specify entity in get_electricity_generation
  • Loading branch information
andypicke committed May 3, 2024
2 parents 3de09de + 874d930 commit a82d1a8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ URL: https://github.com/andypicke/emberr
BugReports: https://github.com/andypicke/emberr/issues
Imports:
httr,
jsonlite
jsonlite,
stringr
14 changes: 12 additions & 2 deletions R/get_electricity_generation.R
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
#' 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

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)
Expand Down
7 changes: 5 additions & 2 deletions man/get_electricity_generation.Rd

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

0 comments on commit a82d1a8

Please sign in to comment.