1
0
mirror of https://github.com/msberends/AMR.git synced 2025-01-24 04:24:34 +01:00

Fix for antibiogram() in R <=3.4

This commit is contained in:
dr. M.S. (Matthijs) Berends 2023-02-13 10:21:43 +01:00
parent 45a9697c84
commit b6d2b1398d
5 changed files with 40 additions and 37 deletions

View File

@ -1,6 +1,6 @@
Package: AMR Package: AMR
Version: 1.8.2.9121 Version: 1.8.2.9122
Date: 2023-02-12 Date: 2023-02-13
Title: Antimicrobial Resistance Data Analysis Title: Antimicrobial Resistance Data Analysis
Description: Functions to simplify and standardise antimicrobial resistance (AMR) Description: Functions to simplify and standardise antimicrobial resistance (AMR)
data analysis and to work with microbial and antimicrobial properties by data analysis and to work with microbial and antimicrobial properties by

View File

@ -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!)* *(this beta version will eventually become v2.0! We're happy to reach a new major milestone soon!)*

View File

@ -29,7 +29,7 @@
#' Generate Antibiogram: Traditional, Combined, Syndromic, or Weighted-Incidence Syndromic Combination (WISCA) #' 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 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 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. #' @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: #' Code example:
#' #'
#' ```r #' ```r
#' antibiogram(your_data, #' library(dplyr)
#' antibiotics = c("TZP", "TZP+TOB", "TZP+GEN"), #' your_data %>%
#' syndromic_group = ifelse(your_data$age >= 65 & your_data$gender == "Male", #' filter(ward == "ICU" & specimen_type == "Respiratory") %>%
#' "Group 1", "Group 2")) #' 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()`. #' 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) --------- #' # 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, #' antibiogram(example_isolates,
#' antibiotics = c("AMC", "AMC+CIP", "TZP", "TZP+TOB"), #' antibiotics = c("AMC", "AMC+CIP", "TZP", "TZP+TOB"),
#' mo_transform = "gramstain", #' 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 & #' syndromic_group = ifelse(example_isolates$age >= 65 &
#' example_isolates$gender == "M", #' example_isolates$gender == "M",
#' "WISCA Group 1", "WISCA Group 2" #' "WISCA Group 1", "WISCA Group 2"
@ -349,6 +353,17 @@ antibiogram <- function(x,
) )
counts <- out 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 # regroup for summarising
if (isTRUE(has_syndromic_group)) { if (isTRUE(has_syndromic_group)) {
colnames(out)[1] <- "syndromic_group" colnames(out)[1] <- "syndromic_group"
@ -358,19 +373,6 @@ antibiogram <- function(x,
out <- out %pm>% out <- out %pm>%
pm_group_by(mo, ab) 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>% out <- out %pm>%
pm_summarise(SI = numerator / total) pm_summarise(SI = numerator / total)

View File

@ -124,8 +124,8 @@ expect_identical(as.character(as.mo(" ")), NA_character_)
expect_warning(as.mo("ab")) expect_warning(as.mo("ab"))
expect_identical( expect_identical(
suppressWarnings(as.character(as.mo(c("Qq species", "", "MRSA", "K. pneu rhino", "esco")))), 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") c("UNKNOWN", "B_STPHY_AURS", "B_KLBSL_PNMN_RHNS", "B_ESCHR_COLI")
) )
# check for Becker classification # check for Becker classification
@ -317,10 +317,7 @@ expect_warning(x[[1]] <- "invalid code")
expect_warning(c(x[1], "test")) expect_warning(c(x[1], "test"))
# ignoring patterns # ignoring patterns
expect_identical( expect_true(is.na(as.mo("E. coli ignorethis", ignore_pattern = "this")))
as.character(as.mo(c("E. coli", "E. coli ignorethis"), ignore_pattern = "this")),
c("B_ESCHR_COLI", NA)
)
# frequency tables # frequency tables
if (AMR:::pkg_is_available("cleaner")) { if (AMR:::pkg_is_available("cleaner")) {

View File

@ -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)} \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{ \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{ \details{
This function returns a table with values between 0 and 100 for \emph{susceptibility}, not resistance. 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: Code example:
\if{html}{\out{<div class="sourceCode r">}}\preformatted{antibiogram(your_data, \if{html}{\out{<div class="sourceCode r">}}\preformatted{library(dplyr)
antibiotics = c("TZP", "TZP+TOB", "TZP+GEN"), your_data \%>\%
syndromic_group = ifelse(your_data$age >= 65 & your_data$gender == "Male", filter(ward == "ICU" & specimen_type == "Respiratory") \%>\%
"Group 1", "Group 2")) 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{</div>}} }\if{html}{\out{</div>}}
} }
@ -210,11 +214,11 @@ antibiogram(ex1,
# Weighted-incidence syndromic combination antibiogram (WISCA) --------- # 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, antibiogram(example_isolates,
antibiotics = c("AMC", "AMC+CIP", "TZP", "TZP+TOB"), antibiotics = c("AMC", "AMC+CIP", "TZP", "TZP+TOB"),
mo_transform = "gramstain", 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 & syndromic_group = ifelse(example_isolates$age >= 65 &
example_isolates$gender == "M", example_isolates$gender == "M",
"WISCA Group 1", "WISCA Group 2" "WISCA Group 1", "WISCA Group 2"