diff --git a/DESCRIPTION b/DESCRIPTION index a9666966f..c654af99c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: gsm Title: Good Statistical Monitoring -Version: 2.2.0.9999 +Version: 2.2.1 Authors@R: c( person("George", "Wu", , "george.wu@gilead.com", role = c("aut", "cre")), person("Jeremy", "Wildfire", , "jwildfire@gmail.com", role = "aut"), diff --git a/NEWS.md b/NEWS.md index af6f402fd..5d14bd69e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,14 @@ +# gsm v2.2.1 + +This patch release primarily addresses minor report updates bug fixes. Specifically: + +- Reduce size of flags over time table by only displaying the most recent 12 periods +- Return an object of class `function` from `GetStrFunctionIfNamespaced()` +- Generalize all `Flag_*()` functions to a single `Flag()` function +- Use `log4r` framework set up in v2.2.0 for `stopif()`/`stopifnot()` calls +- Add Subject Disposition field to Study Completion domain + + # gsm v2.2.0 This minor release prepares the gsm to be used as part of a more sophisticated pipeline, through the addition of diff --git a/R/Analyze_NormalApprox.R b/R/Analyze_NormalApprox.R index 0580889e8..a2ad88171 100644 --- a/R/Analyze_NormalApprox.R +++ b/R/Analyze_NormalApprox.R @@ -36,8 +36,7 @@ Analyze_NormalApprox <- function( dfTransformed, strType = "binary" ) { - - stop_if(cnd = !is.data.frame(dfTransformed), message ="dfTransformed is not a data.frame") + stop_if(cnd = !is.data.frame(dfTransformed), message = "dfTransformed is not a data.frame") stop_if( cnd = !all(c("GroupID", "GroupLevel", "Denominator", "Numerator", "Metric") %in% names(dfTransformed)), message = "One or more of these columns not found: GroupID, GroupLevel, Denominator, Numerator, Metric" diff --git a/R/Flag.R b/R/Flag.R index 7281733c1..a2fd9ffb3 100644 --- a/R/Flag.R +++ b/R/Flag.R @@ -46,11 +46,10 @@ Flag <- function( dfAnalyzed, strColumn = "Score", - vThreshold = c(-3,-2,2,3), - vFlag = c(-2,-1,0,1,2), - vFlagOrder = c(2,-2,1,-1,0) + vThreshold = c(-3, -2, 2, 3), + vFlag = c(-2, -1, 0, 1, 2), + vFlagOrder = c(2, -2, 1, -1, 0) ) { - stop_if(cnd = !is.data.frame(dfAnalyzed), message = "dfAnalyzed is not a data frame") stop_if(cnd = !is.character(strColumn), message = "strColumn is not character") stop_if(cnd = !is.numeric(vThreshold), message = "vThreshold is not numeric") @@ -59,23 +58,25 @@ Flag <- function( stop_if(cnd = length(strColumn) != 1, message = "strColumn must be length of 1") stop_if(cnd = !(strColumn %in% names(dfAnalyzed)), message = "strColumn not found in dfAnalyzed") stop_if(cnd = !is.numeric(vFlag), message = "vFlag must be numeric") - stop_if(cnd = length(vFlag) != length(vThreshold)+1, message = "Improper number of Flag values provided") + stop_if(cnd = length(vFlag) != length(vThreshold) + 1, message = "Improper number of Flag values provided") stop_if(cnd = !is.numeric(vFlagOrder) & !is.null(vFlagOrder), message = "vFlagOrder must be numeric or NULL") dfFlagged <- dfAnalyzed # generate flag values for dfAnalyzed[strColumn] based on vThresold and vFlag dfFlagged$Flag <- cut( - dfFlagged[[strColumn]], - breaks = c(-Inf, vThreshold, Inf), - labels = vFlag, - right = FALSE - ) %>% as.character() %>% as.numeric() #Parse from factor to numeric + dfFlagged[[strColumn]], + breaks = c(-Inf, vThreshold, Inf), + labels = vFlag, + right = FALSE + ) %>% + as.character() %>% + as.numeric() # Parse from factor to numeric # Apply custom sort order using vFlagOrder - if(!is.null(vFlagOrder)){ - #all values in vFlag should be included in vFlagOrder - if(identical(sort(vFlag), sort(vFlagOrder))){ + if (!is.null(vFlagOrder)) { + # all values in vFlag should be included in vFlagOrder + if (identical(sort(vFlag), sort(vFlagOrder))) { dfFlagged <- dfFlagged %>% arrange(match(.data$Flag, vFlagOrder)) LogMessage( level = "info", @@ -87,7 +88,7 @@ Flag <- function( level = "info", message = "Mismatch in vFlagOrder and vFlag values. Aborting Sort and returning unsorted data.", cli_detail = "alert_info" - ) + ) } } diff --git a/R/Report_FormatFlag.R b/R/Report_FormatFlag.R index 6fcd489fc..a908c4a0f 100644 --- a/R/Report_FormatFlag.R +++ b/R/Report_FormatFlag.R @@ -27,4 +27,3 @@ Report_FormatFlag <- function(flag_value, title = NULL) { fa_titled <- function(name, fill, title) { fontawesome::fa(name, fill = fill, title = title) } - diff --git a/R/Summarize.R b/R/Summarize.R index 5ae12afff..e9cab5b9e 100644 --- a/R/Summarize.R +++ b/R/Summarize.R @@ -24,7 +24,7 @@ #' when associated with a workflow. #' #' @examples -#' +#' #' dfTransformed <- Transform_Rate(analyticsInput) #' dfAnalyzed <- Analyze_NormalApprox(dfTransformed) #' dfFlagged <- Flag(dfAnalyzed) @@ -37,8 +37,10 @@ Summarize <- function( nMinDenominator = NULL ) { stop_if(cnd = !is.data.frame(dfFlagged), message = "dfFlagged is not a data frame") - stop_if(cnd = !all(c("GroupID", "GroupLevel", "Flag", "Score") %in% names(dfFlagged)), - message = "One or more of these columns: GroupID, GroupLevel, Flag, Score, not found in dfFlagged" ) + stop_if( + cnd = !all(c("GroupID", "GroupLevel", "Flag", "Score") %in% names(dfFlagged)), + message = "One or more of these columns: GroupID, GroupLevel, Flag, Score, not found in dfFlagged" + ) if (!("Numerator" %in% colnames(dfFlagged))) { dfFlagged$Numerator <- NA diff --git a/R/Visualize_Score.R b/R/Visualize_Score.R index 0103ff8d5..0493895e2 100644 --- a/R/Visualize_Score.R +++ b/R/Visualize_Score.R @@ -39,11 +39,11 @@ Visualize_Score <- function( bFlagFilter = FALSE, strTitle = "" ) { - stop_if(cnd = !is.character(strTitle), message = "strTitle must be character" ) + stop_if(cnd = !is.character(strTitle), message = "strTitle must be character") stop_if(cnd = !is.logical(bFlagFilter), message = "bFlagFilter must be logical") stop_if(cnd = !is.data.frame(dfResults), message = "dfResults must be a data.frame") - stop_if(cnd = !(length(strType) == 1), message = "strType must be length 1" ) - stop_if(cnd = !(strType %in% c("Metric", "Score")), message = "strType must be 'Metric' or 'Score'" ) + stop_if(cnd = !(length(strType) == 1), message = "strType must be length 1") + stop_if(cnd = !(strType %in% c("Metric", "Score")), message = "strType must be 'Metric' or 'Score'") dfResults$FlagAbs <- abs(dfResults$Flag) flagBreaks <- as.character(unique(sort(dfResults$FlagAbs))) diff --git a/R/Widget_FlagOverTime.R b/R/Widget_FlagOverTime.R index 2ebf5fa71..20ad115ad 100644 --- a/R/Widget_FlagOverTime.R +++ b/R/Widget_FlagOverTime.R @@ -48,7 +48,7 @@ Widget_FlagOverTime <- function( bDebug = FALSE ) { stop_if(cnd = !is.data.frame(dfResults), message = "dfResults is not a data.frame") - stop_if(cnd = !is.data.frame(dfMetrics),"dfMetrics is not a data.frame") + stop_if(cnd = !is.data.frame(dfMetrics), "dfMetrics is not a data.frame") stop_if(cnd = !is.character(strGroupLevel), "strGroupLevel is not a character") stop_if(cnd = !is.character(strFootnote) && !is.null(strFootnote), "strFootnote is not a character or NULL") stop_if(cnd = !is.logical(bDebug), "bDebug is not a logical") diff --git a/R/Widget_GroupOverview.R b/R/Widget_GroupOverview.R index 15a6783b6..e1840e167 100644 --- a/R/Widget_GroupOverview.R +++ b/R/Widget_GroupOverview.R @@ -51,10 +51,10 @@ Widget_GroupOverview <- function( strGroupLabelKey = "InvestigatorLastName", bDebug = FALSE ) { - stop_if(cnd = !is.data.frame(dfResults),"dfResults is not a data.frame") - stop_if(cnd = !is.data.frame(dfMetrics),"dfMetrics is not a data.frame") - stop_if(cnd = !is.data.frame(dfGroups),"dfGroups is not a data.frame") - stop_if(cnd = !is.character(strGroupSubset),"strGroupSubset is not a character") + stop_if(cnd = !is.data.frame(dfResults), "dfResults is not a data.frame") + stop_if(cnd = !is.data.frame(dfMetrics), "dfMetrics is not a data.frame") + stop_if(cnd = !is.data.frame(dfGroups), "dfGroups is not a data.frame") + stop_if(cnd = !is.character(strGroupSubset), "strGroupSubset is not a character") stop_if(cnd = !is.character(strGroupLabelKey) && !is.null(strGroupLabelKey), "strGroupLabelKey is not a character or NULL") stop_if(cnd = !is.logical(bDebug), "bDebug is not a logical") diff --git a/R/Widget_ScatterPlot.R b/R/Widget_ScatterPlot.R index 1d5fbd501..c50422a62 100644 --- a/R/Widget_ScatterPlot.R +++ b/R/Widget_ScatterPlot.R @@ -40,12 +40,12 @@ Widget_ScatterPlot <- function( strShinyGroupSelectID = "GroupID", bDebug = FALSE ) { - stop_if(cnd = !is.data.frame(dfResults),"dfResults is not a data.frame") + stop_if(cnd = !is.data.frame(dfResults), "dfResults is not a data.frame") stop_if(cnd = !(is.null(lMetric) || (is.list(lMetric) && !is.data.frame(lMetric))), "lMetric must be a list, but not a data.frame") - stop_if(cnd = !(is.null(dfGroups) || is.data.frame(dfGroups)),"dfGroups is not a data.frame") - stop_if(cnd = !(is.null(dfBounds) || is.data.frame(dfBounds)),"dfBounds is not a data.frame") - stop_if(cnd = !is.logical(bAddGroupSelect),"bAddGroupSelect is not a logical") - stop_if(cnd = !is.character(strShinyGroupSelectID),"strShinyGroupSelectID is not a character") + stop_if(cnd = !(is.null(dfGroups) || is.data.frame(dfGroups)), "dfGroups is not a data.frame") + stop_if(cnd = !(is.null(dfBounds) || is.data.frame(dfBounds)), "dfBounds is not a data.frame") + stop_if(cnd = !is.logical(bAddGroupSelect), "bAddGroupSelect is not a logical") + stop_if(cnd = !is.character(strShinyGroupSelectID), "strShinyGroupSelectID is not a character") stop_if(cnd = !is.logical(bDebug), "bDebug is not a logical") # define widget inputs diff --git a/R/Widget_TimeSeries.R b/R/Widget_TimeSeries.R index 183d07102..cbcc90dcc 100644 --- a/R/Widget_TimeSeries.R +++ b/R/Widget_TimeSeries.R @@ -40,15 +40,14 @@ Widget_TimeSeries <- function( strShinyGroupSelectID = "GroupID", bDebug = FALSE ) { - - stop_if(cnd = !is.data.frame(dfResults),"dfResults is not a data.frame") + stop_if(cnd = !is.data.frame(dfResults), "dfResults is not a data.frame") stop_if(cnd = !(is.null(lMetric) || (is.list(lMetric) && !is.data.frame(lMetric))), "lMetric must be a list, but not a data.frame") - stop_if(cnd = !(is.null(dfGroups) || is.data.frame(dfGroups)),"dfGroups is not a data.frame") - stop_if(cnd = length(strOutcome) != 1,"strOutcome must be length 1") - stop_if(cnd = !is.character(strOutcome),"strOutcome is not a character") - stop_if(cnd = !is.logical(bAddGroupSelect),"bAddGroupSelect is not a logical") - stop_if(cnd = !is.character(strShinyGroupSelectID),"strShinyGroupSelectID is not a character") - stop_if(cnd = !is.logical(bDebug), "bDebug is not a logical" ) + stop_if(cnd = !(is.null(dfGroups) || is.data.frame(dfGroups)), "dfGroups is not a data.frame") + stop_if(cnd = length(strOutcome) != 1, "strOutcome must be length 1") + stop_if(cnd = !is.character(strOutcome), "strOutcome is not a character") + stop_if(cnd = !is.logical(bAddGroupSelect), "bAddGroupSelect is not a logical") + stop_if(cnd = !is.character(strShinyGroupSelectID), "strShinyGroupSelectID is not a character") + stop_if(cnd = !is.logical(bDebug), "bDebug is not a logical") # Parse `vThreshold` from comma-delimited character string to numeric vector. if (!is.null(vThreshold)) { diff --git a/R/util-CalculatePercentage.R b/R/util-CalculatePercentage.R index 758f2b2e1..2b7b66d04 100644 --- a/R/util-CalculatePercentage.R +++ b/R/util-CalculatePercentage.R @@ -3,7 +3,7 @@ #' @description #' #' Used to create percentage values on enrollment data, such as site activation and -#' and participantment enrollment, and pasting together an appropriate n/N (xx.x%) value +#' and participant enrollment, and pasting together an appropriate n/N (xx.x%) value #' as well. #' #' @param data The input dataframe diff --git a/R/util-Ingest.R b/R/util-Ingest.R index 5c9889bb7..dcde395b3 100644 --- a/R/util-Ingest.R +++ b/R/util-Ingest.R @@ -35,7 +35,7 @@ #' @export Ingest <- function(lSourceData, lSpec, strDomain = "Raw") { - stop_if(!is.list(lSourceData),"[ lSourceData ] must be a list.") + stop_if(!is.list(lSourceData), "[ lSourceData ] must be a list.") stop_if(!is.list(lSpec), "[ lSpec ] must be a list.") # If there is a domain (specificed with and underscore) in lSourceData/lSpec names, remove it diff --git a/R/util-MakeBounds.R b/R/util-MakeBounds.R index 724e230d5..79f4b99ea 100644 --- a/R/util-MakeBounds.R +++ b/R/util-MakeBounds.R @@ -66,7 +66,7 @@ MakeBounds <- function( vThreshold <- ParseThreshold(strThreshold = lMetric$Threshold) if (!is.null(lMetric$AnalysisType) && tolower(unique(lMetric$AnalysisType)) %in% c("identity")) { - dfBounds <- NULL + dfBounds <- NULL } else if (!is.null(lMetric$AnalysisType) && tolower(unique(lMetric$AnalysisType)) %in% c("poisson")) { dfBounds <- Analyze_Poisson_PredictBounds( diff --git a/R/util-MakeWorkflowList.R b/R/util-MakeWorkflowList.R index 0596b5f46..d033e4782 100644 --- a/R/util-MakeWorkflowList.R +++ b/R/util-MakeWorkflowList.R @@ -33,7 +33,7 @@ MakeWorkflowList <- function( if (length(strPackage)) { path <- system.file(strPath, package = strPackage) } - stop_if(!dir.exists(path), "[ strPath ] must exist." ) + stop_if(!dir.exists(path), "[ strPath ] must exist.") path <- tools::file_path_as_absolute(path) # list all files to loop through to build the workflow list. diff --git a/R/util-Report.R b/R/util-Report.R index b936ed14b..5747034c7 100644 --- a/R/util-Report.R +++ b/R/util-Report.R @@ -64,33 +64,33 @@ FilterByLatestSnapshotDate <- function(df, strSnapshotDate = NULL) { #' @export FilterByFlags <- function( - dfResults, - bCurrentlyFlagged = FALSE + dfResults, + bCurrentlyFlagged = FALSE ) { dfResultsFlagged <- dfResults %>% group_by(.data$GroupID, .data$MetricID) %>% mutate( - flagsum = sum(abs(.data$Flag), na.rm = TRUE), - flaglatest = Flag[SnapshotDate == max(SnapshotDate)] + flagsum = sum(abs(.data$Flag), na.rm = TRUE), + flaglatest = Flag[SnapshotDate == max(SnapshotDate)] ) %>% ungroup() %>% filter( .data$flagsum > 0 ) - if (bCurrentlyFlagged) { - dfResultsFlagged <- dfResultsFlagged %>% - filter( - .data$flaglatest != 0 - ) - } - + if (bCurrentlyFlagged) { dfResultsFlagged <- dfResultsFlagged %>% - select(-all_of(c( - 'flagsum', 'flaglatest' - ))) + filter( + .data$flaglatest != 0 + ) + } + + dfResultsFlagged <- dfResultsFlagged %>% + select(-all_of(c( + "flagsum", "flaglatest" + ))) - return(dfResultsFlagged) + return(dfResultsFlagged) } add_Groups_metadata <- function( diff --git a/man/CalculatePercentage.Rd b/man/CalculatePercentage.Rd index b09ee9d03..30b93acc4 100644 --- a/man/CalculatePercentage.Rd +++ b/man/CalculatePercentage.Rd @@ -28,6 +28,6 @@ A data frame containing two additional columns for the precentage value and asso } \description{ Used to create percentage values on enrollment data, such as site activation and -and participantment enrollment, and pasting together an appropriate n/N (xx.x\%) value +and participant enrollment, and pasting together an appropriate n/N (xx.x\%) value as well. } diff --git a/tests/testthat/test-Flag.R b/tests/testthat/test-Flag.R index e777e5838..3f4c1b0dc 100644 --- a/tests/testthat/test-Flag.R +++ b/tests/testthat/test-Flag.R @@ -1,83 +1,82 @@ test_that("Flag function works correctly with z-score data", { - dfAnalyzed <- data.frame( - GroupID = 1:12, - Score = c(-4, -3.1,-3,-2.9, -2.1, -2,-1.9, 0, 2, 2.9, 3, 3.1) - ) - - #unsorted - dfFlagged <- Flag(dfAnalyzed, vFlagOrder = NULL) - expect_equal(dfFlagged$Flag, c(-2, -2, -1, -1, -1, 0, 0, 0, 1, 1, 2, 2)) - - # sorted - dfFlagged <- Flag(dfAnalyzed) - expect_equal(dfFlagged$Flag, c(2, 2, -2, -2, 1, 1, -1, -1, -1, 0, 0, 0)) - - # Test with custom thresholds and flags - dfFlagged <- Flag(dfAnalyzed, vThreshold = c(-2,2), vFlag = c(-1,0,1), vFlagOrder = NULL) - expect_equal(dfFlagged$Flag, c(-1, -1, -1, -1, -1, 0, 0, 0, 1, 1, 1, 1)) - - # Test Alias - dfFlagged <- Flag_NormalApprox(dfAnalyzed, vFlagOrder=NULL) - expect_equal(dfFlagged$Flag, c(-2, -2, -1, -1, -1, 0, 0, 0, 1, 1, 2, 2)) + dfAnalyzed <- data.frame( + GroupID = 1:12, + Score = c(-4, -3.1, -3, -2.9, -2.1, -2, -1.9, 0, 2, 2.9, 3, 3.1) + ) + + # unsorted + dfFlagged <- Flag(dfAnalyzed, vFlagOrder = NULL) + expect_equal(dfFlagged$Flag, c(-2, -2, -1, -1, -1, 0, 0, 0, 1, 1, 2, 2)) + + # sorted + dfFlagged <- Flag(dfAnalyzed) + expect_equal(dfFlagged$Flag, c(2, 2, -2, -2, 1, 1, -1, -1, -1, 0, 0, 0)) + + # Test with custom thresholds and flags + dfFlagged <- Flag(dfAnalyzed, vThreshold = c(-2, 2), vFlag = c(-1, 0, 1), vFlagOrder = NULL) + expect_equal(dfFlagged$Flag, c(-1, -1, -1, -1, -1, 0, 0, 0, 1, 1, 1, 1)) + + # Test Alias + dfFlagged <- Flag_NormalApprox(dfAnalyzed, vFlagOrder = NULL) + expect_equal(dfFlagged$Flag, c(-2, -2, -1, -1, -1, 0, 0, 0, 1, 1, 2, 2)) }) test_that("Flag function works correctly with rate data", { - # Test with rate data - dfAnalyzed_Rate <- data.frame( - GroupID = 1:9, - Score = c(0.1, 0.2, 0.5, 0.6, 0.8, 0.85, 0.86, 0.9, 0.99) - ) - dfFlagged_Rate <- Flag_NormalApprox(dfAnalyzed_Rate, vFlag=c(2,1,0), vThreshold = c(0.85, 0.9)) - expect_equal(dfFlagged_Rate$Flag, c(2,2,2,2,2,1,1,0,0)) - - dfFlagged_Rate <- Flag_NormalApprox(dfAnalyzed_Rate, vFlag=c(2,1,0), vThreshold = c(0.85, 0.9), vFlagOrder=c(0,1,2)) - expect_equal(dfFlagged_Rate$Flag, c(0,0,1,1,2,2,2,2,2)) - + # Test with rate data + dfAnalyzed_Rate <- data.frame( + GroupID = 1:9, + Score = c(0.1, 0.2, 0.5, 0.6, 0.8, 0.85, 0.86, 0.9, 0.99) + ) + dfFlagged_Rate <- Flag_NormalApprox(dfAnalyzed_Rate, vFlag = c(2, 1, 0), vThreshold = c(0.85, 0.9)) + expect_equal(dfFlagged_Rate$Flag, c(2, 2, 2, 2, 2, 1, 1, 0, 0)) + + dfFlagged_Rate <- Flag_NormalApprox(dfAnalyzed_Rate, vFlag = c(2, 1, 0), vThreshold = c(0.85, 0.9), vFlagOrder = c(0, 1, 2)) + expect_equal(dfFlagged_Rate$Flag, c(0, 0, 1, 1, 2, 2, 2, 2, 2)) }) test_that("Flag function works correctly with poisson data", { - # Test with Poisson Data - dfAnalyzedCustom <- tibble::tribble( - ~GroupID, ~Numerator, ~Denominator, ~Metric, ~Score, ~PredictedCount, - "166", 5L, 857L, 0.0058343057176196, -11, 5.12722560489132, - "76", 2L, 13L, 0.153846153846154, -6, 2.00753825876477, - "86", 5L, 678L, 0.00737463126843658, 6, 4.86523613634436, - "80", 5L, 678L, 0.00737463126843658, 11, 4.86523613634436 - ) - - dfFlagged <- Flag_Poisson(dfAnalyzedCustom, vThreshold = c(-10, -5, 5, 10)) - expect_equal(dfFlagged$Flag, c(2, -2,1, -1)) - expect_equal(dfFlagged$GroupID, c("80","166", "86", "76")) + # Test with Poisson Data + dfAnalyzedCustom <- tibble::tribble( + ~GroupID, ~Numerator, ~Denominator, ~Metric, ~Score, ~PredictedCount, + "166", 5L, 857L, 0.0058343057176196, -11, 5.12722560489132, + "76", 2L, 13L, 0.153846153846154, -6, 2.00753825876477, + "86", 5L, 678L, 0.00737463126843658, 6, 4.86523613634436, + "80", 5L, 678L, 0.00737463126843658, 11, 4.86523613634436 + ) + + dfFlagged <- Flag_Poisson(dfAnalyzedCustom, vThreshold = c(-10, -5, 5, 10)) + expect_equal(dfFlagged$Flag, c(2, -2, 1, -1)) + expect_equal(dfFlagged$GroupID, c("80", "166", "86", "76")) }) test_that("Flag function works correctly with NA data", { - dfAnalyzed_NA <- data.frame( - GroupID = 1:7, - Score = c(-4, -1, 0, NA, 2, 5, NA) - ) - dfFlagged_NA <- Flag(dfAnalyzed_NA, vFlagOrder = NULL) - expect_equal(dfFlagged_NA$Flag, c(-2, 0, 0, NA, 1, 2, NA)) + dfAnalyzed_NA <- data.frame( + GroupID = 1:7, + Score = c(-4, -1, 0, NA, 2, 5, NA) + ) + dfFlagged_NA <- Flag(dfAnalyzed_NA, vFlagOrder = NULL) + expect_equal(dfFlagged_NA$Flag, c(-2, 0, 0, NA, 1, 2, NA)) }) test_that("errors working as expected", { - dfAnalyzed <- data.frame( - GroupID = 1:12, - Score = c(-4, -3.1,-3,-2.9, -2.1, -2,-1.9, 0, 2, 2.9, 3, 3.1) - ) - # Test with missing strColumn - expect_error(Flag(dfAnalyzed, strColumn = "MissingColumn"), "strColumn not found in dfAnalyzed") + dfAnalyzed <- data.frame( + GroupID = 1:12, + Score = c(-4, -3.1, -3, -2.9, -2.1, -2, -1.9, 0, 2, 2.9, 3, 3.1) + ) + # Test with missing strColumn + expect_error(Flag(dfAnalyzed, strColumn = "MissingColumn"), "strColumn not found in dfAnalyzed") - # Test with improper number of flag values - expect_error(Flag(dfAnalyzed, vThreshold = c(-2, 0, 2), vFlag = c(1,2,3)), "Improper number of Flag values provided") + # Test with improper number of flag values + expect_error(Flag(dfAnalyzed, vThreshold = c(-2, 0, 2), vFlag = c(1, 2, 3)), "Improper number of Flag values provided") - # Test with non-numeric vThreshold - expect_error(Flag(dfAnalyzed, vThreshold = c("a", "b", "c")), "vThreshold is not numeric") + # Test with non-numeric vThreshold + expect_error(Flag(dfAnalyzed, vThreshold = c("a", "b", "c")), "vThreshold is not numeric") - # Test with non-character strColumn - expect_error(Flag(dfAnalyzed, strColumn = 123), "strColumn is not character") + # Test with non-character strColumn + expect_error(Flag(dfAnalyzed, strColumn = 123), "strColumn is not character") - # Test with non-data frame dfAnalyzed - expect_error(Flag(list(SiteID = 1:10, Score = c(-4, -3, -2.5, -2, -1, 0, 1, 2, 2.5, 3))), "dfAnalyzed is not a data frame") + # Test with non-data frame dfAnalyzed + expect_error(Flag(list(SiteID = 1:10, Score = c(-4, -3, -2.5, -2, -1, 0, 1, 2, 2.5, 3))), "dfAnalyzed is not a data frame") }) diff --git a/tests/testthat/test-RunWorkflow.R b/tests/testthat/test-RunWorkflow.R index 8f2162c81..d89231162 100644 --- a/tests/testthat/test-RunWorkflow.R +++ b/tests/testthat/test-RunWorkflow.R @@ -116,7 +116,7 @@ lConfig <- list( purrr::imap( lWorkflow$spec, ~ { - input <- lConfig$Domains[[ .y ]] + input <- lConfig$Domains[[.y]] if (is.data.frame(input)) { data <- input @@ -128,7 +128,7 @@ lConfig <- list( cli::cli_abort("Invalid data source: {input}.") } - lData[[ .y ]] <<- (ApplySpec(data, .x)) + lData[[.y]] <<- (ApplySpec(data, .x)) } ) return(lData) @@ -264,5 +264,3 @@ test_that("RunWorkflow errors out if the data save method does not have expected "must include a function named .SaveData." ) }) - - diff --git a/tests/testthat/test-util-Report.R b/tests/testthat/test-util-Report.R index bdd3fc131..71841f500 100644 --- a/tests/testthat/test-util-Report.R +++ b/tests/testthat/test-util-Report.R @@ -1,59 +1,58 @@ -test_that('[ FilterByFlags ] returns group/metric combinations with a flag at any snapshot.', { - dfResultsFlaggedActual <- FilterByFlags(reportingResults) - - strRiskSignals <- reportingResults %>% - filter( - .data$Flag != 0 - ) %>% - mutate( - riskSignalID = paste(.data$GroupID, .data$MetricID, sep = "_") - ) %>% - distinct( - riskSignalID - ) %>% - pull( - riskSignalID - ) +test_that("[ FilterByFlags ] returns group/metric combinations with a flag at any snapshot.", { + dfResultsFlaggedActual <- FilterByFlags(reportingResults) + + strRiskSignals <- reportingResults %>% + filter( + .data$Flag != 0 + ) %>% + mutate( + riskSignalID = paste(.data$GroupID, .data$MetricID, sep = "_") + ) %>% + distinct( + riskSignalID + ) %>% + pull( + riskSignalID + ) - dfResultsFlaggedExpected <- reportingResults %>% - filter( - paste(.data$GroupID, .data$MetricID, sep = '_') %in% strRiskSignals - ) %>% - as_tibble() + dfResultsFlaggedExpected <- reportingResults %>% + filter( + paste(.data$GroupID, .data$MetricID, sep = "_") %in% strRiskSignals + ) %>% + as_tibble() - expect_equal( - dfResultsFlaggedActual, - dfResultsFlaggedExpected - ) + expect_equal( + dfResultsFlaggedActual, + dfResultsFlaggedExpected + ) }) -test_that('[ FilterByFlags ] returns group/metric combinations with a flag at most recent snapshot.', { - dfResultsFlaggedActual <- FilterByFlags(reportingResults, bCurrentlyFlagged = TRUE) - - strRiskSignals <- reportingResults %>% - FilterByLatestSnapshotDate() %>% - filter( - .data$Flag != 0 - ) %>% - mutate( - riskSignalID = paste(.data$GroupID, .data$MetricID, sep = "_") - ) %>% - distinct( - riskSignalID - ) %>% - pull( - riskSignalID - ) +test_that("[ FilterByFlags ] returns group/metric combinations with a flag at most recent snapshot.", { + dfResultsFlaggedActual <- FilterByFlags(reportingResults, bCurrentlyFlagged = TRUE) + + strRiskSignals <- reportingResults %>% + FilterByLatestSnapshotDate() %>% + filter( + .data$Flag != 0 + ) %>% + mutate( + riskSignalID = paste(.data$GroupID, .data$MetricID, sep = "_") + ) %>% + distinct( + riskSignalID + ) %>% + pull( + riskSignalID + ) - dfResultsFlaggedExpected <- reportingResults %>% - filter( - paste(.data$GroupID, .data$MetricID, sep = '_') %in% strRiskSignals - ) %>% - as_tibble() + dfResultsFlaggedExpected <- reportingResults %>% + filter( + paste(.data$GroupID, .data$MetricID, sep = "_") %in% strRiskSignals + ) %>% + as_tibble() - expect_equal( - dfResultsFlaggedActual, - dfResultsFlaggedExpected - ) + expect_equal( + dfResultsFlaggedActual, + dfResultsFlaggedExpected + ) }) - diff --git a/vignettes/DataReporting.Rmd b/vignettes/DataReporting.Rmd index ab26652f6..767a870da 100644 --- a/vignettes/DataReporting.Rmd +++ b/vignettes/DataReporting.Rmd @@ -38,7 +38,7 @@ These functions and workflows produce data frames, visualizations, metadata, and ![](data_model_detailed.png){width="100%"} -All of the functions to create the data frames in the reporting data model will run automatically and sequentially when a user specifies the metadata and data needed for the report, and calls upon the `RunWorkflow()` function on the yaml files in the `workflow/3_reporting` directory. To create a report, the output of the reporting yamls is fed into the yamls in the `workflow/4_modules` directoty to produce and html document with all charts and tables created in the reporting workflow. For a more detailed discussion of the yaml file and directory structure, see (`vignette("gsmExtensions")`). +All of the functions to create the data frames in the reporting data model will run automatically and sequentially when a user specifies the metadata and data needed for the report, and calls upon the `RunWorkflow()` function on the yaml files in the `workflow/3_reporting` directory. To create a report, the output of the reporting yamls is fed into the yamls in the `workflow/4_modules` directory to produce and html document with all charts and tables created in the reporting workflow. For a more detailed discussion of the yaml file and directory structure, see (`vignette("gsmExtensions")`). Each of the individual functions can also be run independently outside of a specified yaml workflow.