Skip to content

Commit

Permalink
tidied up R/ files
Browse files Browse the repository at this point in the history
  • Loading branch information
KKulma committed Nov 7, 2020
1 parent 3a92cbf commit 703724d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 66 deletions.
21 changes: 10 additions & 11 deletions R/generationmix.R
Original file line number Diff line number Diff line change
Expand Up @@ -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

}
89 changes: 46 additions & 43 deletions R/intensity.R
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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
}

Expand All @@ -50,7 +50,7 @@ get_british_ci <-
#'
#' @return a tibble
#' @export
#'@importFrom rlang .data
#'@importFrom rlang .data
#'
#' @examples \dontrun{
#' get_national_ci()
Expand All @@ -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

}


Expand All @@ -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')
Expand All @@ -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) %>%
Expand All @@ -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

}


Expand All @@ -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
#'
Expand All @@ -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) %>%
Expand All @@ -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


}
17 changes: 11 additions & 6 deletions R/other.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@



#' Get Carbon Intensity factors for each fuel type
#'
#' @return a tibble
Expand Down Expand Up @@ -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")
}

Expand Down
11 changes: 5 additions & 6 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 703724d

Please sign in to comment.