From b94c95a8b00ff033ff66a270c76d038a6abef272 Mon Sep 17 00:00:00 2001 From: fawda123 Date: Thu, 28 Sep 2023 07:51:57 -0400 Subject: [PATCH] better error and warning messages for tabMWRacc type as individual and invalid accchk entries #61 --- R/tabMWRacc.R | 32 +++++++++++++++++++++++++++----- tests/testthat/test-tabMWRacc.R | 22 ++++++++++++++++++---- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/R/tabMWRacc.R b/R/tabMWRacc.R index d32497b7..f427b55c 100644 --- a/R/tabMWRacc.R +++ b/R/tabMWRacc.R @@ -48,6 +48,8 @@ tabMWRacc <- function(res = NULL, acc = NULL, frecom = NULL, fset = NULL, runchk utilMWRinputcheck(mget(ls())) + accchkall <- c('Field Blanks', 'Lab Blanks', 'Field Duplicates', 'Lab Duplicates', 'Lab Spikes / Instrument Checks') + type <- match.arg(type) # table theme @@ -56,9 +58,8 @@ tabMWRacc <- function(res = NULL, acc = NULL, frecom = NULL, fset = NULL, runchk flextable::autofit(x) } - if(type %in% c('summary', 'percent')){ - accchk <- c('Field Blanks', 'Lab Blanks', 'Field Duplicates', 'Lab Duplicates', 'Lab Spikes / Instrument Checks') - } + if(type %in% c('summary', 'percent')) + accchk <- accchkall # get accuracy summary accsum <- qcMWRacc(res = res, acc = acc, frecom = frecom, fset = fset, runchk = runchk, warn = warn, accchk = accchk, suffix = suffix) @@ -68,13 +69,34 @@ tabMWRacc <- function(res = NULL, acc = NULL, frecom = NULL, fset = NULL, runchk if(length(accchk) != 1) stop('accchk must have only one entry for type = "individual"') + if(!accchk %in% accchkall) + stop('accchk must be one of ', paste(accchkall, collapse = ', ')) + totab <- accsum[[1]] # warning if no data to use for table if(is.null(totab)){ - if(warn) - warning(paste('No data to check for', accchk), call. = FALSE) + + # identify valid entries for accchk + chk <- qcMWRacc(res = res, acc = acc, frecom = frecom, fset = fset, runchk = F, warn = F, + accchk = accchkall, suffix = suffix + ) %>% + lapply(is.null) %>% + unlist() + + # check if accsum completely empty + if(all(chk)) + stop('No QC records or reference values for parameters with defined DQOs. Cannot create QC tables.', call. = FALSE) + + # warning for invalid accchk entry, indication of valid acchk entries + if(warn){ + valent <- paste(names(chk)[!chk], collapse = ', ') + msg <- paste0('No data to check for ', accchk, ', valid accchk entries include ', valent) + warning(msg, call. = FALSE) + } + return(NULL) + } totab <- totab %>% diff --git a/tests/testthat/test-tabMWRacc.R b/tests/testthat/test-tabMWRacc.R index bd282baa..b87d0860 100644 --- a/tests/testthat/test-tabMWRacc.R +++ b/tests/testthat/test-tabMWRacc.R @@ -1,5 +1,11 @@ test_that("Checking output format type error if individual and more than one table type", { - expect_error(tabMWRacc(resdat, accdat, frecomdat, runchk = F, warn = F, type = 'individual')) + expect_error(tabMWRacc(resdat, accdat, frecomdat, runchk = F, warn = F, type = 'individual'), + 'accchk must have only one entry for type = "individual"') +}) + +test_that("Checking output format type error if individual and incorrect accchk", { + expect_error(tabMWRacc(resdat, accdat, frecomdat, runchk = F, warn = F, type = 'individual', accchk = 'asdf'), + 'accchk must be one of Field Blanks, Lab Blanks, Field Duplicates, Lab Duplicates, Lab Spikes / Instrument Checks') }) test_that("Checking output format type warning if individual and no table", { @@ -7,7 +13,8 @@ test_that("Checking output format type warning if individual and no table", { filter(!`Activity Type` == 'Quality Control Sample-Field Blank') expect_warning( expect_null( - tabMWRacc(chk, accdat, frecomdat, runchk = F, warn = T, type = 'individual', accchk = 'Field Blanks') + tabMWRacc(chk, accdat, frecomdat, runchk = F, warn = T, type = 'individual', accchk = 'Field Blanks'), + 'No data to check for Field Blanks, valid accchk entries include Lab Blanks, Field Duplicates, Lab Duplicates, Lab Spikes / Instrument Checks' ) ) }) @@ -33,9 +40,16 @@ test_that("Checking output warning if missing completeness data and format type expect_warning(tabMWRacc(respth, accpth, chk, runchk = F, warn = T, type = 'percent')) }) -test_that("Checking error if no QC records or reference values", { +test_that("Checking error if no QC records or reference values and type as percent", { + chk <- readMWRacc(accpth, runchk = F, warn = F) %>% + mutate_at(vars(matches('Duplicate|Blank|Accuracy')), function(x) NA) + + expect_error(tabMWRacc(respth, chk, frecompth, runchk = F, warn = T, type = 'percent'), 'No QC records or reference values for parameters with defined DQOs. Cannot create QC tables.') +}) + +test_that("Checking error if no QC records or reference values and type as individual", { chk <- readMWRacc(accpth, runchk = F, warn = F) %>% mutate_at(vars(matches('Duplicate|Blank|Accuracy')), function(x) NA) - expect_error(tabMWRacc(respth, chk, frecompth, runchk = F, warn = T, type = 'percent')) + expect_error(tabMWRacc(respth, chk, frecompth, runchk = F, warn = T, type = 'individual', accchk = 'Field Blanks'), 'No QC records or reference values for parameters with defined DQOs. Cannot create QC tables.') }) \ No newline at end of file