Skip to content

Commit

Permalink
update arguments for read_importentero for easier input
Browse files Browse the repository at this point in the history
  • Loading branch information
fawda123 committed Aug 2, 2024
1 parent 3b05d38 commit a3a2726
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 66 deletions.
45 changes: 23 additions & 22 deletions R/read_importentero.R
Original file line number Diff line number Diff line change
@@ -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}
#'
Expand All @@ -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 -
Expand Down
16 changes: 1 addition & 15 deletions data-raw/enterodata-raw.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
30 changes: 9 additions & 21 deletions man/read_importentero.Rd

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

10 changes: 2 additions & 8 deletions tests/testthat/test-read_importentero.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit a3a2726

Please sign in to comment.