Skip to content

Commit

Permalink
anlz_fibmatrix returns warning if some insufficient stations, error i…
Browse files Browse the repository at this point in the history
…f no sufficient stations
  • Loading branch information
fawda123 committed Aug 11, 2024
1 parent c17ab3d commit b9844e6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
14 changes: 10 additions & 4 deletions R/anlz_fibmatrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,21 @@ anlz_fibmatrix <- function(fibdata,
if(any(!chk))
stop('Station(s) not found in fibdata: ', paste(stas[!chk], collapse = ', '))

# check stations include enough years
chk <- !stas %in% stasval
if(any(chk))
stop('Stations with insufficient data for lagyr: ', paste(stas[chk], collapse = ', '))

# check if some stations valid for lagyr
if(sum(chk) > 0 & sum(chk) < length(chk))
warning('Stations with insufficient data for lagyr: ', paste(stas[chk], collapse = ', '))

# check if all stations invalid for lagyr
if(sum(chk) == length(chk)){
stop('No stations with sufficient data for lagyr')
}

# get geomean, proportion of sites > 400 cfu / 100mL, and prob of exceedence
# handles lagged calculations
dat <- fibdata %>%
dplyr::filter(station %in% stas) %>%
dplyr::filter(station %in% stasval) %>%
dplyr::filter(yr >= (yrrng[1] - (lagyr - 1)) & yr <= yrrng[2]) %>%
dplyr::filter(!is.na(indic) | indic < 0) %>%
summarise(
Expand Down
9 changes: 7 additions & 2 deletions tests/testthat/test-anlz_fibmatrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ test_that("Checking anlz_fibmatrix station error", {
fixed = T)
})

test_that("Checking anlz_fibmatrix station error insufficient data", {
expect_error(anlz_fibmatrix(fibdata, indic = 'fcolif', stas = '616'), regexp = 'Stations with insufficient data for lagyr: 616',
test_that("Checking anlz_fibmatrix station warning some insufficient data", {
expect_warning(anlz_fibmatrix(fibdata, indic = 'fcolif', stas = c('115', '616')), regexp = 'Stations with insufficient data for lagyr: 616',
fixed = T)
})

test_that("Checking anlz_fibmatrix station error all insufficient data", {
expect_warning(anlz_fibmatrix(fibdata, indic = 'fcolif', stas = '616'), regexp = 'No stations with sufficient data for lagyr',
fixed = T)
})

# Example data
fibdata <- data.frame(
yr = rep(2000:2005, each = 3),
Expand Down

0 comments on commit b9844e6

Please sign in to comment.