Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: DrugUtilisation
Title: Summarise Patient-Level Drug Utilisation in Data Mapped to the OMOP
Common Data Model
Version: 1.0.4
Version: 1.0.5
Authors@R: c(
person(
"Martí", "Català", email = "marti.catalasabate@ndorms.ox.ac.uk",
Expand Down Expand Up @@ -49,9 +49,11 @@ Description: Summarise patient-level drug utilisation cohorts using data mapped
License: Apache License (>= 2)
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
RoxygenNote: 7.3.3
Suggests:
bit64,
CDMConnector (>= 1.4.0),
CohortConstructor,
CohortSurvival,
covr,
DBI,
Expand All @@ -63,22 +65,23 @@ Suggests:
here,
knitr,
odbc,
omock,
rmarkdown,
RPostgres,
scales,
testthat (>= 3.1.5),
tibble,
visOmopResults (>= 1.0.0)
visOmopResults (>= 1.3.0)
Config/testthat/edition: 3
Imports:
CDMConnector (>= 1.4.0),
Imports:
cli,
clock,
CodelistGenerator (>= 3.1.0),
dplyr,
glue,
omopgenerics (>= 1.0.0),
PatientProfiles (>= 1.0.0),
lifecycle,
omopgenerics (>= 1.3.1),
PatientProfiles (>= 1.4.2),
purrr,
rlang,
stringr,
Expand Down
2 changes: 0 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export(generateDrugUtilisationCohortSet)
export(generateIngredientCohortSet)
export(groupColumns)
export(importSummarisedResult)
export(mockDisconnect)
export(mockDrugUtilisation)
export(patternTable)
export(plotDrugRestart)
Expand Down Expand Up @@ -57,7 +56,6 @@ export(tableIndication)
export(tableProportionOfPatientsCovered)
export(tableTreatment)
export(tidy)
importFrom(PatientProfiles,mockDisconnect)
importFrom(dplyr,"%>%")
importFrom(omopgenerics,additionalColumns)
importFrom(omopgenerics,attrition)
Expand Down
13 changes: 13 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# DrugUtilisation 1.0.5

* Explictly copy data into the database using insertTable by @ablack3 in #727
* Correct as.numeric of datediff by @catalamarti in #731
* Add style argument in tables and figures by @catalamarti in #734
* Call libraries explicitly in vignettes by @catalamarti in #739
* Include in vignette how to use days prescribed by @catalamarti in #743
* tests edge case by @catalamarti in #744
* Support local datasets and standardise testing by @catalamarti in #736
* Review mock data vignette by @catalamarti in #745
* add plotIndication params by @ginberg in #740
* add type and style as NULL to use default values by @catalamarti in #747

# DrugUtilisation 1.0.4

* Fix plotDrugUtilisation combining different cdm_name by @catalamarti
Expand Down
24 changes: 14 additions & 10 deletions R/addDrugUtilisation.R
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@ addDrugUseInternal <- function(x,
dplyr::mutate("time_to_exposure" = dplyr::if_else(
.data$time_to_exposure <= .data[[indexDate]],
0L,
as.integer(!!CDMConnector::datediff(start = indexDate, end = "time_to_exposure"))
as.integer(clock::date_count_between(start = .data[[indexDate]], end = .data$time_to_exposure, precision = "day"))
))
}
x <- x |>
Expand All @@ -912,9 +912,11 @@ addDrugUseInternal <- function(x,
if (initialQuantity | initialExposureDuration) {
qs <- c(
"as.numeric(sum(.data$quantity, na.rm = TRUE))",
"max(as.integer(local(CDMConnector::datediff(
start = 'drug_exposure_start_date', end = 'drug_exposure_end_date'
))) + 1L, na.rm = TRUE)"
"max(as.integer(clock::date_count_between(
start = .data$drug_exposure_start_date,
end = .data$drug_exposure_end_date,
precision = 'day'
)) + 1L, na.rm = TRUE)"
) |>
rlang::parse_exprs() |>
rlang::set_names(c("initial_quantity", "initial_exposure_duration"))
Expand Down Expand Up @@ -959,10 +961,10 @@ addDrugUseInternal <- function(x,
.data[[censorDate]], .data$drug_exposure_end_date
)
) %>%
dplyr::mutate("exposed_time" = as.integer(!!CDMConnector::datediff(
start = "drug_exposure_start_date",
end = "drug_exposure_end_date",
interval = "day"
dplyr::mutate("exposed_time" = as.integer(clock::date_count_between(
start = .data$drug_exposure_start_date,
end = .data$drug_exposure_end_date,
precision = "day"
)) + 1L)
}
qs <- c(
Expand Down Expand Up @@ -1017,8 +1019,10 @@ addDrugUseInternal <- function(x,
.data[[censorDate]]
)
) %>%
dplyr::mutate("exposure_duration" = as.integer(!!CDMConnector::datediff(
start = "start_contribution", end = "end_contribution"
dplyr::mutate("exposure_duration" = as.integer(clock::date_count_between(
start = .data$start_contribution,
end = .data$end_contribution,
precision = "day"
)) + 1L) |>
dplyr::select(-c("start_contribution", "end_contribution")) |>
dplyr::compute(
Expand Down
4 changes: 2 additions & 2 deletions R/addIntersect.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@
#' @examples
#' \donttest{
#' library(DrugUtilisation)
#' library(CDMConnector)
#' library(dplyr, warn.conflicts = FALSE)
#' library(CDMConnector)
#'
#' cdm <- mockDrugUtilisation()
#' cdm <- mockDrugUtilisation(source = "duckdb")
#'
#' indications <- list(headache = 378253, asthma = 317009)
#' cdm <- generateConceptCohortSet(cdm = cdm,
Expand Down
29 changes: 13 additions & 16 deletions R/benchmarkDrugUtilisation.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,9 @@
#' @examples
#' \donttest{
#' library(DrugUtilisation)
#' library(CDMConnector)
#' library(duckdb)
#' library(omock)
#'
#' requireEunomia()
#' con <- dbConnect(drv = duckdb(dbdir = eunomiaDir()))
#' cdm <- cdmFromCon(con = con, cdmSchema = "main", writeSchema = "main")
#' cdm <- mockCdmFromDataset(datasetName = "GiBleed", source = "duckdb")
#'
#' timings <- benchmarkDrugUtilisation(cdm)
#'
Expand Down Expand Up @@ -72,7 +69,7 @@ benchmarkDrugUtilisation <- function(cdm,
mainCohort <- paste0(prefix, "main")
altCohort <- paste0(prefix, "alt")
inclusion <- paste0(prefix, "inclusion")
time <- as.numeric(Sys.time() - t0)
time <- as.numeric(difftime(time1 = Sys.time(), time2 = t0, units = "secs"))
result <- result |>
dplyr::union_all(dplyr::tibble(task = task, time = time))

Expand All @@ -88,7 +85,7 @@ benchmarkDrugUtilisation <- function(cdm,
gapEra = 30
)
)
time <- as.numeric(Sys.time() - t0)
time <- as.numeric(difftime(time1 = Sys.time(), time2 = t0, units = "secs"))
result <- result |>
dplyr::union_all(dplyr::tibble(task = task, time = time))

Expand All @@ -106,7 +103,7 @@ benchmarkDrugUtilisation <- function(cdm,
daysPrescribed = TRUE
)
)
time <- as.numeric(Sys.time() - t0)
time <- as.numeric(difftime(time1 = Sys.time(), time2 = t0, units = "secs"))
result <- result |>
dplyr::union_all(dplyr::tibble(task = task, time = time))

Expand All @@ -119,7 +116,7 @@ benchmarkDrugUtilisation <- function(cdm,
requireObservationBeforeDrug(days = 365, name = inclusion) |>
requirePriorDrugWashout(days = 365, name = inclusion)
)
time <- as.numeric(Sys.time() - t0)
time <- as.numeric(difftime(time1 = Sys.time(), time2 = t0, units = "secs"))
result <- result |>
dplyr::union_all(dplyr::tibble(task = task, time = time))

Expand All @@ -134,7 +131,7 @@ benchmarkDrugUtilisation <- function(cdm,
name = altCohort
)
)
time <- as.numeric(Sys.time() - t0)
time <- as.numeric(difftime(time1 = Sys.time(), time2 = t0, units = "secs"))
result <- result |>
dplyr::union_all(dplyr::tibble(task = task, time = time))

Expand All @@ -148,7 +145,7 @@ benchmarkDrugUtilisation <- function(cdm,
gapEra = 30, ingredientConceptId = ingredientConceptId
)
)
time <- as.numeric(Sys.time() - t0)
time <- as.numeric(difftime(time1 = Sys.time(), time2 = t0, units = "secs"))
result <- result |>
dplyr::union_all(dplyr::tibble(task = task, time = time))

Expand All @@ -164,7 +161,7 @@ benchmarkDrugUtilisation <- function(cdm,
indicationCohortName = indicationCohort
)
)
time <- as.numeric(Sys.time() - t0)
time <- as.numeric(difftime(time1 = Sys.time(), time2 = t0, units = "secs"))
result <- result |>
dplyr::union_all(dplyr::tibble(task = task, time = time))
}
Expand All @@ -177,7 +174,7 @@ benchmarkDrugUtilisation <- function(cdm,
cdm[[mainCohort]] |>
summariseDrugRestart(switchCohortTable = altCohort)
)
time <- as.numeric(Sys.time() - t0)
time <- as.numeric(difftime(time1 = Sys.time(), time2 = t0, units = "secs"))
result <- result |>
dplyr::union_all(dplyr::tibble(task = task, time = time))

Expand All @@ -189,7 +186,7 @@ benchmarkDrugUtilisation <- function(cdm,
cdm[[mainCohort]] |>
summariseProportionOfPatientsCovered()
)
time <- as.numeric(Sys.time() - t0)
time <- as.numeric(difftime(time1 = Sys.time(), time2 = t0, units = "secs"))
result <- result |>
dplyr::union_all(dplyr::tibble(task = task, time = time))

Expand All @@ -204,7 +201,7 @@ benchmarkDrugUtilisation <- function(cdm,
window = list(c(-Inf, -1), c(0, 0), c(1, Inf))
)
)
time <- as.numeric(Sys.time() - t0)
time <- as.numeric(difftime(time1 = Sys.time(), time2 = t0, units = "secs"))
result <- result |>
dplyr::union_all(dplyr::tibble(task = task, time = time))

