Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 34 additions & 31 deletions R/ac_get_co2_transit.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,68 +8,71 @@
#' ac_df <- ac_get_co2_transit()
#' }
ac_get_co2_transit <- function() {
co2Array <- NULL
icm_response <-
httr2::request("https://rkhby3mvq3.execute-api.eu-central-1.amazonaws.com/GetTransportCO2Data") |>
httr2::req_perform() |>
httr2::resp_body_json() |>
unlist() |>
httr2::resp_body_json() |>
unlist() |>
lapply(jsonlite::fromJSON)

j_df <- do.call(rbind, icm_response) |>
tibble::rowid_to_column("uid")
j_df <- do.call(rbind, icm_response) |>
tibble::rowid_to_column("uid")

j_df <- lapply(1:nrow(j_df), \(x) json_to_df(j_df[x,]))
j_df <-
j_df <-
do.call(rbind, j_df) |>
sf::st_as_sf(
coords = c("long_first", "lat_first"),
coords = c("long_first", "lat_first"),
crs = sf::st_crs(4326)
) |>
sf::st_make_valid()
sf::st_make_valid() |>
dplyr::mutate(co2Array = as.numeric(co2Array))

return(j_df)
}

#' Turn a co2 json into a dataframe
#'
#' @param x A json
#' @param x A json
#'
#' @returns a dataframe
json_to_df <- function(x) {
timestampArray <-
co2Array <-
longitudeArray <-
latitudeArray <-
startTime <-
uid <-
ppmAvg <-
timestampArray <-
co2Array <-
longitudeArray <-
latitudeArray <-
startTime <-
uid <-
ppmAvg <-
NULL
x |>
x |>
dplyr::mutate(
timestampArray = split_to_numeric(timestampArray) |> range01() |> paste(collapse = ";"),
# mod_loess = list(train_loess(co2_vec = co2Array, time_vec = timestampArray)),
# co2Array = list(split_to_numeric(co2Array)),
co2Array = stringr::str_replace_all(co2Array, ";", ","),
longitudeArray = stringr::str_remove_all(longitudeArray, "(0;|0$)") |>
stringr::str_replace_all(",", "\\.") |>
stringr::str_replace_all(";", ","),
longitudeArray = stringr::str_remove_all(longitudeArray, "(0;|0$)") |>
stringr::str_replace_all(",", "\\.") |>
stringr::str_replace_all(";", ",") ,
latitudeArray = stringr::str_remove_all(latitudeArray, "(0;|0$)") |>
stringr::str_replace_all(",", "\\.") |>
stringr::str_replace_all(",", "\\.") |>
stringr::str_replace_all(";", ","),
date = stringr::str_sub(startTime, end = -4) |>
as.numeric() |>
as.POSIXct()
) |>
tidyr::separate_longer_delim(cols = c(co2Array), delim = ",") |>
dplyr::distinct() |>
) |>
tidyr::separate_longer_delim(cols = c(co2Array), delim = ",") |>
dplyr::distinct() |>
dplyr::mutate(
long_first = stringr::str_split_i(longitudeArray, ",", 1) |> as.numeric(),
lat_first = stringr::str_split_i(latitudeArray, ",", 1) |> as.numeric()
) |>
# sf::st_as_sf(coords = c("long_first", "lat_first"), crs = sf::st_crs(4326)) |>
dplyr::group_by(uid) |>
tibble::rowid_to_column("tsa") |>
dplyr::select(-ppmAvg) |>
tidyr::drop_na()
) |>
# sf::st_as_sf(coords = c("long_first", "lat_first"), crs = sf::st_crs(4326)) |>
dplyr::group_by(uid) |>
tibble::rowid_to_column("tsa") |>
dplyr::select(-ppmAvg) |>
tidyr::drop_na()
}

#' Create a vector of values evenly spaced between 0 and 1
Expand All @@ -86,8 +89,8 @@ range01 <- function(x, ...){(x - min(x, ...)) / (max(x, ...) - min(x, ...))}
#'
#' @returns a numeric vector
split_to_numeric <- function(x) {
x |>
x |>
strsplit(split = ";") |>
unlist() |>
unlist() |>
as.numeric()
}
12 changes: 7 additions & 5 deletions R/ac_unnest_longer.R
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
#' Unnest a co2 df list-column into rows
#'
#' @param x a dataframe from ac_get_co2()
#' @param x a dataframe from ac_get_co2()
#'
#' @returns a dataframe in long format
#'
#' @export
#' @examples
#' ac_df <- ac_get_co2("download") |>
#' \dontrun{
#' ac_df <- ac_get_co2("download") |>
#' ac_unnest_longer()
#' }
ac_unnest_longer <- function(x) {
co2readings <- obs_number <- offset <- interval <- reading_index <- NULL
x_df <- x |> tidyr::unnest_longer(co2readings)

if (all(c("offset", "interval") %in% names(x))) {
x_df <- x_df |>
dplyr::group_by(obs_number) |>
dplyr::group_by(obs_number) |>
dplyr::mutate(
reading_index = dplyr::row_number(),
date_time = lubridate::as_datetime(date) +
lubridate::dminutes(offset) +
lubridate::dminutes(offset) +
lubridate::dminutes(interval * reading_index)
)
}

return(x_df)
}
}
4 changes: 3 additions & 1 deletion man/ac_unnest_longer.Rd

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

6 changes: 3 additions & 3 deletions tests/testthat/test-ac_get_co2_download.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ac_df <- ac_get_co2_transit()
ac_df <- ac_get_co2_download()

test_that("ac_get_co2_download works", {
expect_length(ac_df, 16)
testthat::expect_type(ac_df$co2Array[[1]], "character")
expect_length(ac_df, 22)
testthat::expect_type(ac_df$co2readings[[1]], "integer")
})
8 changes: 6 additions & 2 deletions tests/testthat/test-ac_get_co2_transit.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
test_that("multiplication works", {
expect_equal(2 * 2, 4)
ac_df <- ac_get_co2_transit()

test_that("ac_get_co2_transit works", {
expect_equal(length(ac_df), 16)
expect_gt(nrow(ac_df), 1000)
expect_type(ac_df$co2Array, "double")
})