From b9844e66c2eb1e99ee7b0fb16ce3aa7f67de8d45 Mon Sep 17 00:00:00 2001 From: fawda123 Date: Sun, 11 Aug 2024 14:27:30 -0400 Subject: [PATCH] anlz_fibmatrix returns warning if some insufficient stations, error if no sufficient stations --- R/anlz_fibmatrix.R | 14 ++++++++++---- tests/testthat/test-anlz_fibmatrix.R | 9 +++++++-- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/R/anlz_fibmatrix.R b/R/anlz_fibmatrix.R index b871bc3e..858452af 100644 --- a/R/anlz_fibmatrix.R +++ b/R/anlz_fibmatrix.R @@ -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( diff --git a/tests/testthat/test-anlz_fibmatrix.R b/tests/testthat/test-anlz_fibmatrix.R index c52ac244..76ca1e4e 100644 --- a/tests/testthat/test-anlz_fibmatrix.R +++ b/tests/testthat/test-anlz_fibmatrix.R @@ -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),