From b6d2b1398d4037922ab5c99b2a2d98ef731bb8f0 Mon Sep 17 00:00:00 2001 From: "Matthijs S. Berends" Date: Mon, 13 Feb 2023 10:21:43 +0100 Subject: [PATCH] Fix for `antibiogram()` in R <=3.4 --- DESCRIPTION | 4 ++-- NEWS.md | 2 +- R/antibiogram.R | 44 +++++++++++++++++++++-------------------- inst/tinytest/test-mo.R | 9 +++------ man/antibiogram.Rd | 18 ++++++++++------- 5 files changed, 40 insertions(+), 37 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 126d69e7e..7f0155837 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: AMR -Version: 1.8.2.9121 -Date: 2023-02-12 +Version: 1.8.2.9122 +Date: 2023-02-13 Title: Antimicrobial Resistance Data Analysis Description: Functions to simplify and standardise antimicrobial resistance (AMR) data analysis and to work with microbial and antimicrobial properties by diff --git a/NEWS.md b/NEWS.md index 6fe9460bf..6dbd1b9af 100755 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,4 @@ -# AMR 1.8.2.9121 +# AMR 1.8.2.9122 *(this beta version will eventually become v2.0! We're happy to reach a new major milestone soon!)* diff --git a/R/antibiogram.R b/R/antibiogram.R index b3bca975f..f2bd4af2d 100755 --- a/R/antibiogram.R +++ b/R/antibiogram.R @@ -29,7 +29,7 @@ #' Generate Antibiogram: Traditional, Combined, Syndromic, or Weighted-Incidence Syndromic Combination (WISCA) #' -#' Generate an antibiogram, and communicate the results in plots or tables. These functions follow the logic of Klinker *et al.* (2021, \doi{10.1177/20499361211011373}) and Barbieri *et al.* (2021, \doi{10.1186/s13756-021-00939-2}), and allow reporting in e.g. R Markdown and Quarto as well. +#' Generate an antibiogram, and communicate the results in plots or tables. These functions follow the logic of Klinker *et al.* and Barbieri *et al.* (see *Source*), and allow reporting in e.g. R Markdown and Quarto as well. #' @param x a [data.frame] containing at least a column with microorganisms and columns with antibiotic results (class 'sir', see [as.sir()]) #' @param antibiotics vector of column names, or (any combinations of) [antibiotic selectors][antibiotic_class_selectors] such as [aminoglycosides()] or [carbapenems()]. For combination antibiograms, this can also be column names separated with `"+"`, such as "TZP+TOB" given that the data set contains columns "TZP" and "TOB". See *Examples*. #' @param mo_transform a character to transform microorganism input - must be "name", "shortname", "gramstain", or one of the column names of the [microorganisms] data set: `r vector_or(colnames(microorganisms), sort = FALSE, quotes = TRUE)`. Can also be `NULL` to not transform the input. @@ -92,10 +92,14 @@ #' Code example: #' #' ```r -#' antibiogram(your_data, -#' antibiotics = c("TZP", "TZP+TOB", "TZP+GEN"), -#' syndromic_group = ifelse(your_data$age >= 65 & your_data$gender == "Male", -#' "Group 1", "Group 2")) +#' library(dplyr) +#' your_data %>% +#' filter(ward == "ICU" & specimen_type == "Respiratory") %>% +#' antibiogram(antibiotics = c("TZP", "TZP+TOB", "TZP+GEN"), +#' syndromic_group = ifelse(.$age >= 65 & +#' .$gender == "Male" & +#' .$condition == "Heart Disease", +#' "Study Group", "Control Group")) #' ``` #' #' All types of antibiograms can be generated with the functions as described on this page, and can be plotted (using [ggplot2::autoplot()] or base \R [plot()]/[barplot()]) or printed into R Markdown / Quarto formats for reports. Use functions from specific 'table reporting' packages to transform the output of [antibiogram()] to your needs, e.g. `flextable::as_flextable()` or `gt::gt()`. @@ -193,11 +197,11 @@ #' #' # Weighted-incidence syndromic combination antibiogram (WISCA) --------- #' -#' # the data set could contain a filter for e.g. respiratory specimens +#' # the data set could contain a filter for e.g. respiratory specimens/ICU #' antibiogram(example_isolates, #' antibiotics = c("AMC", "AMC+CIP", "TZP", "TZP+TOB"), #' mo_transform = "gramstain", -#' minimum = 10, # this should be >= 30, but now just as example +#' minimum = 10, # this should be >=30, but now just as example #' syndromic_group = ifelse(example_isolates$age >= 65 & #' example_isolates$gender == "M", #' "WISCA Group 1", "WISCA Group 2" @@ -348,7 +352,18 @@ antibiogram <- function(x, FUN = function(x) x ) counts <- out - + + if (isTRUE(combine_SI)) { + out$numerator <- out$S + out$I + } else { + out$numerator <- out$S + } + if (any(out$total < minimum, na.rm = TRUE)) { + message_("NOTE: ", sum(out$total < minimum, na.rm = TRUE), " combinations had less than `minimum = ", minimum, "` results and were ignored", add_fn = font_red) + out <- out %pm>% + subset(total >= minimum) + } + # regroup for summarising if (isTRUE(has_syndromic_group)) { colnames(out)[1] <- "syndromic_group" @@ -358,19 +373,6 @@ antibiogram <- function(x, out <- out %pm>% pm_group_by(mo, ab) } - - if (isTRUE(combine_SI)) { - out$numerator <- out$S + out$I - } else { - out$numerator <- out$S - } - out$minimum <- minimum - if (any(out$total < out$minimum, na.rm = TRUE)) { - message_("NOTE: ", sum(out$total < out$minimum, na.rm = TRUE), " combinations had less than `minimum = ", minimum, "` results and were ignored", add_fn = font_red) - out <- out %pm>% - subset(total >= minimum) - } - out <- out %pm>% pm_summarise(SI = numerator / total) diff --git a/inst/tinytest/test-mo.R b/inst/tinytest/test-mo.R index 01c130db3..ec961dd50 100644 --- a/inst/tinytest/test-mo.R +++ b/inst/tinytest/test-mo.R @@ -124,8 +124,8 @@ expect_identical(as.character(as.mo(" ")), NA_character_) expect_warning(as.mo("ab")) expect_identical( - suppressWarnings(as.character(as.mo(c("Qq species", "", "MRSA", "K. pneu rhino", "esco")))), - c("UNKNOWN", NA_character_, "B_STPHY_AURS", "B_KLBSL_PNMN_RHNS", "B_ESCHR_COLI") + suppressWarnings(as.character(as.mo(c("Qq species", "MRSA", "K. pneu rhino", "esco")))), + c("UNKNOWN", "B_STPHY_AURS", "B_KLBSL_PNMN_RHNS", "B_ESCHR_COLI") ) # check for Becker classification @@ -317,10 +317,7 @@ expect_warning(x[[1]] <- "invalid code") expect_warning(c(x[1], "test")) # ignoring patterns -expect_identical( - as.character(as.mo(c("E. coli", "E. coli ignorethis"), ignore_pattern = "this")), - c("B_ESCHR_COLI", NA) -) +expect_true(is.na(as.mo("E. coli ignorethis", ignore_pattern = "this"))) # frequency tables if (AMR:::pkg_is_available("cleaner")) { diff --git a/man/antibiogram.Rd b/man/antibiogram.Rd index a082a6211..310eddbc5 100644 --- a/man/antibiogram.Rd +++ b/man/antibiogram.Rd @@ -70,7 +70,7 @@ antibiogram( \item{as_kable}{a \link{logical} to indicate whether the printing should be done using \code{\link[knitr:kable]{knitr::kable()}} (which is the default in non-interactive sessions)} } \description{ -Generate an antibiogram, and communicate the results in plots or tables. These functions follow the logic of Klinker \emph{et al.} (2021, \doi{10.1177/20499361211011373}) and Barbieri \emph{et al.} (2021, \doi{10.1186/s13756-021-00939-2}), and allow reporting in e.g. R Markdown and Quarto as well. +Generate an antibiogram, and communicate the results in plots or tables. These functions follow the logic of Klinker \emph{et al.} and Barbieri \emph{et al.} (see \emph{Source}), and allow reporting in e.g. R Markdown and Quarto as well. } \details{ This function returns a table with values between 0 and 100 for \emph{susceptibility}, not resistance. @@ -113,10 +113,14 @@ Case example: Susceptibility of \emph{Pseudomonas aeruginosa} to TZP among respi Code example: -\if{html}{\out{
}}\preformatted{antibiogram(your_data, - antibiotics = c("TZP", "TZP+TOB", "TZP+GEN"), - syndromic_group = ifelse(your_data$age >= 65 & your_data$gender == "Male", - "Group 1", "Group 2")) +\if{html}{\out{
}}\preformatted{library(dplyr) +your_data \%>\% + filter(ward == "ICU" & specimen_type == "Respiratory") \%>\% + antibiogram(antibiotics = c("TZP", "TZP+TOB", "TZP+GEN"), + syndromic_group = ifelse(.$age >= 65 & + .$gender == "Male" & + .$condition == "Heart Disease", + "Study Group", "Control Group")) }\if{html}{\out{
}} } @@ -210,11 +214,11 @@ antibiogram(ex1, # Weighted-incidence syndromic combination antibiogram (WISCA) --------- -# the data set could contain a filter for e.g. respiratory specimens +# the data set could contain a filter for e.g. respiratory specimens/ICU antibiogram(example_isolates, antibiotics = c("AMC", "AMC+CIP", "TZP", "TZP+TOB"), mo_transform = "gramstain", - minimum = 10, # this should be >= 30, but now just as example + minimum = 10, # this should be >=30, but now just as example syndromic_group = ifelse(example_isolates$age >= 65 & example_isolates$gender == "M", "WISCA Group 1", "WISCA Group 2"