Expand All @@ -213,7 +210,7 @@ benchmarkDrugUtilisation <- function(cdm,
benchmarkMessage(task)
t0 <- Sys.time()
omopgenerics::dropSourceTable(cdm = cdm, name = dplyr::starts_with(prefix))
time <- as.numeric(Sys.time() - t0)
time <- as.numeric(difftime(time1 = Sys.time(), time2 = t0, units = "secs"))
result <- result |>
dplyr::union_all(dplyr::tibble(task = task, time = time))

Expand Down
18 changes: 11 additions & 7 deletions R/cohortUtilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,10 @@ subsetTables <- function(cdm, conceptSet, name, subsetCohort, subsetCohortId) {
cohort <- cohort %>%
dplyr::mutate(
number_exposures = 1L,
days_prescribed = as.integer(!!CDMConnector::datediff(
"cohort_start_date", "cohort_end_date"
days_prescribed = as.integer(clock::date_count_between(
start = .data$cohort_start_date,
end = .data$cohort_end_date,
precision = "day"
)) + 1L
) |>
erafy(
Expand Down Expand Up @@ -199,17 +201,18 @@ erafy <- function(x,
dplyr::select(dplyr::all_of(c(group, "date_event" = end))) |>
dplyr::mutate(date_id = 1, !!!newCols)
if (gap > 0) {
gap <- as.integer(gap)
xend <- xend %>%
dplyr::mutate("date_event" = as.Date(!!CDMConnector::dateadd(
date = "date_event", number = gap, interval = "day"
dplyr::mutate("date_event" = as.Date(clock::add_days(
x = .data$date_event, n = .env$gap
)))
}
x <- xstart |>
dplyr::union_all(xend) |>
dplyr::group_by(dplyr::across(dplyr::all_of(group))) |>
dplyr::arrange(.data$date_event, .data$date_id) |>
dplyr::mutate(era_id = dplyr::if_else(
cumsum(.data$date_id) == -1 && .data$date_id == -1, -1L, 0L
cumsum(.data$date_id) == -1 & .data$date_id == -1, -1L, 0L
)) |>
dplyr::arrange(.data$date_event, .data$date_id, .data$era_id) |>
dplyr::mutate(era_id = cumsum(.data$era_id)) |>
Expand All @@ -223,9 +226,10 @@ erafy <- function(x,
.groups = "drop"
)
if (gap > 0) {
gap <- -gap
x <- x %>%
dplyr::mutate(!!end := as.Date(!!CDMConnector::dateadd(
date = end, number = -gap, interval = "day"
dplyr::mutate(!!end := as.Date(clock::add_days(
x = .data[[end]], n = .env$gap
)))
}
x <- x |>
Expand Down
35 changes: 28 additions & 7 deletions R/dailyDose.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
"quantity"
) |>
dplyr::distinct() %>%
dplyr::mutate(days_exposed = !!CDMConnector::datediff(
start = "drug_exposure_start_date",
end = "drug_exposure_end_date"
dplyr::mutate(days_exposed = clock::date_count_between(
start = .data$drug_exposure_start_date,
end = .data$drug_exposure_end_date,
precision = "day"
) + 1) |>
dplyr::inner_join(
drugStrengthPattern(cdm = cdm, ingredientConceptId = ingredientConceptId),
Expand Down Expand Up @@ -96,6 +97,10 @@ summariseDoseCoverage <- function(cdm,
omopgenerics::assertNumeric(x = sampleSize, min = 0, length = 1, null = TRUE, integerish = TRUE)
omopgenerics::assertCharacter(estimates)

# insert routes table
nmRoutes <- omopgenerics::uniqueTableName()
cdm <- omopgenerics::insertTable(cdm = cdm, name = nmRoutes, table = routes)

# get daily dosage
dailyDose <- cdm[["drug_exposure"]] |>
dplyr::inner_join(
Expand All @@ -109,9 +114,10 @@ summariseDoseCoverage <- function(cdm,
"drug_concept_id", "drug_exposure_start_date", "drug_exposure_end_date",
"quantity"
) %>%
dplyr::mutate(days_exposed = !!CDMConnector::datediff(
start = "drug_exposure_start_date",
end = "drug_exposure_end_date"
dplyr::mutate(days_exposed = clock::date_count_between(
start = .data$drug_exposure_start_date,
end = .data$drug_exposure_end_date,
precision = "day"
) + 1) |>
dplyr::left_join(
drugStrengthPattern(cdm = cdm, ingredientConceptId = ingredientConceptId),
Expand All @@ -123,7 +129,19 @@ summariseDoseCoverage <- function(cdm,
"drug_concept_id", "daily_dose", "unit", "pattern_id",
"concept_id" = "ingredient_concept_id"
) |>
.addRoute() |>
dplyr::left_join(
cdm[["concept_relationship"]] |>
dplyr::select(c("concept_id_1", "concept_id_2", "relationship_id")) |>
dplyr::filter(.data$relationship_id == "RxNorm has dose form") |>
dplyr::select(-"relationship_id") |>
dplyr::rename(
"drug_concept_id" = "concept_id_1",
"dose_form_concept_id" = "concept_id_2"
),
by = "drug_concept_id"
) |>
dplyr::left_join(cdm[[nmRoutes]], by = "dose_form_concept_id") |>
dplyr::select(-"dose_form_concept_id") |>
dplyr::left_join(
cdm[["concept"]] |>
dplyr::rename("ingredient_name" = "concept_name") |>
Expand All @@ -132,6 +150,9 @@ summariseDoseCoverage <- function(cdm,
) |>
dplyr::collect()

# drop nmRoutes
omopgenerics::dropSourceTable(cdm = cdm, name = nmRoutes)

if (!is.null(sampleSize)) {
dailyDose <- dailyDose |>
dplyr::group_by(.data$ingredient_name) |>
Expand Down
Loading