From 3d83ef86c5ba4d37395aeba69a81764ecc25f081 Mon Sep 17 00:00:00 2001 From: "Luke W. Johnston" Date: Thu, 16 May 2024 12:36:40 +0200 Subject: [PATCH 1/8] feat: create (draft) function to include hba1c criteria --- R/include-hba1c.R | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 R/include-hba1c.R diff --git a/R/include-hba1c.R b/R/include-hba1c.R new file mode 100644 index 0000000..aa578be --- /dev/null +++ b/R/include-hba1c.R @@ -0,0 +1,25 @@ +#' Include only those with HbA1c in the required range. +#' +#' In the `lab_forsker` register, NPU27300 is HbA1c in the modern units (IFCC) +#' while NPU03835 is HbA1c in old units (DCCT). +#' +#' @param data The `lab_forsker` register. +#' +#' @return An object of the same input type, default as a [tibble::tibble()], +#' with two columns: `pnr` and `included_hba1c`. +#' @export +#' +#' @examples +#' register_data$lab_forsker |> include_hba1c() +include_hba1c <- function(data) { + verify_required_variables(data, "lab_forsker") + hba1c_criteria <- get_algorithm_logic("hba1c") + data |> + column_names_to_lower() |> + dplyr::filter({{ hba1c_criteria }}) |> + dplyr::transmute( + pnr = .data$patient_cpr, + included_hba1c = TRUE + ) +} + From d7093bbcb1837efb38e1c6cdb9917c4585f87992 Mon Sep 17 00:00:00 2001 From: "Luke W. Johnston" Date: Thu, 16 May 2024 12:37:12 +0200 Subject: [PATCH 2/8] feat: internal function to extract logic from algorithm data. Untested --- R/get-algorithm.R | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 R/get-algorithm.R diff --git a/R/get-algorithm.R b/R/get-algorithm.R new file mode 100644 index 0000000..5aa3c1d --- /dev/null +++ b/R/get-algorithm.R @@ -0,0 +1,7 @@ +get_algorithm_logic <- function(criteria) { + algorithm |> + dplyr::filter(.data$name == criteria) |> + dplyr::pull(.data$logic) |> + stringr::str_replace_all("AND", "&") |> + stringr::str_replace_all("OR", "|") +} From e65b1b6bd6397178c61e780c0a2928dcf28069de Mon Sep 17 00:00:00 2001 From: "Luke W. Johnston" Date: Thu, 16 May 2024 16:29:19 +0200 Subject: [PATCH 3/8] docs: add roxygen docs to logic getter --- R/get-algorithm.R | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/R/get-algorithm.R b/R/get-algorithm.R index 5aa3c1d..b3a292e 100644 --- a/R/get-algorithm.R +++ b/R/get-algorithm.R @@ -1,3 +1,12 @@ +#' Get the criteria algorithmic logic and convert to an R logic condition. +#' +#' @param criteria The name of the inclusion or exclusion criteria to use. +#' +#' @return A character string. +#' @keywords internal +#' +#' @examples +#' get_algorithm_logic("hba1c") get_algorithm_logic <- function(criteria) { algorithm |> dplyr::filter(.data$name == criteria) |> From e45e1e2562027fbef54e1bb90a10e561ac4584b3 Mon Sep 17 00:00:00 2001 From: "Luke W. Johnston" Date: Thu, 16 May 2024 16:29:38 +0200 Subject: [PATCH 4/8] docs: keep this function as internal only --- R/include-hba1c.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/include-hba1c.R b/R/include-hba1c.R index aa578be..873793f 100644 --- a/R/include-hba1c.R +++ b/R/include-hba1c.R @@ -7,7 +7,7 @@ #' #' @return An object of the same input type, default as a [tibble::tibble()], #' with two columns: `pnr` and `included_hba1c`. -#' @export +#' @keywords internal #' #' @examples #' register_data$lab_forsker |> include_hba1c() From b4c36daa18ecdd3817477a53cbd10260a02a8c5c Mon Sep 17 00:00:00 2001 From: "Luke W. Johnston" Date: Thu, 16 May 2024 16:30:42 +0200 Subject: [PATCH 5/8] refactor: keep only earliest two dates, might not work with some databases --- R/include-hba1c.R | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/R/include-hba1c.R b/R/include-hba1c.R index 873793f..5d3e98e 100644 --- a/R/include-hba1c.R +++ b/R/include-hba1c.R @@ -17,9 +17,16 @@ include_hba1c <- function(data) { data |> column_names_to_lower() |> dplyr::filter({{ hba1c_criteria }}) |> + # Keep only the columns we need. dplyr::transmute( pnr = .data$patient_cpr, + date == .data$samplingdate, included_hba1c = TRUE - ) + ) |> + dplyr::group_by(pnr) |> + # This might not work with some databases + # Keep earliest two dates. + dplyr::slice_min(date, n = 2) |> + dplyr::ungroup() } From ed7bb344a6cab0e5e6839a7011b152f28701291e Mon Sep 17 00:00:00 2001 From: "Luke W. Johnston" Date: Thu, 16 May 2024 16:31:12 +0200 Subject: [PATCH 6/8] docs: regenerated the roxygen docs --- man/get_algorithm_logic.Rd | 21 +++++++++++++++++++++ man/include_hba1c.Rd | 23 +++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 man/get_algorithm_logic.Rd create mode 100644 man/include_hba1c.Rd diff --git a/man/get_algorithm_logic.Rd b/man/get_algorithm_logic.Rd new file mode 100644 index 0000000..dfcb894 --- /dev/null +++ b/man/get_algorithm_logic.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/get-algorithm.R +\name{get_algorithm_logic} +\alias{get_algorithm_logic} +\title{Get the criteria algorithmic logic and convert to an R logic condition.} +\usage{ +get_algorithm_logic(criteria) +} +\arguments{ +\item{criteria}{The name of the inclusion or exclusion criteria to use.} +} +\value{ +A character string. +} +\description{ +Get the criteria algorithmic logic and convert to an R logic condition. +} +\examples{ +get_algorithm_logic("hba1c") +} +\keyword{internal} diff --git a/man/include_hba1c.Rd b/man/include_hba1c.Rd new file mode 100644 index 0000000..91e528c --- /dev/null +++ b/man/include_hba1c.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/include-hba1c.R +\name{include_hba1c} +\alias{include_hba1c} +\title{Include only those with HbA1c in the required range.} +\usage{ +include_hba1c(data) +} +\arguments{ +\item{data}{The \code{lab_forsker} register.} +} +\value{ +An object of the same input type, default as a \code{\link[tibble:tibble]{tibble::tibble()}}, +with two columns: \code{pnr} and \code{included_hba1c}. +} +\description{ +In the \code{lab_forsker} register, NPU27300 is HbA1c in the modern units (IFCC) +while NPU03835 is HbA1c in old units (DCCT). +} +\examples{ +register_data$lab_forsker |> include_hba1c() +} +\keyword{internal} From fb401bea8f3864d3be93dd7ba1c40b6fceeb62cd Mon Sep 17 00:00:00 2001 From: "Luke W. Johnston" Date: Fri, 31 May 2024 15:43:13 +0200 Subject: [PATCH 7/8] docs: apply suggestions from review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Signe Kirk Brødbæk <40836345+signekb@users.noreply.github.com> --- R/include-hba1c.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/include-hba1c.R b/R/include-hba1c.R index 5d3e98e..94834c9 100644 --- a/R/include-hba1c.R +++ b/R/include-hba1c.R @@ -24,7 +24,7 @@ include_hba1c <- function(data) { included_hba1c = TRUE ) |> dplyr::group_by(pnr) |> - # This might not work with some databases + # FIXME: This might not work with some databases # Keep earliest two dates. dplyr::slice_min(date, n = 2) |> dplyr::ungroup() From f0f1697488d8f999d18b0e389c3df0588950a76c Mon Sep 17 00:00:00 2001 From: "Luke W. Johnston" Date: Wed, 19 Jun 2024 11:52:49 +0200 Subject: [PATCH 8/8] fix: :bug: keep earliest two dates, not samples --- R/include-hba1c.R | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/R/include-hba1c.R b/R/include-hba1c.R index 94834c9..f044fe0 100644 --- a/R/include-hba1c.R +++ b/R/include-hba1c.R @@ -18,15 +18,17 @@ include_hba1c <- function(data) { column_names_to_lower() |> dplyr::filter({{ hba1c_criteria }}) |> # Keep only the columns we need. - dplyr::transmute( + dplyr::mutate( pnr = .data$patient_cpr, date == .data$samplingdate, - included_hba1c = TRUE + included_hba1c = TRUE, + .keep = "none" ) |> + # Remove any duplicates + dplyr::distinct() |> dplyr::group_by(pnr) |> # FIXME: This might not work with some databases # Keep earliest two dates. dplyr::slice_min(date, n = 2) |> dplyr::ungroup() } -