1
0
mirror of https://github.com/msberends/AMR.git synced 2025-07-08 13:21:50 +02:00

(v0.7.1.9081) bug_drug fixes

This commit is contained in:
2019-09-23 13:53:50 +02:00
parent 66d405ff57
commit 64d9829030
26 changed files with 622 additions and 505 deletions

View File

@ -25,12 +25,16 @@
#' @inheritParams eucast_rules
#' @param combine_IR logical to indicate whether values R and I should be summed
#' @param add_ab_group logical to indicate where the group of the antimicrobials must be included as a first column
#' @param ... argumments passed on to \code{\link{mo_name}}
#' @param FUN the function to call on the \code{mo} column to transform the microorganism IDs, defaults to \code{\link{mo_shortname}}
#' @param ... argumments passed on to \code{FUN}
#' @inheritParams rsi_df
#' @inheritParams base::formatC
#' @importFrom dplyr rename
#' @importFrom tidyr spread
#' @importFrom clean freq
#' @details The function \code{format} calculates the resistance per bug-drug combination. Use \code{combine_IR = FALSE} (default) to test R vs. S+I and \code{combine_IR = TRUE} to test R+I vs. S.
#'
#' The language of the output can be overwritten with \code{options(AMR_locale)}, please see \link{translate}.
#' @export
#' @rdname bug_drug_combinations
#' @source \strong{M39 Analysis and Presentation of Cumulative Antimicrobial Susceptibility Test Data, 4th Edition}, 2014, \emph{Clinical and Laboratory Standards Institute (CLSI)}. \url{https://clsi.org/standards/products/microbiology/documents/m39/}.
@ -40,8 +44,21 @@
#' x <- bug_drug_combinations(example_isolates)
#' x
#' format(x)
#'
#' # Use FUN to change to transformation of microorganism codes
#' x <- bug_drug_combinations(example_isolates,
#' FUN = mo_gramstain)
#'
#' x <- bug_drug_combinations(example_isolates,
#' FUN = function(x) ifelse(x == "B_ESCHR_COLI",
#' "E. coli",
#' "Others"))
#' }
bug_drug_combinations <- function(x, col_mo = NULL, minimum = 30) {
bug_drug_combinations <- function(x,
col_mo = NULL,
minimum = 30,
FUN = mo_shortname,
...) {
if (!is.data.frame(x)) {
stop("`x` must be a data frame.", call. = FALSE)
}
@ -56,7 +73,7 @@ bug_drug_combinations <- function(x, col_mo = NULL, minimum = 30) {
}
x <- x %>%
mutate(col_mo = x %>% pull(col_mo)) %>%
mutate(mo = x %>% pull(col_mo) %>% FUN(...)) %>%
filter(mo %in% (clean::freq(mo) %>%
filter(count >= minimum) %>%
pull(item))) %>%
@ -76,31 +93,37 @@ bug_drug_combinations <- function(x, col_mo = NULL, minimum = 30) {
#' @exportMethod format.bug_drug_combinations
#' @export
#' @rdname bug_drug_combinations
format.bug_drug_combinations <- function(x, combine_IR = FALSE, add_ab_group = TRUE, ...) {
format.bug_drug_combinations <- function(x,
combine_IR = FALSE,
add_ab_group = TRUE,
decimal.mark = getOption("OutDec"),
big.mark = ifelse(decimal.mark == ",", ".", ",")) {
if (combine_IR == FALSE) {
x$isolates <- x$R
} else {
x$isolates <- x$R + x$I
}
y <- x %>%
mutate(mo = mo_name(mo, ...),
txt = paste0(percent(isolates / total, force_zero = TRUE),
" (", trimws(format(isolates, big.mark = ",")), "/",
trimws(format(total, big.mark = ",")), ")")) %>%
mutate(txt = paste0(percent(isolates / total, force_zero = TRUE, decimal.mark = decimal.mark, big.mark = big.mark),
" (", trimws(format(isolates, big.mark = big.mark)), "/",
trimws(format(total, big.mark = big.mark)), ")")) %>%
select(ab, mo, txt) %>%
spread(mo, txt) %>%
mutate_all(~ifelse(is.na(.), "", .)) %>%
mutate(ab = paste0(ab_name(ab), " (", as.ab(ab), ", ", ab_atc(ab), ")"),
ab_group = ab_group(ab)) %>%
mutate(ab_group = ab_group(ab),
ab = paste0(ab_name(ab), " (", as.ab(ab), ", ", ab_atc(ab), ")")) %>%
select(ab_group, ab, everything()) %>%
arrange(ab_group, ab) %>%
mutate(ab_group = ifelse(ab_group != lag(ab_group) | is.na(lag(ab_group)), ab_group, ""))
if (add_ab_group == FALSE) {
y <- y %>% select(-ab_group)
y <- y %>% select(-ab_group) %>% rename("Antibiotic" = ab)
colnames(y)[1] <- translate_AMR(colnames(y)[1], language = get_locale(), only_unknown = FALSE)
} else {
y <- y %>% rename("Group" = ab_group,
"Antibiotic" = ab)
colnames(y)[1:2] <- translate_AMR(colnames(y)[1:2], language = get_locale(), only_unknown = FALSE)
}
y <- y %>% rename("Group" = ab_group,
"Antibiotic" = ab)
y
}