From a3a2726a3be3fa7fb740ad756d18179815844649 Mon Sep 17 00:00:00 2001 From: fawda123 Date: Fri, 2 Aug 2024 10:03:59 -0400 Subject: [PATCH] update arguments for read_importentero for easier input --- R/read_importentero.R | 45 +++++++++++++------------ data-raw/enterodata-raw.R | 16 +-------- man/read_importentero.Rd | 30 +++++------------ tests/testthat/test-read_importentero.R | 10 ++---- 4 files changed, 35 insertions(+), 66 deletions(-) diff --git a/R/read_importentero.R b/R/read_importentero.R index f8df53f6..a9672340 100644 --- a/R/read_importentero.R +++ b/R/read_importentero.R @@ -1,12 +1,8 @@ #' Download Enterococcus data from the Water Quality Portal #' -#' @param args a list of arguments to pass to Water Quality Portal. See \code{examples} for detail on how to use. Must contain the following fields: -#' \describe{ -#' \item{\code{siteid}}{character, vector of stations} -#' \item{\code{characteristicName}}{character, vector to select 'Characteristic Names' from WQP} -#' \item{\code{startDateLo}}{date, start date for data to download} -#' \item{\code{startDateHi}}{date, end date for data to download} -#' } +#' @param stas character, a vector of stations. If \code{NULL}, defaults to all stations in \code{\link{catchprecip}}. +#' @param startDate character, starting date of observations as YYYY-MM-DD +#' @param endDate character, ending date of observations as YYYY-MM-DD #' #' @details Retrieves Enterococcus sample data from selected stations and date range from the Water Quality Portal, \url{https://www.waterqualitydata.us} #' @@ -33,30 +29,35 @@ #' #' @examples #' \dontrun{ -#' # set up list of args +#' # stations to download #' stations <- c('21FLHILL_WQX-101', #' '21FLHILL_WQX-102', #' '21FLHILL_WQX-103') -#' entero_names <- c('Enterococci', -#' 'Enterococcus') -#' startDate <- as.Date('2023-01-01') -#' endDate <- as.Date('2023-02-01') -#' -#' args <- list( -#' siteid = stations, -#' characteristicName = entero_names, -#' startDateLo = format(startDate, '%m-%d-%Y'), -#' startDateHi = format(endDate, '%m-%d-%Y') -#' ) #' #' # download and read the data -#' entero_in <- read_importentero(args = args) +#' entero_in <- read_importentero(stas = stations, startDate = '2023-01-01', endDate = '2023-02-01') +#' #' head(entero_in) #' #' } +read_importentero <- function(stas = NULL, startDate, endDate){ + + # default to all stations if not specified + if(is.null(stas)) + stations <- unique(catchprecip$station) + + entero_names <- c('Enterococci', + 'Enterococcus') + startDate <- as.Date(startDate) + endDate <- as.Date(endDate) + + args <- list( + siteid = stations, + characteristicName = entero_names, + startDateLo = format(startDate, '%m-%d-%Y'), + startDateHi = format(endDate, '%m-%d-%Y') + ) -read_importentero <- function(args){ - # args should be a list of sites, characteristic names, and start/end dates # generate the parts # a weakness here is building the '&' into everything but siteid - diff --git a/data-raw/enterodata-raw.R b/data-raw/enterodata-raw.R index c195c223..d86a2c80 100644 --- a/data-raw/enterodata-raw.R +++ b/data-raw/enterodata-raw.R @@ -2,21 +2,7 @@ # although apparently data collection only started in 2000 at the earliest of these stations library(here) -stations <- unique(catchprecip$station) -entero_names <- c('Enterococci', - 'Enterococcus') -startDate <- as.Date('1995-01-01') -endDate <- as.Date('2023-12-31') - -args <- list( - siteid = stations, - characteristicName = entero_names, - startDateLo = format(startDate, '%m-%d-%Y'), - startDateHi = format(endDate, '%m-%d-%Y') -) -# date format has to be mm-dd-yyyy - -enterodata <- read_importentero(args = args) %>% +enterodata <- read_importentero(startDate = '1995-01-01', endDate = '2023-12-31') %>% dplyr::select(-qualifier, -LabComments) diff --git a/man/read_importentero.Rd b/man/read_importentero.Rd index bc70452a..80c78d72 100644 --- a/man/read_importentero.Rd +++ b/man/read_importentero.Rd @@ -4,16 +4,14 @@ \alias{read_importentero} \title{Download Enterococcus data from the Water Quality Portal} \usage{ -read_importentero(args) +read_importentero(stas = NULL, startDate, endDate) } \arguments{ -\item{args}{a list of arguments to pass to Water Quality Portal. See \code{examples} for detail on how to use. Must contain the following fields: -\describe{ - \item{\code{siteid}}{character, vector of stations} - \item{\code{characteristicName}}{character, vector to select 'Characteristic Names' from WQP} - \item{\code{startDateLo}}{date, start date for data to download} - \item{\code{startDateHi}}{date, end date for data to download} -}} +\item{stas}{character, a vector of stations. If \code{NULL}, defaults to all stations in \code{\link{catchprecip}}.} + +\item{startDate}{character, starting date of observations as YYYY-MM-DD} + +\item{endDate}{character, ending date of observations as YYYY-MM-DD} } \value{ a data frame containing one row for each sample. Columns returned are: @@ -42,24 +40,14 @@ Retrieves Enterococcus sample data from selected stations and date range from th } \examples{ \dontrun{ -# set up list of args +# stations to download stations <- c('21FLHILL_WQX-101', '21FLHILL_WQX-102', '21FLHILL_WQX-103') -entero_names <- c('Enterococci', - 'Enterococcus') -startDate <- as.Date('2023-01-01') -endDate <- as.Date('2023-02-01') - -args <- list( - siteid = stations, - characteristicName = entero_names, - startDateLo = format(startDate, '\%m-\%d-\%Y'), - startDateHi = format(endDate, '\%m-\%d-\%Y') -) # download and read the data -entero_in <- read_importentero(args = args) +entero_in <- read_importentero(stas = stations, startDate = '2023-01-01', endDate = '2023-02-01') + head(entero_in) } diff --git a/tests/testthat/test-read_importentero.R b/tests/testthat/test-read_importentero.R index 93f4f2d1..f54aceb6 100644 --- a/tests/testthat/test-read_importentero.R +++ b/tests/testthat/test-read_importentero.R @@ -25,16 +25,10 @@ test_that("read_importentero works correctly", { mock_read_csv <- mock(return_value = mock_data) stub(read_importentero, "read.csv", mock_read_csv) - # Prepare arguments - args <- list( - siteid = c("21FLHILL_WQX-101", "21FLHILL_WQX-102"), - characteristicName = c("Enterococci", "Enterococcus"), - startDateLo = "01-01-2023", - startDateHi = "12-31-2023" - ) + stations <- c("21FLHILL_WQX-101", "21FLHILL_WQX-102") # Call the function with mocked dependencies - result <- read_importentero(args) + result <- read_importentero(stas = stations, startDate = "2023-01-01", endDate = "2023-12-31") # Define expected output expected_output <- data.frame(