Skip to content

Commit

Permalink
add visR attrition diagram
Browse files Browse the repository at this point in the history
  • Loading branch information
ablack3 committed Dec 9, 2023
1 parent ae68b8f commit 2806779
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 3 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ Suggests:
lubridate,
tibble,
testthat (>= 3.0.0),
pool
pool,
visR
Enhances:
CirceR,
Capr
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ S3method(cohortSet,GeneratedCohortSet)
S3method(collect,cdm_reference)
S3method(dbplyr_edition,BigQueryConnection)
S3method(print,cdm_reference)
S3method(visr,omop_attrition)
export("%>%")
export(appendPermanent)
export(append_permanent)
Expand Down Expand Up @@ -124,4 +125,5 @@ importFrom(tidyselect,matches)
importFrom(tidyselect,starts_with)
importFrom(utils,download.file)
importFrom(utils,head)
importFrom(visR,visr)
importMethodsFrom(DBI,dbConnect)
6 changes: 4 additions & 2 deletions R/generateCohortSet.R
Original file line number Diff line number Diff line change
Expand Up @@ -813,9 +813,11 @@ cohort_attrition <- cohortAttrition
#' @export
cohortAttrition.GeneratedCohortSet <- function(x) {
if (is.null(attr(x, "cohort_attrition"))) {
NULL
return(invisible(NULL))
} else {
dplyr::collect(attr(x, "cohort_attrition"))
a <- dplyr::collect(attr(x, "cohort_attrition"))
class(a) <- c("omop_attrition", "attrition", class(a))
return(a)
}
}

Expand Down
43 changes: 43 additions & 0 deletions R/visR.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

#' Create an attrition diagram from a generated cohort set
#'
#' @param x A GeneratedCohortSet object
#' @param ... Not used
#'
#' `r lifecycle::badge("experimental")`
#'
#' @return No return value. This function will create one attrition plot for each generated cohort.
#' @export
#' @importFrom visR visr
#'
#' @examples
#' \dontrun{
#' library(CDMConnector)
#' library(dplyr)
#'
#' con <- DBI::dbConnect(duckdb::duckdb(), eunomia_dir())
#' cdm <- cdm_from_con(con, "main", "main")
#' cohort_set <- read_cohort_set(system.file("cohorts2", package = "CDMConnector"))
#' cdm <- generate_cohort_set(cdm, cohort_set, name = "cohort", overwrite = T)
#'
#' cohort_attrition(cdm$cohort) %>%
#' dplyr::filter(cohort_definition_id == 3) %>%
#' visR::visr()
#'
#' DBI::dbDisconnect(con, shutdown = TRUE)
#' }
#'
visr.omop_attrition <- function(x, ...) {
if (!rlang::is_installed("visR")) cli::cli_abort("Please install the visR package.")

ids <- unique(x$cohort_definition_id)
if (length(ids) > 1) {
cli::cli_abort("Cannot create attrition diagram because your cohort set has more than one cohort ID ({paste(ids, collapse = ', ')}). \nFirst make sure there is only one cohort_definition_id in the cohort set.")
}
checkmate::assertIntegerish(ids, len = 1, lower = 1, any.missing = FALSE)

x <- x %>%
dplyr::select(Criteria = reason, `Remaining N` = number_subjects)

NextMethod(x)
}
39 changes: 39 additions & 0 deletions man/visr.omop_attrition.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions tests/testthat/test-visR.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

test_that("visR attrition diagram works", {
con <- DBI::dbConnect(duckdb::duckdb(), eunomia_dir())
cdm <- cdm_from_con(con, "main", "main")
cohort_set <- read_cohort_set(system.file("cohorts2", package = "CDMConnector"))
cdm <- generate_cohort_set(cdm, cohort_set, name = "cohort", overwrite = T)

expect_error({
cohort_attrition(cdm$cohort) %>%
visR::visr()
})

expect_no_error({
cohort_attrition(cdm$cohort) %>%
dplyr::filter(cohort_definition_id == 3) %>%
visR::visr()
})

DBI::dbDisconnect(con, shutdown = TRUE)
})



0 comments on commit 2806779

Please sign in to comment.