mirror of
https://github.com/msberends/AMR.git
synced 2025-07-12 13:01:51 +02:00
styled, unit test fix
This commit is contained in:
420
R/ab_selectors.R
420
R/ab_selectors.R
@ -9,7 +9,7 @@
|
||||
# (c) 2018-2022 Berends MS, Luz CF et al. #
|
||||
# Developed at the University of Groningen, the Netherlands, in #
|
||||
# collaboration with non-profit organisations Certe Medical #
|
||||
# Diagnostics & Advice, and University Medical Center Groningen. #
|
||||
# Diagnostics & Advice, and University Medical Center Groningen. #
|
||||
# #
|
||||
# This R package is free software; you can freely use and distribute #
|
||||
# it for both personal and commercial purposes under the terms of the #
|
||||
@ -24,7 +24,7 @@
|
||||
# ==================================================================== #
|
||||
|
||||
#' Antibiotic Selectors
|
||||
#'
|
||||
#'
|
||||
#' These functions allow for filtering rows and selecting columns based on antibiotic test results that are of a specific antibiotic class or group, without the need to define the columns or antibiotic abbreviations. In short, if you have a column name that resembles an antimicrobial agent, it will be picked up by any of these functions that matches its pharmaceutical class: "cefazolin", "CZO" and "J01DB04" will all be picked up by [cephalosporins()].
|
||||
#' @param ab_class an antimicrobial class or a part of it, such as `"carba"` and `"carbapenems"`. The columns `group`, `atc_group1` and `atc_group2` of the [antibiotics] data set will be searched (case-insensitive) for this value.
|
||||
#' @param filter an [expression] to be evaluated in the [antibiotics] data set, such as `name %like% "trim"`
|
||||
@ -33,12 +33,12 @@
|
||||
#' @param ... ignored, only in place to allow future extensions
|
||||
#' @details
|
||||
#' These functions can be used in data set calls for selecting columns and filtering rows. They are heavily inspired by the [Tidyverse selection helpers][tidyselect::language] such as [`everything()`][tidyselect::everything()], but also work in base \R and not only in `dplyr` verbs. Nonetheless, they are very convenient to use with `dplyr` functions such as [`select()`][dplyr::select()], [`filter()`][dplyr::filter()] and [`summarise()`][dplyr::summarise()], see *Examples*.
|
||||
#'
|
||||
#' All columns in the data in which these functions are called will be searched for known antibiotic names, abbreviations, brand names, and codes (ATC, EARS-Net, WHO, etc.) according to the [antibiotics] data set. This means that a selector such as [aminoglycosides()] will pick up column names like 'gen', 'genta', 'J01GB03', 'tobra', 'Tobracin', etc.
|
||||
#'
|
||||
#'
|
||||
#' All columns in the data in which these functions are called will be searched for known antibiotic names, abbreviations, brand names, and codes (ATC, EARS-Net, WHO, etc.) according to the [antibiotics] data set. This means that a selector such as [aminoglycosides()] will pick up column names like 'gen', 'genta', 'J01GB03', 'tobra', 'Tobracin', etc.
|
||||
#'
|
||||
#' The [ab_class()] function can be used to filter/select on a manually defined antibiotic class. It searches for results in the [antibiotics] data set within the columns `group`, `atc_group1` and `atc_group2`.
|
||||
#' @section Full list of supported (antibiotic) classes:
|
||||
#'
|
||||
#'
|
||||
#' `r paste0(" * ", na.omit(sapply(DEFINED_AB_GROUPS, function(ab) ifelse(tolower(gsub("^AB_", "", ab)) %in% ls(envir = asNamespace("AMR")), paste0("[", tolower(gsub("^AB_", "", ab)), "()] can select: \\cr ", vector_and(paste0(ab_name(eval(parse(text = ab), envir = asNamespace("AMR")), language = NULL, tolower = TRUE), " (", eval(parse(text = ab), envir = asNamespace("AMR")), ")"), quotes = FALSE, sort = TRUE)), character(0)), USE.NAMES = FALSE)), "\n", collapse = "")`
|
||||
#' @rdname antibiotic_class_selectors
|
||||
#' @name antibiotic_class_selectors
|
||||
@ -46,153 +46,144 @@
|
||||
#' @export
|
||||
#' @inheritSection AMR Reference Data Publicly Available
|
||||
|
||||
#' @examples
|
||||
#' @examples
|
||||
#' # `example_isolates` is a data set available in the AMR package.
|
||||
#' # See ?example_isolates.
|
||||
#' example_isolates
|
||||
#'
|
||||
#'
|
||||
#' # base R ------------------------------------------------------------------
|
||||
#'
|
||||
#'
|
||||
#' # select columns 'IPM' (imipenem) and 'MEM' (meropenem)
|
||||
#' example_isolates[, carbapenems()]
|
||||
#'
|
||||
#'
|
||||
#' # select columns 'mo', 'AMK', 'GEN', 'KAN' and 'TOB'
|
||||
#' example_isolates[, c("mo", aminoglycosides())]
|
||||
#'
|
||||
#'
|
||||
#' # select only antibiotic columns with DDDs for oral treatment
|
||||
#' example_isolates[, administrable_per_os()]
|
||||
#'
|
||||
#'
|
||||
#' # filter using any() or all()
|
||||
#' example_isolates[any(carbapenems() == "R"), ]
|
||||
#' subset(example_isolates, any(carbapenems() == "R"))
|
||||
#'
|
||||
#'
|
||||
#' # filter on any or all results in the carbapenem columns (i.e., IPM, MEM):
|
||||
#' example_isolates[any(carbapenems()), ]
|
||||
#' example_isolates[all(carbapenems()), ]
|
||||
#'
|
||||
#'
|
||||
#' # filter with multiple antibiotic selectors using c()
|
||||
#' example_isolates[all(c(carbapenems(), aminoglycosides()) == "R"), ]
|
||||
#'
|
||||
#'
|
||||
#' # filter + select in one go: get penicillins in carbapenems-resistant strains
|
||||
#' example_isolates[any(carbapenems() == "R"), penicillins()]
|
||||
#'
|
||||
#'
|
||||
#' # You can combine selectors with '&' to be more specific. For example,
|
||||
#' # penicillins() would select benzylpenicillin ('peni G') and
|
||||
#' # administrable_per_os() would select erythromycin. Yet, when combined these
|
||||
#' # drugs are both omitted since benzylpenicillin is not administrable per os
|
||||
#' # and erythromycin is not a penicillin:
|
||||
#' example_isolates[, penicillins() & administrable_per_os()]
|
||||
#'
|
||||
#'
|
||||
#' # ab_selector() applies a filter in the `antibiotics` data set and is thus very
|
||||
#' # flexible. For instance, to select antibiotic columns with an oral DDD of at
|
||||
#' # least 1 gram:
|
||||
#' example_isolates[, ab_selector(oral_ddd > 1 & oral_units == "g")]
|
||||
#'
|
||||
#'
|
||||
#' # dplyr -------------------------------------------------------------------
|
||||
#' \donttest{
|
||||
#' if (require("dplyr")) {
|
||||
#'
|
||||
#'
|
||||
#' # get AMR for all aminoglycosides e.g., per ward:
|
||||
#' example_isolates %>%
|
||||
#' group_by(ward) %>%
|
||||
#' group_by(ward) %>%
|
||||
#' summarise(across(aminoglycosides(), resistance))
|
||||
#'
|
||||
#' }
|
||||
#' if (require("dplyr")) {
|
||||
#'
|
||||
#'
|
||||
#' # You can combine selectors with '&' to be more specific:
|
||||
#' example_isolates %>%
|
||||
#' select(penicillins() & administrable_per_os())
|
||||
#'
|
||||
#' }
|
||||
#' if (require("dplyr")) {
|
||||
#'
|
||||
#'
|
||||
#' # get AMR for only drugs that matter - no intrinsic resistance:
|
||||
#' example_isolates %>%
|
||||
#' filter(mo_genus() %in% c("Escherichia", "Klebsiella")) %>%
|
||||
#' group_by(ward) %>%
|
||||
#' filter(mo_genus() %in% c("Escherichia", "Klebsiella")) %>%
|
||||
#' group_by(ward) %>%
|
||||
#' summarise(across(not_intrinsic_resistant(), resistance))
|
||||
#'
|
||||
#' }
|
||||
#' if (require("dplyr")) {
|
||||
#'
|
||||
#'
|
||||
#' # get susceptibility for antibiotics whose name contains "trim":
|
||||
#' example_isolates %>%
|
||||
#' filter(first_isolate()) %>%
|
||||
#' group_by(ward) %>%
|
||||
#' filter(first_isolate()) %>%
|
||||
#' group_by(ward) %>%
|
||||
#' summarise(across(ab_selector(name %like% "trim"), susceptibility))
|
||||
#'
|
||||
#' }
|
||||
#' if (require("dplyr")) {
|
||||
#'
|
||||
#'
|
||||
#' # this will select columns 'IPM' (imipenem) and 'MEM' (meropenem):
|
||||
#' example_isolates %>%
|
||||
#' example_isolates %>%
|
||||
#' select(carbapenems())
|
||||
#'
|
||||
#' }
|
||||
#' if (require("dplyr")) {
|
||||
#'
|
||||
#'
|
||||
#' # this will select columns 'mo', 'AMK', 'GEN', 'KAN' and 'TOB':
|
||||
#' example_isolates %>%
|
||||
#' example_isolates %>%
|
||||
#' select(mo, aminoglycosides())
|
||||
#'
|
||||
#' }
|
||||
#' if (require("dplyr")) {
|
||||
#'
|
||||
#' # any() and all() work in dplyr's filter() too:
|
||||
#' example_isolates %>%
|
||||
#' filter(any(aminoglycosides() == "R"),
|
||||
#' all(cephalosporins_2nd() == "R"))
|
||||
#'
|
||||
#'
|
||||
#' # any() and all() work in dplyr's filter() too:
|
||||
#' example_isolates %>%
|
||||
#' filter(
|
||||
#' any(aminoglycosides() == "R"),
|
||||
#' all(cephalosporins_2nd() == "R")
|
||||
#' )
|
||||
#' }
|
||||
#' if (require("dplyr")) {
|
||||
#'
|
||||
#' # also works with c():
|
||||
#' example_isolates %>%
|
||||
#'
|
||||
#' # also works with c():
|
||||
#' example_isolates %>%
|
||||
#' filter(any(c(carbapenems(), aminoglycosides()) == "R"))
|
||||
#'
|
||||
#' }
|
||||
#' if (require("dplyr")) {
|
||||
#'
|
||||
#' # not setting any/all will automatically apply all():
|
||||
#' example_isolates %>%
|
||||
#'
|
||||
#' # not setting any/all will automatically apply all():
|
||||
#' example_isolates %>%
|
||||
#' filter(aminoglycosides() == "R")
|
||||
#'
|
||||
#' }
|
||||
#' if (require("dplyr")) {
|
||||
#'
|
||||
#'
|
||||
#' # this will select columns 'mo' and all antimycobacterial drugs ('RIF'):
|
||||
#' example_isolates %>%
|
||||
#' example_isolates %>%
|
||||
#' select(mo, ab_class("mycobact"))
|
||||
#'
|
||||
#' }
|
||||
#' if (require("dplyr")) {
|
||||
#'
|
||||
#'
|
||||
#' # get bug/drug combinations for only glycopeptides in Gram-positives:
|
||||
#' example_isolates %>%
|
||||
#' filter(mo_is_gram_positive()) %>%
|
||||
#' select(mo, glycopeptides()) %>%
|
||||
#' example_isolates %>%
|
||||
#' filter(mo_is_gram_positive()) %>%
|
||||
#' select(mo, glycopeptides()) %>%
|
||||
#' bug_drug_combinations() %>%
|
||||
#' format()
|
||||
#'
|
||||
#' }
|
||||
#' if (require("dplyr")) {
|
||||
#'
|
||||
#' data.frame(some_column = "some_value",
|
||||
#' J01CA01 = "S") %>% # ATC code of ampicillin
|
||||
#' select(penicillins()) # only the 'J01CA01' column will be selected
|
||||
#'
|
||||
#' data.frame(
|
||||
#' some_column = "some_value",
|
||||
#' J01CA01 = "S"
|
||||
#' ) %>% # ATC code of ampicillin
|
||||
#' select(penicillins()) # only the 'J01CA01' column will be selected
|
||||
#' }
|
||||
#' if (require("dplyr")) {
|
||||
#'
|
||||
#'
|
||||
#' # with recent versions of dplyr this is all equal:
|
||||
#' x <- example_isolates[carbapenems() == "R", ]
|
||||
#' y <- example_isolates %>% filter(carbapenems() == "R")
|
||||
#' z <- example_isolates %>% filter(if_all(carbapenems(), ~.x == "R"))
|
||||
#' z <- example_isolates %>% filter(if_all(carbapenems(), ~ .x == "R"))
|
||||
#' identical(x, y) && identical(y, z)
|
||||
#' }
|
||||
#' }
|
||||
ab_class <- function(ab_class,
|
||||
ab_class <- function(ab_class,
|
||||
only_rsi_columns = FALSE,
|
||||
only_treatable = TRUE,
|
||||
...) {
|
||||
@ -205,30 +196,36 @@ ab_class <- function(ab_class,
|
||||
#' @rdname antibiotic_class_selectors
|
||||
#' @details The [ab_selector()] function can be used to internally filter the [antibiotics] data set on any results, see *Examples*. It allows for filtering on a (part of) a certain name, and/or a group name or even a minimum of DDDs for oral treatment. This function yields the highest flexibility, but is also the least user-friendly, since it requires a hard-coded filter to set.
|
||||
#' @export
|
||||
ab_selector <- function(filter,
|
||||
ab_selector <- function(filter,
|
||||
only_rsi_columns = FALSE,
|
||||
only_treatable = TRUE,
|
||||
...) {
|
||||
meet_criteria(only_rsi_columns, allow_class = "logical", has_length = 1)
|
||||
meet_criteria(only_treatable, allow_class = "logical", has_length = 1)
|
||||
|
||||
|
||||
# get_current_data() has to run each time, for cases where e.g., filter() and select() are used in same call
|
||||
# but it only takes a couple of milliseconds
|
||||
vars_df <- get_current_data(arg_name = NA, call = -2)
|
||||
# to improve speed, get_column_abx() will only run once when e.g. in a select or group call
|
||||
ab_in_data <- get_column_abx(vars_df, info = FALSE, only_rsi_columns = only_rsi_columns,
|
||||
sort = FALSE, fn = "ab_selector")
|
||||
ab_in_data <- get_column_abx(vars_df,
|
||||
info = FALSE, only_rsi_columns = only_rsi_columns,
|
||||
sort = FALSE, fn = "ab_selector"
|
||||
)
|
||||
call <- substitute(filter)
|
||||
agents <- tryCatch(AMR::antibiotics[which(eval(call, envir = AMR::antibiotics)), "ab", drop = TRUE],
|
||||
error = function(e) stop_(e$message, call = -5))
|
||||
error = function(e) stop_(e$message, call = -5)
|
||||
)
|
||||
agents <- ab_in_data[ab_in_data %in% agents]
|
||||
message_agent_names(function_name = "ab_selector",
|
||||
agents = agents,
|
||||
ab_group = NULL,
|
||||
examples = "",
|
||||
call = call)
|
||||
message_agent_names(
|
||||
function_name = "ab_selector",
|
||||
agents = agents,
|
||||
ab_group = NULL,
|
||||
examples = "",
|
||||
call = call
|
||||
)
|
||||
structure(unname(agents),
|
||||
class = c("ab_selector", "character"))
|
||||
class = c("ab_selector", "character")
|
||||
)
|
||||
}
|
||||
|
||||
#' @rdname antibiotic_class_selectors
|
||||
@ -419,24 +416,34 @@ administrable_per_os <- function(only_rsi_columns = FALSE, ...) {
|
||||
# but it only takes a couple of milliseconds
|
||||
vars_df <- get_current_data(arg_name = NA, call = -2)
|
||||
# to improve speed, get_column_abx() will only run once when e.g. in a select or group call
|
||||
ab_in_data <- get_column_abx(vars_df, info = FALSE, only_rsi_columns = only_rsi_columns,
|
||||
sort = FALSE, fn = "administrable_per_os")
|
||||
ab_in_data <- get_column_abx(vars_df,
|
||||
info = FALSE, only_rsi_columns = only_rsi_columns,
|
||||
sort = FALSE, fn = "administrable_per_os"
|
||||
)
|
||||
agents_all <- antibiotics[which(!is.na(antibiotics$oral_ddd)), "ab", drop = TRUE]
|
||||
agents <- antibiotics[which(antibiotics$ab %in% ab_in_data & !is.na(antibiotics$oral_ddd)), "ab", drop = TRUE]
|
||||
agents <- ab_in_data[ab_in_data %in% agents]
|
||||
message_agent_names(function_name = "administrable_per_os",
|
||||
agents = agents,
|
||||
ab_group = "administrable_per_os",
|
||||
examples = paste0(" (such as ",
|
||||
vector_or(ab_name(sample(agents_all,
|
||||
size = min(5, length(agents_all)),
|
||||
replace = FALSE),
|
||||
tolower = TRUE,
|
||||
language = NULL),
|
||||
quotes = FALSE),
|
||||
")"))
|
||||
message_agent_names(
|
||||
function_name = "administrable_per_os",
|
||||
agents = agents,
|
||||
ab_group = "administrable_per_os",
|
||||
examples = paste0(
|
||||
" (such as ",
|
||||
vector_or(ab_name(sample(agents_all,
|
||||
size = min(5, length(agents_all)),
|
||||
replace = FALSE
|
||||
),
|
||||
tolower = TRUE,
|
||||
language = NULL
|
||||
),
|
||||
quotes = FALSE
|
||||
),
|
||||
")"
|
||||
)
|
||||
)
|
||||
structure(unname(agents),
|
||||
class = c("ab_selector", "character"))
|
||||
class = c("ab_selector", "character")
|
||||
)
|
||||
}
|
||||
|
||||
#' @rdname antibiotic_class_selectors
|
||||
@ -447,17 +454,22 @@ administrable_iv <- function(only_rsi_columns = FALSE, ...) {
|
||||
# but it only takes a couple of milliseconds
|
||||
vars_df <- get_current_data(arg_name = NA, call = -2)
|
||||
# to improve speed, get_column_abx() will only run once when e.g. in a select or group call
|
||||
ab_in_data <- get_column_abx(vars_df, info = FALSE, only_rsi_columns = only_rsi_columns,
|
||||
sort = FALSE, fn = "administrable_iv")
|
||||
ab_in_data <- get_column_abx(vars_df,
|
||||
info = FALSE, only_rsi_columns = only_rsi_columns,
|
||||
sort = FALSE, fn = "administrable_iv"
|
||||
)
|
||||
agents_all <- antibiotics[which(!is.na(antibiotics$iv_ddd)), "ab", drop = TRUE]
|
||||
agents <- antibiotics[which(antibiotics$ab %in% ab_in_data & !is.na(antibiotics$iv_ddd)), "ab", drop = TRUE]
|
||||
agents <- ab_in_data[ab_in_data %in% agents]
|
||||
message_agent_names(function_name = "administrable_iv",
|
||||
agents = agents,
|
||||
ab_group = "administrable_iv",
|
||||
examples = "")
|
||||
message_agent_names(
|
||||
function_name = "administrable_iv",
|
||||
agents = agents,
|
||||
ab_group = "administrable_iv",
|
||||
examples = ""
|
||||
)
|
||||
structure(unname(agents),
|
||||
class = c("ab_selector", "character"))
|
||||
class = c("ab_selector", "character")
|
||||
)
|
||||
}
|
||||
|
||||
#' @rdname antibiotic_class_selectors
|
||||
@ -470,35 +482,47 @@ not_intrinsic_resistant <- function(only_rsi_columns = FALSE, col_mo = NULL, ver
|
||||
# but it only takes a couple of milliseconds
|
||||
vars_df <- get_current_data(arg_name = NA, call = -2)
|
||||
# to improve speed, get_column_abx() will only run once when e.g. in a select or group call
|
||||
ab_in_data <- get_column_abx(vars_df, info = FALSE, only_rsi_columns = only_rsi_columns,
|
||||
sort = FALSE, fn = "not_intrinsic_resistant")
|
||||
ab_in_data <- get_column_abx(vars_df,
|
||||
info = FALSE, only_rsi_columns = only_rsi_columns,
|
||||
sort = FALSE, fn = "not_intrinsic_resistant"
|
||||
)
|
||||
# intrinsic vars
|
||||
vars_df_R <- tryCatch(sapply(eucast_rules(vars_df,
|
||||
col_mo = col_mo,
|
||||
version_expertrules = version_expertrules,
|
||||
rules = "expert",
|
||||
info = FALSE),
|
||||
function(col) tryCatch(!any(is.na(col)) && all(col == "R"),
|
||||
error = function(e) FALSE)),
|
||||
error = function(e) stop_("in not_intrinsic_resistant(): ", e$message, call = FALSE))
|
||||
|
||||
vars_df_R <- tryCatch(sapply(
|
||||
eucast_rules(vars_df,
|
||||
col_mo = col_mo,
|
||||
version_expertrules = version_expertrules,
|
||||
rules = "expert",
|
||||
info = FALSE
|
||||
),
|
||||
function(col) {
|
||||
tryCatch(!any(is.na(col)) && all(col == "R"),
|
||||
error = function(e) FALSE
|
||||
)
|
||||
}
|
||||
),
|
||||
error = function(e) stop_("in not_intrinsic_resistant(): ", e$message, call = FALSE)
|
||||
)
|
||||
|
||||
agents <- ab_in_data[ab_in_data %in% names(vars_df_R[which(vars_df_R)])]
|
||||
if (length(agents) > 0 &&
|
||||
message_not_thrown_before("not_intrinsic_resistant", sort(agents))) {
|
||||
message_not_thrown_before("not_intrinsic_resistant", sort(agents))) {
|
||||
agents_formatted <- paste0("'", font_bold(agents, collapse = NULL), "'")
|
||||
agents_names <- ab_name(names(agents), tolower = TRUE, language = NULL)
|
||||
need_name <- generalise_antibiotic_name(agents) != generalise_antibiotic_name(agents_names)
|
||||
agents_formatted[need_name] <- paste0(agents_formatted[need_name], " (", agents_names[need_name], ")")
|
||||
message_("For `not_intrinsic_resistant()` removing ",
|
||||
ifelse(length(agents) == 1, "column ", "columns "),
|
||||
vector_and(agents_formatted, quotes = FALSE, sort = FALSE))
|
||||
message_(
|
||||
"For `not_intrinsic_resistant()` removing ",
|
||||
ifelse(length(agents) == 1, "column ", "columns "),
|
||||
vector_and(agents_formatted, quotes = FALSE, sort = FALSE)
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
vars_df_R <- names(vars_df_R)[which(!vars_df_R)]
|
||||
# find columns that are abx, but also intrinsic R
|
||||
out <- unname(intersect(ab_in_data, vars_df_R))
|
||||
structure(out,
|
||||
class = c("ab_selector", "character"))
|
||||
class = c("ab_selector", "character")
|
||||
)
|
||||
}
|
||||
|
||||
ab_select_exec <- function(function_name,
|
||||
@ -509,61 +533,74 @@ ab_select_exec <- function(function_name,
|
||||
# but it only takes a couple of milliseconds
|
||||
vars_df <- get_current_data(arg_name = NA, call = -3)
|
||||
# to improve speed, get_column_abx() will only run once when e.g. in a select or group call
|
||||
ab_in_data <- get_column_abx(vars_df, info = FALSE, only_rsi_columns = only_rsi_columns,
|
||||
sort = FALSE, fn = function_name)
|
||||
ab_in_data <- get_column_abx(vars_df,
|
||||
info = FALSE, only_rsi_columns = only_rsi_columns,
|
||||
sort = FALSE, fn = function_name
|
||||
)
|
||||
# untreatable drugs
|
||||
if (only_treatable == TRUE) {
|
||||
untreatable <- antibiotics[which(antibiotics$name %like% "-high|EDTA|polysorbate|macromethod|screening|/nacubactam"), "ab", drop = TRUE]
|
||||
if (any(untreatable %in% names(ab_in_data))) {
|
||||
if (message_not_thrown_before(function_name, "ab_class", "untreatable", entire_session = TRUE)) {
|
||||
warning_("in `", function_name, "()`: some agents were ignored since they cannot be used for treating patients: ",
|
||||
vector_and(ab_name(names(ab_in_data)[names(ab_in_data) %in% untreatable],
|
||||
language = NULL,
|
||||
tolower = TRUE),
|
||||
quotes = FALSE,
|
||||
sort = TRUE), ". They can be included using `", function_name, "(only_treatable = FALSE)`. ",
|
||||
"This warning will be shown once per session.")
|
||||
warning_(
|
||||
"in `", function_name, "()`: some agents were ignored since they cannot be used for treating patients: ",
|
||||
vector_and(ab_name(names(ab_in_data)[names(ab_in_data) %in% untreatable],
|
||||
language = NULL,
|
||||
tolower = TRUE
|
||||
),
|
||||
quotes = FALSE,
|
||||
sort = TRUE
|
||||
), ". They can be included using `", function_name, "(only_treatable = FALSE)`. ",
|
||||
"This warning will be shown once per session."
|
||||
)
|
||||
}
|
||||
ab_in_data <- ab_in_data[!names(ab_in_data) %in% untreatable]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (length(ab_in_data) == 0) {
|
||||
message_("No antimicrobial agents found in the data.")
|
||||
return(NULL)
|
||||
}
|
||||
|
||||
|
||||
if (is.null(ab_class_args)) {
|
||||
# their upper case equivalent are vectors with class <ab>, created in data-raw/pre-commit-hook.R
|
||||
# their upper case equivalent are vectors with class <ab>, created in data-raw/_pre_commit_hook.R
|
||||
# carbapenems() gets its codes from AMR:::AB_CARBAPENEMS
|
||||
abx <- get(paste0("AB_", toupper(function_name)), envir = asNamespace("AMR"))
|
||||
abx <- get(paste0("AB_", toupper(function_name)), envir = asNamespace("AMR"))
|
||||
ab_group <- function_name
|
||||
examples <- paste0(" (such as ", vector_or(ab_name(sample(abx, size = min(2, length(abx)), replace = FALSE),
|
||||
tolower = TRUE,
|
||||
language = NULL),
|
||||
quotes = FALSE), ")")
|
||||
tolower = TRUE,
|
||||
language = NULL
|
||||
),
|
||||
quotes = FALSE
|
||||
), ")")
|
||||
} else {
|
||||
# this for the 'manual' ab_class() function
|
||||
abx <- subset(AB_lookup,
|
||||
group %like% ab_class_args |
|
||||
atc_group1 %like% ab_class_args |
|
||||
atc_group2 %like% ab_class_args)$ab
|
||||
abx <- subset(
|
||||
AB_lookup,
|
||||
group %like% ab_class_args |
|
||||
atc_group1 %like% ab_class_args |
|
||||
atc_group2 %like% ab_class_args
|
||||
)$ab
|
||||
ab_group <- find_ab_group(ab_class_args)
|
||||
function_name <- "ab_class"
|
||||
examples <- paste0(" (such as ", find_ab_names(ab_class_args, 2), ")")
|
||||
}
|
||||
|
||||
|
||||
# get the columns with a group names in the chosen ab class
|
||||
agents <- ab_in_data[names(ab_in_data) %in% abx]
|
||||
|
||||
message_agent_names(function_name = function_name,
|
||||
agents = agents,
|
||||
ab_group = ab_group,
|
||||
examples = examples,
|
||||
ab_class_args = ab_class_args)
|
||||
|
||||
|
||||
message_agent_names(
|
||||
function_name = function_name,
|
||||
agents = agents,
|
||||
ab_group = ab_group,
|
||||
examples = examples,
|
||||
ab_class_args = ab_class_args
|
||||
)
|
||||
|
||||
structure(unname(agents),
|
||||
class = c("ab_selector", "character"))
|
||||
class = c("ab_selector", "character")
|
||||
)
|
||||
}
|
||||
|
||||
#' @method c ab_selector
|
||||
@ -571,7 +608,8 @@ ab_select_exec <- function(function_name,
|
||||
#' @noRd
|
||||
c.ab_selector <- function(...) {
|
||||
structure(unlist(lapply(list(...), as.character)),
|
||||
class = c("ab_selector", "character"))
|
||||
class = c("ab_selector", "character")
|
||||
)
|
||||
}
|
||||
|
||||
all_any_ab_selector <- function(type, ..., na.rm = TRUE) {
|
||||
@ -583,18 +621,20 @@ all_any_ab_selector <- function(type, ..., na.rm = TRUE) {
|
||||
}
|
||||
cols_ab <- cols_ab[!cols_ab %in% result]
|
||||
df <- get_current_data(arg_name = NA, call = -3)
|
||||
|
||||
|
||||
if (type == "all") {
|
||||
scope_fn <- all
|
||||
} else {
|
||||
scope_fn <- any
|
||||
}
|
||||
|
||||
|
||||
x_transposed <- as.list(as.data.frame(t(df[, cols_ab, drop = FALSE]), stringsAsFactors = FALSE))
|
||||
vapply(FUN.VALUE = logical(1),
|
||||
X = x_transposed,
|
||||
FUN = function(y) scope_fn(y %in% result, na.rm = na.rm),
|
||||
USE.NAMES = FALSE)
|
||||
vapply(
|
||||
FUN.VALUE = logical(1),
|
||||
X = x_transposed,
|
||||
FUN = function(y) scope_fn(y %in% result, na.rm = na.rm),
|
||||
USE.NAMES = FALSE
|
||||
)
|
||||
}
|
||||
|
||||
#' @method all ab_selector
|
||||
@ -654,12 +694,15 @@ any.ab_selector_any_all <- function(..., na.rm = FALSE) {
|
||||
} else {
|
||||
type <- "all"
|
||||
if (length(e1) > 1) {
|
||||
message_("Assuming a filter on ", type, " ", length(e1), " ", gsub("[\\(\\)]", "", fn_name),
|
||||
". Wrap around `all()` or `any()` to prevent this note.")
|
||||
message_(
|
||||
"Assuming a filter on ", type, " ", length(e1), " ", gsub("[\\(\\)]", "", fn_name),
|
||||
". Wrap around `all()` or `any()` to prevent this note."
|
||||
)
|
||||
}
|
||||
}
|
||||
structure(all_any_ab_selector(type = type, e1, e2),
|
||||
class = c("ab_selector_any_all", "logical"))
|
||||
class = c("ab_selector_any_all", "logical")
|
||||
)
|
||||
}
|
||||
|
||||
#' @method != ab_selector
|
||||
@ -676,15 +719,18 @@ any.ab_selector_any_all <- function(..., na.rm = FALSE) {
|
||||
} else {
|
||||
type <- "all"
|
||||
if (length(e1) > 1) {
|
||||
message_("Assuming a filter on ", type, " ", length(e1), " ", gsub("[\\(\\)]", "", fn_name),
|
||||
". Wrap around `all()` or `any()` to prevent this note.")
|
||||
message_(
|
||||
"Assuming a filter on ", type, " ", length(e1), " ", gsub("[\\(\\)]", "", fn_name),
|
||||
". Wrap around `all()` or `any()` to prevent this note."
|
||||
)
|
||||
}
|
||||
}
|
||||
# this is `!=`, so turn around the values
|
||||
rsi <- c("R", "S", "I")
|
||||
e2 <- rsi[rsi != e2]
|
||||
structure(all_any_ab_selector(type = type, e1, e2),
|
||||
class = c("ab_selector_any_all", "logical"))
|
||||
class = c("ab_selector_any_all", "logical")
|
||||
)
|
||||
}
|
||||
|
||||
#' @method & ab_selector
|
||||
@ -694,7 +740,8 @@ any.ab_selector_any_all <- function(..., na.rm = FALSE) {
|
||||
# this is only required for base R, since tidyselect has already implemented this
|
||||
# e.g., for: example_isolates[, penicillins() & administrable_per_os()]
|
||||
structure(intersect(unclass(e1), unclass(e2)),
|
||||
class = c("ab_selector", "character"))
|
||||
class = c("ab_selector", "character")
|
||||
)
|
||||
}
|
||||
#' @method | ab_selector
|
||||
#' @export
|
||||
@ -703,7 +750,8 @@ any.ab_selector_any_all <- function(..., na.rm = FALSE) {
|
||||
# this is only required for base R, since tidyselect has already implemented this
|
||||
# e.g., for: example_isolates[, penicillins() | administrable_per_os()]
|
||||
structure(union(unclass(e1), unclass(e2)),
|
||||
class = c("ab_selector", "character"))
|
||||
class = c("ab_selector", "character")
|
||||
)
|
||||
}
|
||||
|
||||
is_any <- function(el1) {
|
||||
@ -720,38 +768,40 @@ is_all <- function(el1) {
|
||||
find_ab_group <- function(ab_class_args) {
|
||||
ab_class_args <- gsub("[^a-zA-Z0-9]", ".*", ab_class_args)
|
||||
AB_lookup %pm>%
|
||||
subset(group %like% ab_class_args |
|
||||
atc_group1 %like% ab_class_args |
|
||||
atc_group2 %like% ab_class_args) %pm>%
|
||||
subset(group %like% ab_class_args |
|
||||
atc_group1 %like% ab_class_args |
|
||||
atc_group2 %like% ab_class_args) %pm>%
|
||||
pm_pull(group) %pm>%
|
||||
unique() %pm>%
|
||||
tolower() %pm>%
|
||||
sort() %pm>%
|
||||
sort() %pm>%
|
||||
paste(collapse = "/")
|
||||
}
|
||||
|
||||
find_ab_names <- function(ab_group, n = 3) {
|
||||
ab_group <- gsub("[^a-zA-Z|0-9]", ".*", ab_group)
|
||||
|
||||
|
||||
# try popular first, they have DDDs
|
||||
drugs <- antibiotics[which((!is.na(antibiotics$iv_ddd) | !is.na(antibiotics$oral_ddd)) &
|
||||
antibiotics$name %unlike% " " &
|
||||
antibiotics$group %like% ab_group &
|
||||
antibiotics$ab %unlike% "[0-9]$"), ]$name
|
||||
antibiotics$name %unlike% " " &
|
||||
antibiotics$group %like% ab_group &
|
||||
antibiotics$ab %unlike% "[0-9]$"), ]$name
|
||||
if (length(drugs) < n) {
|
||||
# now try it all
|
||||
drugs <- antibiotics[which((antibiotics$group %like% ab_group |
|
||||
antibiotics$atc_group1 %like% ab_group |
|
||||
antibiotics$atc_group2 %like% ab_group) &
|
||||
antibiotics$ab %unlike% "[0-9]$"), ]$name
|
||||
antibiotics$atc_group1 %like% ab_group |
|
||||
antibiotics$atc_group2 %like% ab_group) &
|
||||
antibiotics$ab %unlike% "[0-9]$"), ]$name
|
||||
}
|
||||
if (length(drugs) == 0) {
|
||||
return("??")
|
||||
}
|
||||
vector_or(ab_name(sample(drugs, size = min(n, length(drugs)), replace = FALSE),
|
||||
tolower = TRUE,
|
||||
language = NULL),
|
||||
quotes = FALSE)
|
||||
tolower = TRUE,
|
||||
language = NULL
|
||||
),
|
||||
quotes = FALSE
|
||||
)
|
||||
}
|
||||
|
||||
message_agent_names <- function(function_name, agents, ab_group = NULL, examples = "", ab_class_args = NULL, call = NULL) {
|
||||
@ -771,15 +821,19 @@ message_agent_names <- function(function_name, agents, ab_group = NULL, examples
|
||||
agents_names <- ab_name(names(agents), tolower = TRUE, language = NULL)
|
||||
need_name <- generalise_antibiotic_name(agents) != generalise_antibiotic_name(agents_names)
|
||||
agents_formatted[need_name] <- paste0(agents_formatted[need_name], " (", agents_names[need_name], ")")
|
||||
message_("For `", function_name, "(",
|
||||
ifelse(function_name == "ab_class",
|
||||
paste0("\"", ab_class_args, "\""),
|
||||
ifelse(!is.null(call),
|
||||
paste0(deparse(call), collapse = " "),
|
||||
"")),
|
||||
")` using ",
|
||||
ifelse(length(agents) == 1, "column ", "columns "),
|
||||
vector_and(agents_formatted, quotes = FALSE, sort = FALSE))
|
||||
message_(
|
||||
"For `", function_name, "(",
|
||||
ifelse(function_name == "ab_class",
|
||||
paste0("\"", ab_class_args, "\""),
|
||||
ifelse(!is.null(call),
|
||||
paste0(deparse(call), collapse = " "),
|
||||
""
|
||||
)
|
||||
),
|
||||
")` using ",
|
||||
ifelse(length(agents) == 1, "column ", "columns "),
|
||||
vector_and(agents_formatted, quotes = FALSE, sort = FALSE)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user