diff --git a/R/get_sensor_history.R b/R/get_sensor_history.R index 53f07f2..02f8f82 100644 --- a/R/get_sensor_history.R +++ b/R/get_sensor_history.R @@ -22,7 +22,9 @@ get_sensor_history <- function(sensor_index, fields, start_timestamp, end_timestamp, - average = c("10min", "30min", "60min", "6hr", "1day", "1week", "1month", "1year", "real-time"), + average = c("10min", "30min", "60min", + "6hr", "1day", "1week", + "1month", "1year", "real-time"), purple_air_api_key = Sys.getenv("PURPLE_AIR_API_KEY"), read_key = NULL) { if (!rlang::is_integer(as.integer(sensor_index))) cli::cli_abort("sensor_index must be an integer") @@ -35,7 +37,7 @@ get_sensor_history <- function(sensor_index, )[avg] ) resp <- - purple_air_request( + PurpleAir:::purple_air_request( resource = "sensor_history", success_code = as.integer(200), sensor_index = as.integer(sensor_index), @@ -44,7 +46,9 @@ get_sensor_history <- function(sensor_index, average = avg_int, fields = fields, read_key = read_key - ) + ) |> + httr2::req_perform() |> + httr2::resp_body_json() out <- purrr::map(resp$data, stats::setNames, resp$fields) |> purrr::modify(as.data.frame) |> @@ -52,5 +56,5 @@ get_sensor_history <- function(sensor_index, tibble::as_tibble() out$time_stamp <- as.POSIXct.numeric(out$time_stamp) return(out) - md <- purrr::discard_at(resp, c("fields", "data")) + ## md <- purrr::discard_at(resp, c("fields", "data")) } diff --git a/R/purple_air_request.R b/R/purple_air_request.R index 4ac19e1..b8323d8 100644 --- a/R/purple_air_request.R +++ b/R/purple_air_request.R @@ -1,4 +1,8 @@ -purple_air_request <- function(purple_air_api_key = Sys.getenv("PURPLE_AIR_API_KEY"), resource = c("keys", "organization", "sensors", "sensor_history"), sensor_index = NULL, success_code, ...) { +purple_air_request <- function(purple_air_api_key = Sys.getenv("PURPLE_AIR_API_KEY"), + resource = c("keys", "organization", "sensors", "sensor_history"), + sensor_index = NULL, + success_code, + ...) { if (!rlang::is_integer(success_code)) cli::cli_abort("success_code must be an integer") resource <- rlang::arg_match(resource) req <- diff --git a/README.Rmd b/README.Rmd index 7c6b270..ea82ca1 100644 --- a/README.Rmd +++ b/README.Rmd @@ -81,6 +81,11 @@ Get the latest data from several PurpleAir sensors: Get historical data from a single PurpleAir sensor: +```{r} +#| echo: false +Sys.sleep(3) +``` + ```{r} my_history <- get_sensor_history( diff --git a/README.md b/README.md index 100f9cb..6010687 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ use: ``` r check_api_key(Sys.getenv("PURPLE_AIR_API_KEY")) -#> ✔ Using valid 'READ' key with version V1.0.14-0.0.57 of the PurpleAir API on 2024-07-05 14:44:18 +#> ✔ Using valid 'READ' key with version V1.0.14-0.0.57 of the PurpleAir API on 2024-07-06 00:06:47 ``` Get the latest data from a single PurpleAir sensor, defined by its @@ -64,16 +64,16 @@ Get the latest data from a single PurpleAir sensor, defined by its get_sensor_data(sensor_index = 175413, fields = c("name", "last_seen", "pm2.5_cf_1", "pm2.5_atm")) #> $last_seen -#> [1] "2024-07-05 14:41:43 EDT" +#> [1] "2024-07-06 00:05:48 EDT" #> #> $name #> [1] "JN-Clifton,OH" #> #> $pm2.5_atm -#> [1] 4.6 +#> [1] 23.5 #> #> $pm2.5_cf_1 -#> [1] 4.6 +#> [1] 23.5 ``` Get the latest data from many PurpleAir sensors, defined by their sensor @@ -85,10 +85,51 @@ get_sensors_data(x = as.integer(c(175257, 175413)), #> # A tibble: 2 × 5 #> sensor_index last_seen name pm2.5_atm pm2.5_cf_1 #> -#> 1 175257 2024-07-05 14:42:00 Lillard 3.9 3.9 -#> 2 175413 2024-07-05 14:41:43 JN-Clifton,OH 4.6 4.6 +#> 1 175257 2024-07-06 00:06:06 Lillard 29.5 30.2 +#> 2 175413 2024-07-06 00:05:48 JN-Clifton,OH 23.5 23.5 ``` a geographic bounding box, or a date from which sensors must have been modified since. + +Get the latest data from several PurpleAir sensors: + +Get historical data from a single PurpleAir sensor: + +``` r +my_history <- + get_sensor_history( + sensor_index = 175413, + fields = c("pm1.0_cf_1", "pm1.0_atm", "pm2.5_cf_1", "pm2.5_atm"), + start_timestamp = as.POSIXct("2024-07-02"), + end_timestamp = as.POSIXct("2024-07-05") + ) + +my_history +#> # A tibble: 432 × 5 +#> time_stamp pm1.0_cf_1 pm1.0_atm pm2.5_atm pm2.5_cf_1 +#> +#> 1 2024-07-04 22:00:00 2512. 1678. 1682. 2516. +#> 2 2024-07-04 21:20:00 2505. 1671. 1672. 2506. +#> 3 2024-07-04 20:30:00 16.5 14.2 16.5 18.9 +#> 4 2024-07-04 21:40:00 2509. 1675. 1679. 2512. +#> 5 2024-07-04 22:30:00 2522. 1683. 1689. 2530. +#> 6 2024-07-04 21:10:00 2505. 1671. 1672. 2506. +#> 7 2024-07-04 20:40:00 31.9 25.3 27.9 34.6 +#> 8 2024-07-04 23:30:00 2510. 1676. 1680. 2513. +#> 9 2024-07-04 23:50:00 2509. 1675. 1678. 2512. +#> 10 2024-07-04 23:20:00 2514. 1679. 1683. 2518. +#> # ℹ 422 more rows +``` + +and plot it: + +``` r +my_history |> + tidyr::pivot_longer(cols = tidyr::starts_with("pm"), names_to = "pollutant", values_to = "concentration") |> + ggplot2::ggplot(ggplot2::aes(time_stamp, concentration, color = pollutant)) + + ggplot2::geom_line() +``` + + diff --git a/man/figures/README-unnamed-chunk-11-1.png b/man/figures/README-unnamed-chunk-11-1.png new file mode 100644 index 0000000..f1246b7 Binary files /dev/null and b/man/figures/README-unnamed-chunk-11-1.png differ