Skip to content

Commit

Permalink
better error and warning messages for tabMWRacc type as individual an…
Browse files Browse the repository at this point in the history
…d invalid accchk entries #61
  • Loading branch information
fawda123 committed Sep 28, 2023
1 parent aa1b9a7 commit b94c95a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
32 changes: 27 additions & 5 deletions R/tabMWRacc.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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 %>%
Expand Down
22 changes: 18 additions & 4 deletions tests/testthat/test-tabMWRacc.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
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", {
chk <- resdat %>%
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'
)
)
})
Expand All @@ -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.')
})

0 comments on commit b94c95a

Please sign in to comment.