Skip to content

Commit

Permalink
Improved support for NA. Added testing for renal function.
Browse files Browse the repository at this point in the history
  • Loading branch information
LS31 committed Nov 11, 2020
1 parent 7c451d8 commit 2af7b66
Show file tree
Hide file tree
Showing 16 changed files with 264 additions and 121 deletions.
19 changes: 13 additions & 6 deletions R/blood_pressure.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@
#'
#' @param sbp Systolic blood pressure (mmHg).
#' @param dbp Diastolic blood pressure (mmHg).
#' @return MAP (mmHg).
#' @return MAP (mmHg), or `NA` if any parameters are `NA`.
#' @export
#' @seealso \code{\link[units]{set_units}}, \code{\link[units]{drop_units}}
estimate_map <- function(sbp, dbp) {
assertthat::assert_that(assertthat::is.number(sbp) | is.na(sbp))
assertthat::assert_that(assertthat::is.number(dbp) | is.na(dbp))
if (anyNA(c(sbp, dbp))) {
return(NA)
}
assertthat::assert_that(assertthat::is.number(sbp))
assertthat::assert_that(assertthat::is.number(dbp))

(dbp + (1/3 * (sbp - dbp))) %>%
units::set_units("mmHg", mode = "standard")
}
Expand All @@ -30,12 +34,15 @@ estimate_map <- function(sbp, dbp) {
#'
#' @param sbp Systolic blood pressure (mmHg).
#' @param dbp Diastolic blood pressure (mmHg).
#' @return Pulse pressure (mmHg).
#' @return Pulse pressure (mmHg), or `NA` if any parameters are `NA`.
#' @export
#' @seealso \code{\link[units]{set_units}}, \code{\link[units]{drop_units}}
calculate_pulse_pressure <- function(sbp, dbp) {
assertthat::assert_that(assertthat::is.number(sbp) | is.na(sbp))
assertthat::assert_that(assertthat::is.number(dbp) | is.na(dbp))
if (anyNA(c(sbp, dbp))) {
return(NA)
}
assertthat::assert_that(assertthat::is.number(sbp))
assertthat::assert_that(assertthat::is.number(dbp))
(sbp - dbp) %>%
units::set_units("mmHg", mode = "standard")
}
13 changes: 10 additions & 3 deletions R/bmi.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@
#'
#' @param weight Weight (kg).
#' @param height Height (cm).
#' @return BMI (\eqn{\text{kg} m^{-2}}).
#' @return BMI (\eqn{\text{kg} m^{-2}}), or `NA` if any parameters are `NA`.
#' @export
#' @seealso [units::set_units()], [units::drop_units()]
calculate_bmi <- function(weight, height) {
if (anyNA(c(weight, height))) {
return(NA)
}
assertthat::assert_that(assertthat::is.number(weight) |
is.na(weight))
assertthat::assert_that(assertthat::is.number(height) |
is.na(height))

(weight / ((height / 100) ^ 2)) %>%
units::set_units("kg1 m-2", mode = "standard")
}
Expand All @@ -35,11 +39,14 @@ calculate_bmi <- function(weight, height) {
#' @param height Height (cm).
#' @param is_female `TRUE` if patient is female, `FALSE` if patient is
#' male.
#' @return IBW (kg).
#' @return IBW (kg), or `NA` if any parameters are `NA`.
#' @export
#' @seealso [units::set_units()], [units::drop_units()]
estimate_ibw <- function(height, is_female) {
assertthat::assert_that(assertthat::is.number(height) | is.na(height))
if (anyNA(c(height, is_female))) {
return(NA)
}
assertthat::assert_that(assertthat::is.number(height))
assertthat::assert_that(assertthat::is.flag(is_female))

if(is_female) {
Expand Down
10 changes: 9 additions & 1 deletion R/bsa.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#' @param height Height (cm).
#' @param method Formula to estimate BSA. Options are: "Monsteller" (default),
#' "Du Bois".
#' @return BSA (\eqn{m^2}).
#' @return BSA (\eqn{m^2}), or `NA` if any parameters are `NA`.
#' @export
#' @seealso [units::set_units()], [units::drop_units()]
estimate_bsa <- function(weight, height, method = "Monsteller") {
Expand All @@ -25,21 +25,29 @@ estimate_bsa <- function(weight, height, method = "Monsteller") {
#' @describeIn estimate_bsa Estimate the body surface area (BSA) according to Monsteller.
#' @noRd
estimate_bsa_monsteller <- function(weight, height) {
if (anyNA(c(weight, height))) {
return(NA)
}
assertthat::assert_that(assertthat::is.number(weight) |
is.na(weight))
assertthat::assert_that(assertthat::is.number(height) |
is.na(height))

((weight * height / 3600) ^ (0.5)) %>%
units::set_units("m-2", mode = "standard")
}

#' @describeIn estimate_bsa Estimate the body surface area (BSA) according to Du Bois
#' @noRd
estimate_bsa_du_bois <- function(weight, height) {
if (anyNA(c(weight, height))) {
return(NA)
}
assertthat::assert_that(assertthat::is.number(weight) |
is.na(weight))
assertthat::assert_that(assertthat::is.number(height) |
is.na(height))

(0.007184 * (weight ^ 0.425) * (height ^ 0.725)) %>%
units::set_units("m-2", mode = "standard")
}
24 changes: 13 additions & 11 deletions R/electrolytes.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@
#' @param calcium Measured total calcium level (mmol/l).
#' @param albumin Measured albumin level (g/l).
#' @param normal_albumin Normal value of albumin (g/l) (40 by default).
#' @return Albumin-adjusted calcium level (mmol/l).
#' @return Albumin-adjusted calcium level (mmol/l), or `NA` if any parameters are `NA`.
#' @export
#' @seealso [units::set_units()], [units::drop_units()]
adjust_calcium_for_albumin <-
function(calcium, albumin, normal_albumin = 40) {
assertthat::assert_that(assertthat::is.number(calcium) |
is.na(calcium))
assertthat::assert_that(assertthat::is.number(albumin) |
is.na(albumin))
if (anyNA(c(calcium, albumin, normal_albumin))) {
return(NA)
}
assertthat::assert_that(assertthat::is.number(calcium))
assertthat::assert_that(assertthat::is.number(albumin))

(calcium + 0.025 * (normal_albumin - albumin)) %>%
units::set_units("mmol1 l-1", mode = "standard")
Expand Down Expand Up @@ -53,22 +54,23 @@ adjust_calcium_for_albumin <-
#' @param sodium Measured sodium level (mmol/l).
#' @param glucose Measured glucose level (mmol/l).
#' @param method Formula. Options are: "Hillier" (default), "Katz".
#' @return Glucose-adjusted sodium level (mmol/l).
#' @return Glucose-adjusted sodium level (mmol/l), or `NA` if any parameters are `NA`.
#' @export
#' @seealso [units::set_units()], [units::drop_units()]
adjust_sodium_for_glucose <-
function(sodium, glucose, method = "Hillier") {
assertthat::assert_that(assertthat::is.number(sodium) |
is.na(sodium))
assertthat::assert_that(assertthat::is.number(glucose) |
is.na(glucose))
if (anyNA(c(sodium, glucose, method))) {
return(NA)
}
assertthat::assert_that(assertthat::is.number(sodium))
assertthat::assert_that(assertthat::is.number(glucose))
assertthat::assert_that(assertthat::is.string(method))

xkappa <- switch(
method,
"Hillier" = 2.4,
"Katz" = 1.6,
stop("Illegal value for argument method in adjust_sodium_for_glucose().")
stop(paste0("Error: `method` must be `Hillier` or `Katz`.\n You've supplied method `", method, "`."))
)

(sodium + xkappa * ((glucose-5.6)/5.6)) %>%
Expand Down
6 changes: 5 additions & 1 deletion R/metabolic_syndrome.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
#' @param has_glucose_drug `TRUE` if patient is on drug treatment for
#' elevated glucose, otherwise `FALSE` (default).
#' @return `TRUE` if patient has the metabolic syndrome, `FALSE` is
#' not, `NA` if indetermined.
#' not, `NA` if indetermined or `is_female` was `NA`.
#' @export
has_metabolic_syndrome_atpiii <- function(is_female,
waist_circumference = NA,
Expand All @@ -57,6 +57,10 @@ has_metabolic_syndrome_atpiii <- function(is_female,
has_antihypertensive_drug = FALSE,
has_lipid_drug = FALSE,
has_glucose_drug = FALSE) {
if(is.na(is_female)) {
warning("Warning: `is_female` was `NA`, therefore presence of the metabolic syndrome could not be determined. `NA` was returned.")
return(NA)
}
assertthat::assert_that(assertthat::is.flag(is_female))
assertthat::assert_that(assertthat::is.number(waist_circumference) | is.na(waist_circumference))
assertthat::assert_that(assertthat::is.number(sbp) | is.na(sbp))
Expand Down
6 changes: 5 additions & 1 deletion R/pneumonia.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@
#' @param sbp Systolic blood pressure (mmHg).
#' @param dbp Diastolic blood pressure (mmHg).
#' @param age Age (years).
#' @return CURB-65 score (points).
#' @return CURB-65 score (points), or `NA` if any parameters are `NA`.
#' @export
score_curb65 <- function(confusion,
urea,
respiratory_rate,
sbp,
dbp,
age) {
if(anyNA(c(confusion, urea, respiratory_rate, sbp, dbp, age))){
return(NA)
}
assertthat::assert_that(assertthat::is.flag(confusion))
assertthat::assert_that(assertthat::is.number(urea))
assertthat::assert_that(assertthat::is.number(respiratory_rate))
assertthat::assert_that(assertthat::is.number(sbp))
assertthat::assert_that(assertthat::is.number(dbp))
Expand Down
18 changes: 13 additions & 5 deletions R/qtc.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' @param heart_rate Heart rate (/min).
#' @param method Formula for QTc. Options are: "Fridericia" (default),
#' "Bazzett".
#' @return QTc (ms).
#' @return QTc (ms), or `NA` if any parameters are `NA`.
#' @export
#' @seealso [units::set_units()], [units::drop_units()]
calculate_qtc <- function(qt, heart_rate, method = "Fridericia") {
Expand All @@ -20,8 +20,12 @@ calculate_qtc <- function(qt, heart_rate, method = "Fridericia") {
#' @describeIn calculate_qtc QTc according to Bazett.
#' @noRd
calculate_qtc_bazett <- function(qt, heart_rate) {
assertthat::assert_that(assertthat::is.number(qt) | is.na(qt))
assertthat::assert_that(assertthat::is.number(heart_rate) | is.na(heart_rate))
if (anyNA(c(qt, heart_rate))) {
return(NA)
}
assertthat::assert_that(assertthat::is.number(qt))
assertthat::assert_that(assertthat::is.number(heart_rate))

qt <- qt / 1000
rr <- 60 / heart_rate
(qt / (rr ^ (1 / 2)) * 1000) %>%
Expand All @@ -31,8 +35,12 @@ calculate_qtc_bazett <- function(qt, heart_rate) {
#' @describeIn calculate_qtc QTc according to Fridericia.
#' @noRd
calculate_qtc_fridericia <- function(qt, heart_rate) {
assertthat::assert_that(assertthat::is.number(qt) | is.na(qt))
assertthat::assert_that(assertthat::is.number(heart_rate) | is.na(heart_rate))
if (anyNA(c(qt, heart_rate))) {
return(NA)
}
assertthat::assert_that(assertthat::is.number(qt))
assertthat::assert_that(assertthat::is.number(heart_rate))

qt <- qt / 1000
rr <- 60 / heart_rate
(qt / (rr ^ (1 / 3)) * 1000) %>%
Expand Down
Loading

0 comments on commit 2af7b66

Please sign in to comment.