italicise antibiogram

This commit is contained in:
dr. M.S. (Matthijs) Berends 2023-02-17 09:42:51 +01:00
parent db2830124f
commit 714a048fa9
5 changed files with 80 additions and 41 deletions

View File

@ -1,6 +1,6 @@
Package: AMR Package: AMR
Version: 1.8.2.9131 Version: 1.8.2.9132
Date: 2023-02-15 Date: 2023-02-17
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.9131 # AMR 1.8.2.9132
*(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

@ -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 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 sep a separating character for antibiotic columns in combination antibiograms
#' @param object an [antibiogram()] object #' @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. #' @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. #' **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,6 +208,15 @@
#' ) #' )
#' ) #' )
#' #'
#' # 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 -------------------------------- #' # Generate plots with ggplot2 or base R --------------------------------
#' #'
@ -221,17 +230,16 @@
#' syndromic_group = "ward" #' syndromic_group = "ward"
#' ) #' )
#' #'
#' plot(ab1)
#'
#' if (requireNamespace("ggplot2")) { #' if (requireNamespace("ggplot2")) {
#' ggplot2::autoplot(ab1) #' ggplot2::autoplot(ab1)
#' } #' }
#'
#' plot(ab2)
#'
#' if (requireNamespace("ggplot2")) { #' if (requireNamespace("ggplot2")) {
#' ggplot2::autoplot(ab2) #' ggplot2::autoplot(ab2)
#' } #' }
#'
#' plot(ab1)
#' plot(ab2)
#'
#' } #' }
antibiogram <- function(x, antibiogram <- function(x,
antibiotics = where(is.sir), antibiotics = where(is.sir),
@ -540,17 +548,36 @@ autoplot.antibiogram <- function(object, ...) {
#' @export #' @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 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.). #' @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 #' @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) meet_criteria(as_kable, allow_class = "logical", has_length = 1)
meet_criteria(italicise, allow_class = "logical", has_length = 1)
kable <- import_fn("kable", "knitr", error_on_fail = FALSE) if (isTRUE(as_kable) &&
if (!is.null(kable) &&
isTRUE(as_kable) &&
# be sure not to run kable in pkgdown for our website generation # be sure not to run kable in pkgdown for our website generation
!identical(Sys.getenv("IN_PKGDOWN"), "true")) { !(missing(as_kable) && identical(Sys.getenv("IN_PKGDOWN"), "true"))) {
kable(x, ...) 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 { } else {
# remove 'antibiogram' class and print with default method # remove 'antibiogram' class and print with default method

View File

@ -85,18 +85,18 @@ antibiogram(example_isolates,
antibiotics = c(aminoglycosides(), carbapenems())) antibiotics = c(aminoglycosides(), carbapenems()))
``` ```
|Pathogen (N min-max) | AMK| GEN| IPM| KAN| MEM| TOB| |Pathogen (N min-max) | AMK| GEN| IPM| KAN| MEM| TOB|
|:----------------------|---:|---:|---:|---:|---:|---:| |:------------------------|---:|---:|---:|---:|---:|---:|
|CoNS (43-309) | 0| 86| 52| 0| 52| 22| |CoNS (43-309) | 0| 86| 52| 0| 52| 22|
|E. coli (0-462) | 100| 98| 100| NA| 100| 97| |*E. coli* (0-462) | 100| 98| 100| | 100| 97|
|E. faecalis (0-39) | 0| 0| 100| 0| NA| 0| |*E. faecalis* (0-39) | 0| 0| 100| 0| | 0|
|K. pneumoniae (0-58) | NA| 90| 100| NA| 100| 90| |*K. pneumoniae* (0-58) | | 90| 100| | 100| 90|
|P. aeruginosa (17-30) | NA| 100| NA| 0| NA| 100| |*P. aeruginosa* (17-30) | | 100| | 0| | 100|
|P. mirabilis (0-34) | NA| 94| 94| NA| NA| 94| |*P. mirabilis* (0-34) | | 94| 94| | | 94|
|S. aureus (2-233) | NA| 99| NA| NA| NA| 98| |*S. aureus* (2-233) | | 99| | | | 98|
|S. epidermidis (8-163) | 0| 79| NA| 0| NA| 51| |*S. epidermidis* (8-163) | 0| 79| | 0| | 51|
|S. hominis (3-80) | NA| 92| NA| NA| NA| 85| |*S. hominis* (3-80) | | 92| | | | 85|
|S. pneumoniae (11-117) | 0| 0| NA| 0| NA| 0| |*S. pneumoniae* (11-117) | 0| 0| | 0| | 0|
In combination antibiograms, it is clear that combined antibiotics yield higher empiric coverage: 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-negative (641-693) | 88| 99| 98|
|Gram-positive (345-1044) | 86| 98| 95| |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 ```r
antibiogram(example_isolates, antibiogram(example_isolates,
@ -129,6 +129,8 @@ antibiogram(example_isolates,
#### Calculating resistance per group #### Calculating resistance per group
For a manual approach, you can use the `resistance` or `susceptibility()` function:
```r ```r
library(AMR) library(AMR)
library(dplyr) library(dplyr)
@ -147,7 +149,7 @@ out
|:-----------|------:|------:|------:|------:|------:| |:-----------|------:|------:|------:|------:|------:|
| Clinical | 0.229 | 0.315 | 0.626 | 1 | 0.780 | | Clinical | 0.229 | 0.315 | 0.626 | 1 | 0.780 |
| ICU | 0.290 | 0.400 | 0.662 | 1 | 0.857 | | 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 ```r
# transform the antibiotic columns to names: # transform the antibiotic columns to names:
@ -158,7 +160,7 @@ out %>% set_ab_names()
|:-----------|-----------:|-----------:|----------|----------:|----------:| |:-----------|-----------:|-----------:|----------|----------:|----------:|
| Clinical | 0.229 | 0.315 | 0.626 | 1 | 0.780 | | Clinical | 0.229 | 0.315 | 0.626 | 1 | 0.780 |
| ICU | 0.290 | 0.400 | 0.662 | 1 | 0.857 | | 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 ```r
# transform the antibiotic column to ATC codes: # 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 | | Clinical | 0.229 | 0.315 | 0.626 | 1 | 0.780 |
| ICU | 0.290 | 0.400 | 0.662 | 1 | 0.857 | | 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? ### What else can you do with this package?

View File

@ -34,7 +34,7 @@ antibiogram(
\method{autoplot}{antibiogram}(object, ...) \method{autoplot}{antibiogram}(object, ...)
\method{print}{antibiogram}(x, as_kable = !interactive(), ...) \method{print}{antibiogram}(x, as_kable = !interactive(), italicise = TRUE, ...)
} }
\arguments{ \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()}})} \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{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{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{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{ \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. 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 -------------------------------- # Generate plots with ggplot2 or base R --------------------------------
@ -238,16 +249,15 @@ ab2 <- antibiogram(example_isolates,
syndromic_group = "ward" syndromic_group = "ward"
) )
plot(ab1)
if (requireNamespace("ggplot2")) { if (requireNamespace("ggplot2")) {
ggplot2::autoplot(ab1) ggplot2::autoplot(ab1)
} }
plot(ab2)
if (requireNamespace("ggplot2")) { if (requireNamespace("ggplot2")) {
ggplot2::autoplot(ab2) ggplot2::autoplot(ab2)
} }
plot(ab1)
plot(ab2)
} }
} }