From 703724df0e0b660507d6d507a7c16dbbf0e25f4b Mon Sep 17 00:00:00 2001 From: kkulma Date: Sat, 7 Nov 2020 19:52:26 +0000 Subject: [PATCH] tidied up R/ files --- R/generationmix.R | 21 ++++++----- R/intensity.R | 89 ++++++++++++++++++++++++----------------------- R/other.R | 17 +++++---- R/utils.R | 11 +++--- 4 files changed, 72 insertions(+), 66 deletions(-) diff --git a/R/generationmix.R b/R/generationmix.R index 3af05c8..e589099 100644 --- a/R/generationmix.R +++ b/R/generationmix.R @@ -13,32 +13,31 @@ #' get_mix()} get_mix <- function(start = NULL, end = NULL) { url <- 'https://api.carbonintensity.org.uk/generation/' - + if (all(!is.null(c(start, end)))) { from_date <- paste0(as.Date(start), "T00:00Z/") to_date <- paste0(as.Date(end), "T23:59Z") - + call <- paste0(url, from_date, to_date) } else { call <- url } - + data <- get_data(call) - + if (all(!is.null(c(start, end)))) { result <- data %>% - tidyr::unnest(.data$generationmix) %>% - dplyr::mutate( - from = lubridate::ymd_hm(.data$from), - to = lubridate::ymd_hm(.data$to)) %>% - tibble::as_tibble() + tidyr::unnest(.data$generationmix) %>% + dplyr::mutate(from = lubridate::ymd_hm(.data$from), + to = lubridate::ymd_hm(.data$to)) %>% + tibble::as_tibble() } else { result <- data$generationmix %>% dplyr::mutate(from = lubridate::ymd_hm(data$from), to = lubridate::ymd_hm(data$to)) %>% tibble::as_tibble() } - + result - + } diff --git a/R/intensity.R b/R/intensity.R index 4827dc7..28b602c 100644 --- a/R/intensity.R +++ b/R/intensity.R @@ -1,11 +1,11 @@ #' Fetch British carbon intensity data for specified time period #' -#' @param start {character} A start date of the intesity. +#' @param start {character} A start date of the intesity. #' @param end {character} An end date of the intesity data. The maximum date range is limited to 14 days. #' #' @return a data.frame with 1/2-hourly carbon intensity data for specified time period #' @export -#' @importFrom rlang .data +#' @importFrom rlang .data #' #' @examples \dontrun{ #' get_british_ci() @@ -15,28 +15,28 @@ get_british_ci <- function(start = NULL, end = NULL) { url <- 'https://api.carbonintensity.org.uk/intensity/' - + if (all(is.null(c(start, end)))) { call <- url } else if (all(!is.null(c(start, end)))) { from_date <- paste0(as.Date(start), 'T00:00Z/') to_date <- paste0(as.Date(end), 'T23:59Z') - + call <- paste0(url, from_date, to_date) } else { stop('Both start and end arguments have to be either NULL or in use') } - + data <- get_data(call) - + result <- data %>% dplyr::mutate(from = lubridate::ymd_hm(.data$from), to = lubridate::ymd_hm(.data$to)) %>% tibble::as_tibble() - + clean_names <- gsub('intensity.', '', colnames(result)) colnames(result) <- clean_names - + result } @@ -50,7 +50,7 @@ get_british_ci <- #' #' @return a tibble #' @export -#'@importFrom rlang .data +#'@importFrom rlang .data #' #' @examples \dontrun{ #' get_national_ci() @@ -67,41 +67,40 @@ get_national_ci <- !region %in% c('England', 'Scotland', 'Wales')) { stop("Region has to be either NULL or it must equate to one of 'England', 'Scotland', 'Wales'") } - + url <- 'https://api.carbonintensity.org.uk/regional/' - - + + if (all(is.null(c(region, start, end)))) { call <- url } else if (all(is.null(c(start, end)))) { call <- paste0(url, tolower(region), '/') } else if (all(!is.null(c(start, end)))) { - from_date <- paste0(as.Date(start), 'T00:00Z/') to_date <- paste0(as.Date(end), 'T23:59Z') call <- paste0(url, 'intensity/', from_date, to_date) } - + data <- get_data(call) - + if (is.null(region)) { unnest_var <- "regions" } else { unnest_var <- "data" } - + result <- data %>% tidyr::unnest(unnest_var) %>% tidyr::unnest(.data$generationmix) %>% dplyr::mutate(to = lubridate::ymd_hm(.data$to), from = lubridate::ymd_hm(.data$from)) %>% tibble::as_tibble() - + clean_names <- gsub('intensity.', '', colnames(result)) colnames(result) <- clean_names - + result - + } @@ -115,7 +114,7 @@ get_national_ci <- #' @return tibble #' @export #' -#' @importFrom rlang .data +#' @importFrom rlang .data #' @examples \dontrun{ #' get_postcode_ci(postcode = 'EN2') #' get_postcode_ci(postcode = 'EN2', start = '2019-01-01', end = '2019-01-02') @@ -129,21 +128,19 @@ get_postcode_ci <- function(postcode, } else if (all(!is.null(c(start, end)))) { from_date <- paste0(as.Date(start), 'T00:00Z/') to_date <- paste0(as.Date(end), 'T23:59Z/') - + url <- 'https://api.carbonintensity.org.uk/regional/intensity/' call <- paste0(url, from_date, to_date, 'postcode/', postcode) } - + data <- get_data(call) - + if (all(is.null(c(start, end)))) { result <- data %>% tidyr::unnest(.data$data) %>% - tidyr::unnest(.data$generationmix) %>% - dplyr::mutate( - from = lubridate::ymd_hm(.data$from), - to = lubridate::ymd_hm(.data$to) - ) + tidyr::unnest(.data$generationmix) %>% + dplyr::mutate(from = lubridate::ymd_hm(.data$from), + to = lubridate::ymd_hm(.data$to)) } else { result <- data$data %>% tidyr::unnest(.data$generationmix) %>% @@ -154,14 +151,17 @@ get_postcode_ci <- function(postcode, from = lubridate::ymd_hm(.data$from), to = lubridate::ymd_hm(.data$to) ) %>% - dplyr::select(.data$region, .data$shortname, .data$postcode, dplyr::everything()) + dplyr::select(.data$region, + .data$shortname, + .data$postcode, + dplyr::everything()) } - + clean_names <- gsub('intensity.', '', colnames(result)) colnames(result) <- clean_names - + result - + } @@ -171,7 +171,7 @@ get_postcode_ci <- function(postcode, #' @param start {character} A start date of the intesity data #' @param end {character} An end date of the intesity data #' -#' @importFrom rlang .data +#' @importFrom rlang .data #' @return a tibble #' @export #' @@ -183,22 +183,22 @@ get_regional_ci <- function(region_id, start = NULL, end = NULL) { url <- 'https://api.carbonintensity.org.uk/regional/regionid/' - + if (all(is.null(c(start, end)))) { call <- paste0(url, region_id) } else if (all(!is.null(c(region_id, start, end)))) { url <- 'https://api.carbonintensity.org.uk/regional/intensity/' - + from_date <- paste0(as.Date(start), 'T00:00Z/') to_date <- paste0(as.Date(end), 'T23:59Z/') - + call <- paste0(url, from_date, to_date, 'regionid/', region_id) } else { stop("Both start and end parameters must be NULL or in use") } - + data <- get_data(call) - + if (all(is.null(c(start, end)))) result <- data %>% tidyr::unnest(data) %>% @@ -215,13 +215,16 @@ get_regional_ci <- function(region_id, from = lubridate::ymd_hm(.data$from), to = lubridate::ymd_hm(.data$to) ) %>% - dplyr::select(.data$dnoregion, .data$shortname, .data$region_id, dplyr::everything()) + dplyr::select(.data$dnoregion, + .data$shortname, + .data$region_id, + dplyr::everything()) } - + clean_names <- gsub('intensity.', '', colnames(result)) colnames(result) <- clean_names - + result - - + + } diff --git a/R/other.R b/R/other.R index c873b0d..937b25e 100644 --- a/R/other.R +++ b/R/other.R @@ -1,5 +1,6 @@ + #' Get Carbon Intensity factors for each fuel type #' #' @return a tibble @@ -34,17 +35,21 @@ get_factors <- function() { get_stats <- function(start, end, block = NULL) { url <- "https://api.carbonintensity.org.uk/intensity/stats/" - if(as.numeric(as.Date(end) - as.Date(start)) > 30) { - stop(paste0("The start & end date range should not exceed 30 days. Currently it is ", - as.numeric(as.Date(end) - as.Date(start)), - " days")) + if (as.numeric(as.Date(end) - as.Date(start)) > 30) { + stop( + paste0( + "The start & end date range should not exceed 30 days. Currently it is ", + as.numeric(as.Date(end) - as.Date(start)), + " days" + ) + ) } - if(!is.null(block) && block > 24) { + if (!is.null(block) && block > 24) { stop("Block value should not exceed 24") } - if(!is.null(block) && block < 1) { + if (!is.null(block) && block < 1) { stop("Block value should not be lower than 1") } diff --git a/R/utils.R b/R/utils.R index dec2543..ca6472c 100644 --- a/R/utils.R +++ b/R/utils.R @@ -4,22 +4,21 @@ #' #' @return tibble get_data <- function(call) { - response <- httr::GET(call) - + if (httr::http_type(response) != "application/json") { stop("API did not return json", call. = FALSE) } - + if (response$status_code != 200) { stop(paste0("ERROR: The status call is ", response$status_code)) } - + response_content <- httr::content(response, as = "text", encoding = "UTF-8") - + data <- jsonlite::fromJSON(response_content, flatten = TRUE)[[1]] - + data }