From 9ade86f02ba02d4afb2282c8f4031c4b166bc306 Mon Sep 17 00:00:00 2001 From: Sam Herniman Date: Tue, 14 Oct 2025 02:19:27 +0200 Subject: [PATCH] Fixed co2array numeric issue --- R/ac_get_co2_transit.R | 65 ++++++++++++----------- R/ac_unnest_longer.R | 12 +++-- man/ac_unnest_longer.Rd | 4 +- tests/testthat/test-ac_get_co2_download.R | 6 +-- tests/testthat/test-ac_get_co2_transit.R | 8 ++- 5 files changed, 53 insertions(+), 42 deletions(-) diff --git a/R/ac_get_co2_transit.R b/R/ac_get_co2_transit.R index f06145b..3261d27 100644 --- a/R/ac_get_co2_transit.R +++ b/R/ac_get_co2_transit.R @@ -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 @@ -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() } diff --git a/R/ac_unnest_longer.R b/R/ac_unnest_longer.R index f330cb9..c65a974 100644 --- a/R/ac_unnest_longer.R +++ b/R/ac_unnest_longer.R @@ -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) -} \ No newline at end of file +} diff --git a/man/ac_unnest_longer.Rd b/man/ac_unnest_longer.Rd index 9168907..34ab1e2 100644 --- a/man/ac_unnest_longer.Rd +++ b/man/ac_unnest_longer.Rd @@ -16,6 +16,8 @@ a dataframe in long format Unnest a co2 df list-column into rows } \examples{ -ac_df <- ac_get_co2("download") |> +\dontrun{ +ac_df <- ac_get_co2("download") |> ac_unnest_longer() } +} diff --git a/tests/testthat/test-ac_get_co2_download.R b/tests/testthat/test-ac_get_co2_download.R index 1052fd1..b23d211 100644 --- a/tests/testthat/test-ac_get_co2_download.R +++ b/tests/testthat/test-ac_get_co2_download.R @@ -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") }) diff --git a/tests/testthat/test-ac_get_co2_transit.R b/tests/testthat/test-ac_get_co2_transit.R index 8849056..db53960 100644 --- a/tests/testthat/test-ac_get_co2_transit.R +++ b/tests/testthat/test-ac_get_co2_transit.R @@ -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") })