From 2582dc991b20716d7ca282ef84630f3671e9f3ec Mon Sep 17 00:00:00 2001 From: Eric Scott Date: Tue, 28 Jan 2025 16:29:28 -0700 Subject: [PATCH 1/4] convert to datetime or character, even when all NAs and add example for easier debugging in the future --- R/npn_data_download.R | 9 ++++++++- man/npn_get_data.Rd | 7 +++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/R/npn_data_download.R b/R/npn_data_download.R index f9768ee..6dfd6c3 100755 --- a/R/npn_data_download.R +++ b/R/npn_data_download.R @@ -689,6 +689,11 @@ npn_get_data_by_year <- function(endpoint, #' @returns A tibble of the requested data. If a `download_path` was specified, #' the file path is returned. #' @keywords internal +#' @examples \dontrun{ +#' npn_get_data( +#' url = "https://services.usanpn.org/npn_portal//observations/getObservations.ndjson?", +#' query = list(request_src = "Unit%20Test", climate_data = "0", `species_id[1]` = "6", start_date = "2010-01-01", end_date = "2010-12-31") +#' } npn_get_data <- function(url, query, download_path = NULL, @@ -722,7 +727,9 @@ npn_get_data <- function(url, dplyr::mutate(dplyr::across(dplyr::where(is.numeric), \(x) ifelse(x == -9999, NA_real_, x))) %>% dplyr::mutate(dplyr::across(dplyr::where(is.character), - \(x) ifelse(x == "-9999", NA_character_, x))) + \(x) ifelse(x == "-9999", NA_character_, x))) %>% + dplyr::mutate(update_datetime = as.POSIXct(update_datetime), + intensity_value = as.character(intensity_value)) # Reconcile all the points in the frame with the SIX leaf raster, # if it's been requested. diff --git a/man/npn_get_data.Rd b/man/npn_get_data.Rd index 10eaada..0cec1f3 100755 --- a/man/npn_get_data.Rd +++ b/man/npn_get_data.Rd @@ -34,4 +34,11 @@ the file path is returned. \description{ Generic utility function for querying data from the NPN data services. } +\examples{ +\dontrun{ +npn_get_data( + url = "https://services.usanpn.org/npn_portal//observations/getObservations.ndjson?", + query = list(request_src = "Unit\%20Test", climate_data = "0", `species_id[1]` = "6", start_date = "2010-01-01", end_date = "2010-12-31") +} +} \keyword{internal} From 9bf0bb6658b55f36ed14bcf05213f4bdfe0d2b7f Mon Sep 17 00:00:00 2001 From: Eric Scott Date: Tue, 28 Jan 2025 16:37:29 -0700 Subject: [PATCH 2/4] don't use anonymous function shortcut \(x) and use across(any_of()) because columns aren't always there. --- R/npn_data_download.R | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/R/npn_data_download.R b/R/npn_data_download.R index 6dfd6c3..1ca5931 100755 --- a/R/npn_data_download.R +++ b/R/npn_data_download.R @@ -724,12 +724,21 @@ npn_get_data <- function(url, promote_num_to_string = TRUE) %>% tibble::as_tibble() %>% #replace missing data indicator with NA - dplyr::mutate(dplyr::across(dplyr::where(is.numeric), - \(x) ifelse(x == -9999, NA_real_, x))) %>% - dplyr::mutate(dplyr::across(dplyr::where(is.character), - \(x) ifelse(x == "-9999", NA_character_, x))) %>% - dplyr::mutate(update_datetime = as.POSIXct(update_datetime), - intensity_value = as.character(intensity_value)) + dplyr::mutate( + dplyr::across(dplyr::where(is.numeric), + function(x) ifelse(x == -9999, NA_real_, x)) + ) %>% + dplyr::mutate( + dplyr::across(dplyr::where(is.character), + function(x) ifelse(x == "-9999", NA_character_, x)) + ) %>% + #handle some columns that may be read in as the wrong type if they happen + #to be all NAs (#87). `any_of()` is used because not all endpoints will + #return these columns! + dplyr::mutate( + dplyr::across(dplyr::any_of("update_datetime"), as.POSIXct), + dplyr::across(dplyr::any_of("intensity_value"), as.character) + ) # Reconcile all the points in the frame with the SIX leaf raster, # if it's been requested. From 85224075bf8c923df4a967ca9b176665304393ee Mon Sep 17 00:00:00 2001 From: Eric Scott Date: Tue, 28 Jan 2025 16:47:24 -0700 Subject: [PATCH 3/4] wrap example --- R/npn_data_download.R | 11 +++++++++-- man/npn_get_data.Rd | 11 +++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/R/npn_data_download.R b/R/npn_data_download.R index 1ca5931..34db94f 100755 --- a/R/npn_data_download.R +++ b/R/npn_data_download.R @@ -691,8 +691,15 @@ npn_get_data_by_year <- function(endpoint, #' @keywords internal #' @examples \dontrun{ #' npn_get_data( -#' url = "https://services.usanpn.org/npn_portal//observations/getObservations.ndjson?", -#' query = list(request_src = "Unit%20Test", climate_data = "0", `species_id[1]` = "6", start_date = "2010-01-01", end_date = "2010-12-31") +#' url = "https://services.usanpn.org/npn_portal//observations/getObservations.ndjson?", +#' query = list( +#' request_src = "Unit%20Test", +#' climate_data = "0", +#' `species_id[1]` = "6", +#' start_date = "2010-01-01", +#' end_date = "2010-12-31" +#' ) +#' ) #' } npn_get_data <- function(url, query, diff --git a/man/npn_get_data.Rd b/man/npn_get_data.Rd index 0cec1f3..e6aa5f1 100755 --- a/man/npn_get_data.Rd +++ b/man/npn_get_data.Rd @@ -37,8 +37,15 @@ Generic utility function for querying data from the NPN data services. \examples{ \dontrun{ npn_get_data( - url = "https://services.usanpn.org/npn_portal//observations/getObservations.ndjson?", - query = list(request_src = "Unit\%20Test", climate_data = "0", `species_id[1]` = "6", start_date = "2010-01-01", end_date = "2010-12-31") + url = "https://services.usanpn.org/npn_portal//observations/getObservations.ndjson?", + query = list( + request_src = "Unit\%20Test", + climate_data = "0", + `species_id[1]` = "6", + start_date = "2010-01-01", + end_date = "2010-12-31" + ) +) } } \keyword{internal} From 9640dc8f7c1a7e43c2d0ac18a0d3a396d60044c4 Mon Sep 17 00:00:00 2001 From: Eric Scott Date: Tue, 28 Jan 2025 16:53:36 -0700 Subject: [PATCH 4/4] use UTC as timezone --- R/npn_data_download.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/npn_data_download.R b/R/npn_data_download.R index 34db94f..6dc0078 100755 --- a/R/npn_data_download.R +++ b/R/npn_data_download.R @@ -743,7 +743,8 @@ npn_get_data <- function(url, #to be all NAs (#87). `any_of()` is used because not all endpoints will #return these columns! dplyr::mutate( - dplyr::across(dplyr::any_of("update_datetime"), as.POSIXct), + dplyr::across(dplyr::any_of("update_datetime"), + function(x) as.POSIXct(x, tz = "UTC")), dplyr::across(dplyr::any_of("intensity_value"), as.character) )