Skip to content

Commit

Permalink
Reuse aki.units
Browse files Browse the repository at this point in the history
  • Loading branch information
lymt committed Oct 25, 2020
1 parent 8b9c568 commit cf8ced2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 42 deletions.
18 changes: 7 additions & 11 deletions R/aki.R
Expand Up @@ -81,28 +81,23 @@ aki.default <- function(data,
aki = "aki",
units = list("SCr" = "umol/l"), na.rm = FALSE, ...) {
# TODO check if aki is an existing company
# Check SCr or bCr are given1
# Check SCr or bCr are given!
# Calc bCr if not given
# TODO consider fuctionalising aki.bCr, etc

data %>%
dplyr::mutate("{aki}.test" := !!as.name(SCr) * 2) %>%
dplyr::mutate(
"{aki}.bCr" := dplyr::case_when(
.sCr2metric(!!as.name(SCr)) >= units::set_units(4.0, "mg/dl") ~ .aki_stages[3],
!!as.name(SCr) >= 3.0 * !!as.name(bCr) ~ .aki_stages[3],
!!as.name(SCr) >= 2.0 * !!as.name(bCr) ~ .aki_stages[2],
!!as.name(SCr) >= 1.5 * !!as.name(bCr) ~ .aki_stages[1],
TRUE ~ .aki_stages[length(.aki_stages)]
)
"{aki}.bCr" := aki.units(!!as.name(SCr), !!as.name(bCr))
) %>%
dplyr::mutate(
"{aki}.cr_ch" := !!as.name(SCr) * 2
)
}


#' @importFrom rlang .data
#' @importFrom rlang `:=`
.generate_cr_ch <- function(data, SCr, dttm, pt_id = NULL) {
# TODO break into 48hr increments to reduce combn
# TODO Consider saving current grouping settings e.g. dplyr::group_data()
# Ref: https://tidyeval.tidyverse.org/dplyr.html
data_gr <- data[, c(SCr, dttm)]
Expand All @@ -118,7 +113,8 @@ aki.default <- function(data,
unique() %>%
dplyr::mutate(
admin = cumsum(
(dttm - dplyr::lag(dttm, default = lubridate::as_date(0))) >= lubridate::duration(hours = 48)
(dttm - dplyr::lag(dttm, default = lubridate::as_date(0))) >=
lubridate::duration(hours = 48)
)
) %>%
dplyr::group_by(.data$admin, .add = TRUE)
Expand Down
66 changes: 35 additions & 31 deletions tests/testthat/test-aki.R
Expand Up @@ -56,39 +56,43 @@ test_that("aki() for vector of SCr in mg/dl with baseline", {
expect_equal(aki(SCr, bCr), aki_stages)
})

pt_id <- "pt_id_"
dttm <- "dttm_"
SCr <- "SCr_"
bCr <- "bCr_"
aki <- "aki"
data_ <- data.frame(
pt_id_ = c(rep("pt1", 11 + 7), rep("pt2", 13)),
dttm_ = c(
seq(
lubridate::as_datetime("2020-10-18 09:00:00", tz = "Australia/Melbourne"),
lubridate::as_datetime("2020-10-20 09:00:00", tz = "Australia/Melbourne"),
length.out = 11
# Test for .generate_cr_ch

test_that("aki() for dataframe of SCr and bCr only", {
pt_id <- "pt_id_"
dttm <- "dttm_"
SCr <- "SCr_"
bCr <- "bCr_"
aki <- "aki"
data_ <- data.frame(
pt_id_ = c(rep("pt1", 11 + 7), rep("pt2", 13)),
dttm_ = c(
seq(
lubridate::as_datetime("2020-10-18 09:00:00", tz = "Australia/Melbourne"),
lubridate::as_datetime("2020-10-20 09:00:00", tz = "Australia/Melbourne"),
length.out = 11
),
seq(
lubridate::as_datetime("2020-10-23 09:00:00", tz = "Australia/Melbourne"),
lubridate::as_datetime("2020-10-25 09:00:00", tz = "Australia/Melbourne"),
length.out = 7
),
seq(
lubridate::as_datetime("2020-10-18 10:00:00", tz = "Australia/Melbourne"),
lubridate::as_datetime("2020-10-29 10:00:00", tz = "Australia/Melbourne"),
length.out = 13
)
),
seq(
lubridate::as_datetime("2020-10-23 09:00:00", tz = "Australia/Melbourne"),
lubridate::as_datetime("2020-10-25 09:00:00", tz = "Australia/Melbourne"),
length.out = 7
SCr_ = c(
units::set_units(seq(2.0, 4.5, by = 0.25), "mg/dl"),
units::set_units(seq(2.5, 4.0, by = 0.25), "mg/dl"),
units::set_units(seq(3.0, 4.2, by = 0.10), "mg/dl")
),
seq(
lubridate::as_datetime("2020-10-18 10:00:00", tz = "Australia/Melbourne"),
lubridate::as_datetime("2020-10-29 10:00:00", tz = "Australia/Melbourne"),
length.out = 13
bCr_ = c(
rep(units::set_units(1.8, "mg/dl"), 11 + 7),
rep(units::set_units(3.0, "mg/dl"), 13)
)
),
SCr_ = c(
units::set_units(seq(2.0, 4.5, by = 0.25), "mg/dl"),
units::set_units(seq(2.5, 4.0, by = 0.25), "mg/dl"),
units::set_units(seq(3.0, 4.2, by = 0.10), "mg/dl")
),
bCr_ = c(
rep(units::set_units(1.8, "mg/dl"), 11 + 7),
rep(units::set_units(3.0, "mg/dl"), 13)
)
)

data <- data_[sample(nrow(data_)), ]
data <- data_[sample(nrow(data_)), ]
})

0 comments on commit cf8ced2

Please sign in to comment.