diff --git a/DESCRIPTION b/DESCRIPTION index 94d1c28..6bfbbba 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -45,6 +45,6 @@ License: Apache License VignetteBuilder: knitr URL: https://ohdsi.github.io/CohortGenerator/, https://github.com/OHDSI/CohortGenerator BugReports: https://github.com/OHDSI/CohortGenerator/issues -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.1 Encoding: UTF-8 Language: en-US diff --git a/R/CohortCount.R b/R/CohortCount.R index 3e44424..1035bf0 100644 --- a/R/CohortCount.R +++ b/R/CohortCount.R @@ -27,7 +27,10 @@ #' @template CohortTable #' #' @param cohortIds The cohort Id(s) used to reference the cohort in the cohort -#' table. If left empty, all cohorts in the table will be included. +#' table. If left empty and no `cohortDefinitionSet` argument is +#' specified, all cohorts in the table will be included. If +#' you specify the `cohortIds` AND `cohortDefinitionSet`, the counts will +#' reflect the `cohortIds` from the `cohortDefinitionSet`. #' #' @template CohortDefinitionSet #' @@ -67,6 +70,12 @@ getCohortCounts <- function(connectionDetails = NULL, delta <- Sys.time() - start ParallelLogger::logInfo(paste("Counting cohorts took", signif(delta, 3), attr(delta, "units"))) if (!is.null(cohortDefinitionSet)) { + # If the user has NOT specified a list of cohortIds + # to use to filter the cohortDefinitionSet, then + # extract the unique cohortIds + if (length(cohortIds) == 0) { + cohortIds <- cohortDefinitionSet$cohortId + } counts <- merge( x = counts, y = cohortDefinitionSet[cohortDefinitionSet$cohortId %in% cohortIds, ], diff --git a/man/getCohortCounts.Rd b/man/getCohortCounts.Rd index f8ad454..240f26a 100644 --- a/man/getCohortCounts.Rd +++ b/man/getCohortCounts.Rd @@ -33,7 +33,10 @@ this should include both the database and schema name, for example \item{cohortTable}{The name of the cohort table.} \item{cohortIds}{The cohort Id(s) used to reference the cohort in the cohort -table. If left empty, all cohorts in the table will be included.} +table. If left empty and no `cohortDefinitionSet` argument is +specified, all cohorts in the table will be included. If +you specify the `cohortIds` AND `cohortDefinitionSet`, the counts will +reflect the `cohortIds` from the `cohortDefinitionSet`.} \item{cohortDefinitionSet}{The \code{cohortDefinitionSet} argument must be a data frame with the following columns: \describe{ diff --git a/tests/testthat/test-CohortCount.R b/tests/testthat/test-CohortCount.R index 35d263a..84e1d63 100644 --- a/tests/testthat/test-CohortCount.R +++ b/tests/testthat/test-CohortCount.R @@ -119,5 +119,31 @@ test_that("Call getCohortCounts with a cohortDefinitionSet returns 0 counts for expect_true(testCohortCounts[testCohortCounts$cohortId == 100, "cohortSubjects"] == 0) }) +test_that("Call getCohortCounts with no cohortId specified and cohortDefinitionSet returns 0 counts for cohortId not in cohort table", { + cohortDefinitionSet <- getCohortDefinitionSet( + settingsFileName = "testdata/id/Cohorts.csv", + jsonFolder = "testdata/id/cohorts", + sqlFolder = "testdata/id/sql/sql_server", + packageName = "CohortGenerator", + verbose = TRUE + ) + + cohortDefinitionSet <- rbind( + cohortDefinitionSet, + cohortDefinitionSet[1, ] |> transform(atlasId = 100, cohortId = 100, cohortName = "not in cohort table", logicDescription = "not in cohort table") + ) + + testCohortCounts <- getCohortCounts( + connectionDetails = connectionDetails, + cohortDatabaseSchema = "main", + cohortTable = "cohort", + cohortDefinitionSet = cohortDefinitionSet + ) + + expect_true(nrow(testCohortCounts) == 4) + expect_true(testCohortCounts[testCohortCounts$cohortId == 100, "cohortEntries"] == 0) + expect_true(testCohortCounts[testCohortCounts$cohortId == 100, "cohortSubjects"] == 0) +}) + # Cleanup ------ rm(cohortCounts)