From 714a048fa9bb1c498dd7982848a1f763234fcedc Mon Sep 17 00:00:00 2001 From: "Matthijs S. Berends" Date: Fri, 17 Feb 2023 09:42:51 +0100 Subject: [PATCH] italicise antibiogram --- DESCRIPTION | 4 ++-- NEWS.md | 2 +- R/antibiogram.R | 57 ++++++++++++++++++++++++++++++++++------------ index.md | 34 ++++++++++++++------------- man/antibiogram.Rd | 24 +++++++++++++------ 5 files changed, 80 insertions(+), 41 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 9a8f6cee..49d7d84a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: AMR -Version: 1.8.2.9131 -Date: 2023-02-15 +Version: 1.8.2.9132 +Date: 2023-02-17 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 01fc31c1..97956eb6 100755 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,4 @@ -# AMR 1.8.2.9131 +# AMR 1.8.2.9132 *(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 97893ce4..a6816979 100755 --- a/R/antibiogram.R +++ b/R/antibiogram.R @@ -44,7 +44,7 @@ #' @param combine_SI a [logical] to indicate whether all susceptibility should be determined by results of either S or I, instead of only S (defaults to `TRUE`) #' @param sep a separating character for antibiotic columns in combination antibiograms #' @param object an [antibiogram()] object -#' @param ... method extensions +#' @param ... when used in [print()]: arguments passed on to [knitr::kable()] (otherwise, has no use) #' @details This function returns a table with values between 0 and 100 for *susceptibility*, not resistance. #' #' **Remember that you should filter your data to let it contain only first isolates!** This is needed to exclude duplicates and to reduce selection bias. Use [first_isolate()] to determine them in your data set with one of the four available algorithms. @@ -208,7 +208,16 @@ #' ) #' ) #' -#' +#' # Print the output for R Markdown / Quarto ----------------------------- +#' +#' ureido <- antibiogram(example_isolates, +#' antibiotics = ureidopenicillins(), +#' ab_transform = "name") +#' +#' # in an Rmd file, you would just need print(ureido), but to be explicit: +#' print(ureido, as_kable = TRUE, format = "markdown", italicise = TRUE) +#' +#' #' # Generate plots with ggplot2 or base R -------------------------------- #' #' ab1 <- antibiogram(example_isolates, @@ -221,17 +230,16 @@ #' syndromic_group = "ward" #' ) #' -#' plot(ab1) -#' #' if (requireNamespace("ggplot2")) { #' ggplot2::autoplot(ab1) #' } -#' -#' plot(ab2) -#' #' if (requireNamespace("ggplot2")) { #' ggplot2::autoplot(ab2) #' } +#' +#' plot(ab1) +#' plot(ab2) +#' #' } antibiogram <- function(x, antibiotics = where(is.sir), @@ -540,18 +548,37 @@ autoplot.antibiogram <- function(object, ...) { #' @export #' @param as_kable a [logical] to indicate whether the printing should be done using [knitr::kable()] (which is the default in non-interactive sessions) +#' @param italicise a [logical] to indicate whether the microorganism names in the output table should be made italic, using [italicise_taxonomy()]. This only works when the output format is markdown, such as in HTML output. #' @details Printing the antibiogram in non-interactive sessions will be done by [knitr::kable()], with support for [all their implemented formats][knitr::kable()], such as "markdown". The knitr format will be automatically determined if printed inside a knitr document (LaTeX, HTML, etc.). #' @rdname antibiogram -print.antibiogram <- function(x, as_kable = !interactive(), ...) { +print.antibiogram <- function(x, as_kable = !interactive(), italicise = TRUE, ...) { meet_criteria(as_kable, allow_class = "logical", has_length = 1) - - kable <- import_fn("kable", "knitr", error_on_fail = FALSE) - if (!is.null(kable) && - isTRUE(as_kable) && + meet_criteria(italicise, allow_class = "logical", has_length = 1) + + if (isTRUE(as_kable) && # be sure not to run kable in pkgdown for our website generation - !identical(Sys.getenv("IN_PKGDOWN"), "true")) { - kable(x, ...) - + !(missing(as_kable) && identical(Sys.getenv("IN_PKGDOWN"), "true"))) { + out <- knitr::kable(x, ...) + format <- attributes(out)$format + if (!is.null(format) && format %in% c("markdown", "pipe")) { + # try to italicise the output + rows_with_txt <- which(out %like% "[a-z]") + rows_without_txt <- setdiff(seq_len(length(out)), rows_with_txt) + out[rows_with_txt] <- gsub("^[|]", "| ", out[rows_with_txt]) + # put hyphen directly after second character + out[rows_without_txt] <- gsub("^[|](.)", "|\\1-", out[rows_without_txt]) + out_ita <- italicise_taxonomy(as.character(out), type = "markdown") + if (length(unique(nchar(out_ita))) != 1) { + # so there has been alterations done by italicise_taxonomy() + to_fill <- which(nchar(out_ita) < max(nchar(out_ita))) + out_ita[intersect(to_fill, rows_with_txt)] <- gsub("(^[|].*?)([|])(.*)", "\\1 \\2\\3", out_ita[intersect(to_fill, rows_with_txt)], perl = TRUE) + out_ita[intersect(to_fill, rows_without_txt)] <- gsub("(^[|].*?)([|])(.*)", "\\1--\\2\\3", out_ita[intersect(to_fill, rows_without_txt)], perl = TRUE) + } + attributes(out_ita) <- attributes(out) + out <- out_ita + } + out + } else { # remove 'antibiogram' class and print with default method class(x) <- class(x)[class(x) != "antibiogram"] diff --git a/index.md b/index.md index 312b0696..93bf3d9b 100644 --- a/index.md +++ b/index.md @@ -85,18 +85,18 @@ antibiogram(example_isolates, antibiotics = c(aminoglycosides(), carbapenems())) ``` -|Pathogen (N min-max) | AMK| GEN| IPM| KAN| MEM| TOB| -|:----------------------|---:|---:|---:|---:|---:|---:| -|CoNS (43-309) | 0| 86| 52| 0| 52| 22| -|E. coli (0-462) | 100| 98| 100| NA| 100| 97| -|E. faecalis (0-39) | 0| 0| 100| 0| NA| 0| -|K. pneumoniae (0-58) | NA| 90| 100| NA| 100| 90| -|P. aeruginosa (17-30) | NA| 100| NA| 0| NA| 100| -|P. mirabilis (0-34) | NA| 94| 94| NA| NA| 94| -|S. aureus (2-233) | NA| 99| NA| NA| NA| 98| -|S. epidermidis (8-163) | 0| 79| NA| 0| NA| 51| -|S. hominis (3-80) | NA| 92| NA| NA| NA| 85| -|S. pneumoniae (11-117) | 0| 0| NA| 0| NA| 0| +|Pathogen (N min-max) | AMK| GEN| IPM| KAN| MEM| TOB| +|:------------------------|---:|---:|---:|---:|---:|---:| +|CoNS (43-309) | 0| 86| 52| 0| 52| 22| +|*E. coli* (0-462) | 100| 98| 100| | 100| 97| +|*E. faecalis* (0-39) | 0| 0| 100| 0| | 0| +|*K. pneumoniae* (0-58) | | 90| 100| | 100| 90| +|*P. aeruginosa* (17-30) | | 100| | 0| | 100| +|*P. mirabilis* (0-34) | | 94| 94| | | 94| +|*S. aureus* (2-233) | | 99| | | | 98| +|*S. epidermidis* (8-163) | 0| 79| | 0| | 51| +|*S. hominis* (3-80) | | 92| | | | 85| +|*S. pneumoniae* (11-117) | 0| 0| | 0| | 0| In combination antibiograms, it is clear that combined antibiotics yield higher empiric coverage: @@ -111,7 +111,7 @@ antibiogram(example_isolates, |Gram-negative (641-693) | 88| 99| 98| |Gram-positive (345-1044) | 86| 98| 95| -Like many other functions in this package, `antibiograms()` comes with support for 20 languages that are often detected automatically based on system language: +Like many other functions in this package, `antibiogram()` comes with support for 20 languages that are often detected automatically based on system language: ```r antibiogram(example_isolates, @@ -129,6 +129,8 @@ antibiogram(example_isolates, #### Calculating resistance per group +For a manual approach, you can use the `resistance` or `susceptibility()` function: + ```r library(AMR) library(dplyr) @@ -147,7 +149,7 @@ out |:-----------|------:|------:|------:|------:|------:| | Clinical | 0.229 | 0.315 | 0.626 | 1 | 0.780 | | ICU | 0.290 | 0.400 | 0.662 | 1 | 0.857 | -| Outpatient | 0.200 | 0.368 | 0.605 | NA | 0.889 | +| Outpatient | 0.200 | 0.368 | 0.605 | | 0.889 | ```r # transform the antibiotic columns to names: @@ -158,7 +160,7 @@ out %>% set_ab_names() |:-----------|-----------:|-----------:|----------|----------:|----------:| | Clinical | 0.229 | 0.315 | 0.626 | 1 | 0.780 | | ICU | 0.290 | 0.400 | 0.662 | 1 | 0.857 | -| Outpatient | 0.200 | 0.368 | 0.605 | NA | 0.889 | +| Outpatient | 0.200 | 0.368 | 0.605 | | 0.889 | ```r # transform the antibiotic column to ATC codes: @@ -169,7 +171,7 @@ out %>% set_ab_names(property = "atc") |:-----------|-----------:|-----------:|----------|----------:|----------:| | Clinical | 0.229 | 0.315 | 0.626 | 1 | 0.780 | | ICU | 0.290 | 0.400 | 0.662 | 1 | 0.857 | -| Outpatient | 0.200 | 0.368 | 0.605 | NA | 0.889 | +| Outpatient | 0.200 | 0.368 | 0.605 | | 0.889 | ### What else can you do with this package? diff --git a/man/antibiogram.Rd b/man/antibiogram.Rd index a02c14b6..ced05dd7 100644 --- a/man/antibiogram.Rd +++ b/man/antibiogram.Rd @@ -34,7 +34,7 @@ antibiogram( \method{autoplot}{antibiogram}(object, ...) -\method{print}{antibiogram}(x, as_kable = !interactive(), ...) +\method{print}{antibiogram}(x, as_kable = !interactive(), italicise = TRUE, ...) } \arguments{ \item{x}{a \link{data.frame} containing at least a column with microorganisms and columns with antibiotic results (class 'sir', see \code{\link[=as.sir]{as.sir()}})} @@ -63,11 +63,13 @@ antibiogram( \item{sep}{a separating character for antibiotic columns in combination antibiograms} -\item{...}{method extensions} +\item{...}{when used in \code{\link[=print]{print()}}: arguments passed on to \code{\link[knitr:kable]{knitr::kable()}} (otherwise, has no use)} \item{object}{an \code{\link[=antibiogram]{antibiogram()}} object} \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{italicise}{a \link{logical} to indicate whether the microorganism names in the output table should be made italic, using \code{\link[=italicise_taxonomy]{italicise_taxonomy()}}. This only works when the output format is markdown, such as in HTML output.} } \description{ 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. @@ -225,6 +227,15 @@ antibiogram(example_isolates, ) ) +# Print the output for R Markdown / Quarto ----------------------------- + +ureido <- antibiogram(example_isolates, + antibiotics = ureidopenicillins(), + ab_transform = "name") + +# in an Rmd file, you would just need print(ureido), but to be explicit: +print(ureido, as_kable = TRUE, format = "markdown", italicise = TRUE) + # Generate plots with ggplot2 or base R -------------------------------- @@ -238,16 +249,15 @@ ab2 <- antibiogram(example_isolates, syndromic_group = "ward" ) -plot(ab1) - if (requireNamespace("ggplot2")) { ggplot2::autoplot(ab1) } - -plot(ab2) - if (requireNamespace("ggplot2")) { ggplot2::autoplot(ab2) } + +plot(ab1) +plot(ab2) + } }