Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ✨ include hba1c criteria #106

Merged
merged 10 commits into from
Jun 19, 2024
16 changes: 16 additions & 0 deletions R/get-algorithm.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#' 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) |>
dplyr::pull(.data$logic) |>
stringr::str_replace_all("AND", "&") |>
stringr::str_replace_all("OR", "|")
lwjohnst86 marked this conversation as resolved.
Show resolved Hide resolved
}
32 changes: 32 additions & 0 deletions R/include-hba1c.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#' 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()],
lwjohnst86 marked this conversation as resolved.
Show resolved Hide resolved
#' with two columns: `pnr` and `included_hba1c`.
#' @keywords internal
#'
#' @examples
#' register_data$lab_forsker |> include_hba1c()
include_hba1c <- function(data) {
verify_required_variables(data, "lab_forsker")
hba1c_criteria <- get_algorithm_logic("hba1c")
signekb marked this conversation as resolved.
Show resolved Hide resolved
data |>
column_names_to_lower() |>
dplyr::filter({{ hba1c_criteria }}) |>
# Keep only the columns we need.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the smallest comment ever, but do we want to end inline comments with . or not?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, good question. I don't have a strong opinion. I tend to use periods because if there are two sentences, would the second sentence end in a period or not end in one? If it ends in one, it won't match the other comments. But if it doesn't end in one, it seems a bit strange because the previous sentence has a period.

dplyr::transmute(
pnr = .data$patient_cpr,
date == .data$samplingdate,
included_hba1c = TRUE
lwjohnst86 marked this conversation as resolved.
Show resolved Hide resolved
) |>
dplyr::group_by(pnr) |>
# This might not work with some databases
lwjohnst86 marked this conversation as resolved.
Show resolved Hide resolved
# Keep earliest two dates.
lwjohnst86 marked this conversation as resolved.
Show resolved Hide resolved
dplyr::slice_min(date, n = 2) |>
dplyr::ungroup()
lwjohnst86 marked this conversation as resolved.
Show resolved Hide resolved
}

21 changes: 21 additions & 0 deletions man/get_algorithm_logic.Rd

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

23 changes: 23 additions & 0 deletions man/include_hba1c.Rd

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