diff --git a/.Rbuildignore b/.Rbuildignore index dbd2c8e7..7f1c8569 100755 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -2,3 +2,4 @@ ^\.Rproj\.user$ .travis.yml .zenodo.json +^cran-comments\.md$ diff --git a/.gitignore b/.gitignore index a431e20d..304c2613 100755 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ inst/doc *.dll vignettes/*.R .DS_Store +^cran-comments\.md$ diff --git a/DESCRIPTION b/DESCRIPTION index 53375f98..d6d8fd22 100755 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: AMR -Version: 0.2.0.9022 -Date: 2018-08-10 +Version: 0.2.0.9023 +Date: 2018-08-11 Title: Antimicrobial Resistance Analysis Authors@R: c( person( @@ -46,7 +46,7 @@ Description: Functions to simplify the analysis of Antimicrobial Resistance (AMR on antibiograms according to Leclercq (2013) . Depends: - R (>= 3.0.0) + R (>= 3.1.0) Imports: backports, clipr, @@ -57,19 +57,18 @@ Imports: Rcpp (>= 0.12.14), readr, rvest (>= 0.3.2), - tibble + tibble, + ggplot2 Suggests: testthat (>= 1.0.2), covr (>= 3.0.1), rmarkdown, rstudioapi, - tidyr, - ggplot2 -LinkingTo: Rcpp + tidyr VignetteBuilder: knitr URL: https://github.com/msberends/AMR BugReports: https://github.com/msberends/AMR/issues License: GPL-2 | file LICENSE Encoding: UTF-8 LazyData: true -RoxygenNote: 6.0.1.9000 +RoxygenNote: 6.1.0 diff --git a/NAMESPACE b/NAMESPACE index cd7fc56e..93f122d9 100755 --- a/NAMESPACE +++ b/NAMESPACE @@ -43,11 +43,14 @@ export(atc_groups) export(atc_property) export(clipboard_export) export(clipboard_import) +export(facet_rsi) export(first_isolate) export(freq) export(frequency_tbl) export(full_join_microorganisms) export(g.test) +export(geom_rsi) +export(ggplot_rsi) export(guess_atc) export(guess_bactid) export(inner_join_microorganisms) @@ -68,13 +71,17 @@ export(portion_IR) export(portion_R) export(portion_S) export(portion_SI) +export(portion_df) export(ratio) export(resistance_predict) export(right_join_microorganisms) export(rsi) export(rsi_predict) +export(scale_rsi_colours) +export(scale_y_percent) export(semi_join_microorganisms) export(skewness) +export(theme_rsi) export(top_freq) exportMethods(as.data.frame.bactid) exportMethods(as.data.frame.frequency_tbl) @@ -105,7 +112,6 @@ exportMethods(skewness.default) exportMethods(skewness.matrix) exportMethods(summary.mic) exportMethods(summary.rsi) -importFrom(Rcpp,evalCpp) importFrom(clipr,read_clip_tbl) importFrom(clipr,write_clip) importFrom(curl,nslookup) @@ -114,6 +120,7 @@ importFrom(dplyr,arrange) importFrom(dplyr,arrange_at) importFrom(dplyr,as_tibble) importFrom(dplyr,between) +importFrom(dplyr,bind_cols) importFrom(dplyr,case_when) importFrom(dplyr,desc) importFrom(dplyr,filter) @@ -130,6 +137,7 @@ importFrom(dplyr,row_number) importFrom(dplyr,select) importFrom(dplyr,slice) importFrom(dplyr,summarise) +importFrom(dplyr,summarise_if) importFrom(dplyr,tibble) importFrom(dplyr,top_n) importFrom(grDevices,boxplot.stats) @@ -161,4 +169,3 @@ importFrom(utils,object.size) importFrom(utils,read.delim) importFrom(utils,write.table) importFrom(xml2,read_html) -useDynLib(AMR, .registration = TRUE) diff --git a/NEWS.md b/NEWS.md index 6c0e033a..6ca03b69 100755 --- a/NEWS.md +++ b/NEWS.md @@ -1,12 +1,16 @@ # 0.2.0.90xx (development version) -**Published on CRAN: (unpublished)** #### New -* **BREAKING**: `rsi_df` was removed in favour of new functions `portion_R`, `portion_IR`, `portion_I`, `portion_SI` and `portion_S` to selectively calculate resistance or susceptibility. These functions use **hybrid evaluation**, which means that calculations are not done in R directly but rather in C++ using the `Rcpp` package, making them 20 to 30 times faster. The function `rsi` still works, but is deprecated. +* **BREAKING**: `rsi_df` was removed in favour of new functions `portion_R`, `portion_IR`, `portion_I`, `portion_SI` and `portion_S` to selectively calculate resistance or susceptibility. These functions are 20 to 30 times faster than the old `rsi` function. The old function still works, but is deprecated. + * New function `portion_df` to get all portions of S, I and R of a data set with antibiotic columns * **BREAKING**: the methodology for determining first weighted isolates was changed. The antibiotics that are compared between isolates (call *key antibiotics*) to include more first isolates (afterwards called first *weighted* isolates) are now as follows: * Universal: amoxicillin, amoxicillin/clavlanic acid, cefuroxime, piperacillin/tazobactam, ciprofloxacin, trimethoprim/sulfamethoxazole * Gram-positive: vancomycin, teicoplanin, tetracycline, erythromycin, oxacillin, rifampicin * Gram-negative: gentamicin, tobramycin, colistin, cefotaxime, ceftazidime, meropenem +* Support for `ggplot2` + * New functions `geom_rsi`, `facet_rsi`, `scale_y_percent`, `scale_rsi_colours` and `theme_rsi` + * New wrapper function `ggplot_rsi` to apply all above functions on a data set: + * `septic_patients %>% select(tobr, gent) %>% ggplot_rsi` will show portions of S, I and R immediately in a pretty plot * Determining bacterial ID: * New functions `as.bactid` and `is.bactid` to transform/ look up microbial ID's. * The existing function `guess_bactid` is now an alias of `as.bactid` diff --git a/R/RcppExports.R b/R/RcppExports.R deleted file mode 100644 index 89b4cc73..00000000 --- a/R/RcppExports.R +++ /dev/null @@ -1,15 +0,0 @@ -# Generated by using Rcpp::compileAttributes() -> do not edit by hand -# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393 - -rsi_calc_S <- function(x, include_I) { - .Call(`_AMR_rsi_calc_S`, x, include_I) -} - -rsi_calc_I <- function(x) { - .Call(`_AMR_rsi_calc_I`, x) -} - -rsi_calc_R <- function(x, include_I) { - .Call(`_AMR_rsi_calc_R`, x, include_I) -} - diff --git a/R/ggplot_rsi.R b/R/ggplot_rsi.R new file mode 100644 index 00000000..d18d4bfd --- /dev/null +++ b/R/ggplot_rsi.R @@ -0,0 +1,130 @@ +# ==================================================================== # +# TITLE # +# Antimicrobial Resistance (AMR) Analysis # +# # +# AUTHORS # +# Berends MS (m.s.berends@umcg.nl), Luz CF (c.f.luz@umcg.nl) # +# # +# LICENCE # +# This program is free software; you can redistribute it and/or modify # +# it under the terms of the GNU General Public License version 2.0, # +# as published by the Free Software Foundation. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# ==================================================================== # + +#' AMR bar plots with \code{ggplot} +#' +#' Use these functions to create bar plots for antimicrobial resistance analysis. All functions rely on internal \code{\link{ggplot}} functions. +#' @param data a \code{data.frame} with column(s) of class \code{"rsi"} (see \code{\link{as.rsi}}) +#' @param position position adjustment of bars, either \code{"stack"} (default) or \code{"dodge"} +#' @param x parameter to show on x axis, either \code{"Antibiotic"} (default) or \code{"Interpretation"} +#' @param facet parameter to split plots by, either \code{"Interpretation"} (default) or \code{"Antibiotic"} +#' @details At default, the names of antibiotics will be shown on the plots using \code{\link{abname}}. This can be set with the option \code{get_antibiotic_names} (a logical value), so change it e.g. to \code{FALSE} with \code{options(get_antibiotic_names = FALSE)}. +#' +#' \strong{The functions}\cr +#' \code{geom_rsi} will take any variable from the data that has an \code{rsi} class (created with \code{\link{as.rsi}}) using \code{\link{portion_df}} and will plot bars with the percentage R, I and S. The default behaviour is to have the bars stacked and to have the different antibiotics on the x axis. +#' +#' \code{facet_rsi} creates 2d plots (at default based on S/I/R) using \code{\link{facet_wrap}}. +#' +#' \code{scale_y_percent} transforms the y axis to a 0 to 100% range. +#' +#' \code{scale_rsi_colours} sets colours to the bars: green for S, yellow for I and red for R. +#' +#' \code{theme_rsi} is a \code{\link{theme}} with minimal distraction. +#' +#' \code{ggplot_rsi} is a wrapper around all above functions that uses data as first input. This makes it possible to use this function after a pipe (\code{\%>\%}). See Examples. +#' @rdname ggplot_rsi +#' @export +#' @examples +#' library(dplyr) +#' library(ggplot2) +#' +#' # get antimicrobial results for drugs against a UTI: +#' ggplot(septic_patients %>% select(amox, nitr, fosf, trim, cipr)) + +#' geom_rsi() +#' +#' # prettify it using some additional functions +#' df <- septic_patients[, c("amox", "nitr", "fosf", "trim", "cipr")] +#' ggplot(df) + +#' geom_rsi(x = "Interpretation") + +#' facet_rsi(facet = "Antibiotic") + +#' scale_y_percent() + +#' scale_rsi_colours() + +#' theme_rsi() +#' +#' # or better yet, simplify this using the wrapper function - a single command: +#' septic_patients %>% +#' select(amox, nitr, fosf, trim, cipr) %>% +#' ggplot_rsi() +#' +#' septic_patients %>% +#' select(amox, nitr, fosf, trim, cipr) %>% +#' ggplot_rsi(x = "Interpretation", facet = "Antibiotic") +ggplot_rsi <- function(data, + x = "Antibiotic", + facet = NULL) { + p <- ggplot2::ggplot(data = data) + + geom_rsi(x = x) + + scale_y_percent() + + scale_rsi_colours() + + theme_rsi() + + if (!is.null(facet)) { + p <- p + facet_rsi(facet = facet) + } + + p +} + +#' @rdname ggplot_rsi +#' @export +geom_rsi <- function(position = "stack", x = c("Antibiotic", "Interpretation")) { + + x <- x[1] + if (!x %in% c("Antibiotic", "Interpretation")) { + stop("`x` must be 'Antibiotic' or 'Interpretation'") + } + + ggplot2::layer(geom = "bar", stat = "identity", position = position, + mapping = ggplot2::aes_string(x = x, y = "Percentage", fill = "Interpretation"), + data = AMR::portion_df, params = list()) + +} + +#' @rdname ggplot_rsi +#' @export +facet_rsi <- function(facet = c("Interpretation", "Antibiotic")) { + facet <- facet[1] + if (!facet %in% c("Antibiotic", "Interpretation")) { + stop("`facet` must be 'Antibiotic' or 'Interpretation'") + } + ggplot2::facet_wrap(facets = facet, scales = "free") +} + +#' @rdname ggplot_rsi +#' @export +scale_y_percent <- function() { + ggplot2::scale_y_continuous(name = "Percentage", + breaks = seq(0, 1, 0.1), + limits = c(0, 1), + labels = percent(seq(0, 1, 0.1))) +} + +#' @rdname ggplot_rsi +#' @export +scale_rsi_colours <- function() { + ggplot2::scale_fill_brewer(palette = "RdYlGn") +} + +#' @rdname ggplot_rsi +#' @export +theme_rsi <- function() { + theme_minimal() + + theme(panel.grid.major.x = element_blank(), + panel.grid.minor = element_blank(), + panel.grid.major.y = element_line(colour = "grey75")) +} diff --git a/R/globals.R b/R/globals.R index f5f3a530..32afe883 100755 --- a/R/globals.R +++ b/R/globals.R @@ -17,6 +17,14 @@ # ==================================================================== # globalVariables(c('abname', + 'Antibiotic', + 'Interpretation', + 'Percentage', + 'bind_rows', + 'element_blank', + 'element_line', + 'theme', + 'theme_minimal', 'antibiotic', 'antibiotics', 'atc', diff --git a/R/portion.R b/R/portion.R index 646f01a0..a7e874f4 100755 --- a/R/portion.R +++ b/R/portion.R @@ -25,8 +25,12 @@ #' @param ab2 like \code{ab}, a vector of antibiotic interpretations. Use this to calculate (the lack of) co-resistance: the probability where one of two drugs have a resistant or susceptible result. See Examples. #' @param minimum minimal amount of available isolates. Any number lower than \code{minimum} will return \code{NA}. The default number of \code{30} isolates is advised by the CLSI as best practice, see Source. #' @param as_percent logical to indicate whether the output must be returned as percent (text), will else be a double +#' @param data a code{data.frame} containing columns with class \code{rsi} (see \code{\link{as.rsi}}) +#' @param translate a logical value to indicate whether antibiotic abbreviations should be translated with \code{\link{abname}} #' @details \strong{Remember that you should filter your table to let it contain only first isolates!} Use \code{\link{first_isolate}} to determine them in your data set. #' +#' \code{portion_df} takes any variable from \code{data} that has an \code{"rsi"} class (created with \code{\link{as.rsi}}) and calculates the portions R, I and S. The resulting \code{data.frame} will have three rows (for R/I/S) and a column for each variable with class \code{"rsi"}. +#' #' The old \code{\link{rsi}} function is still available for backwards compatibility but is deprecated. #' \if{html}{ #' \cr\cr @@ -225,11 +229,11 @@ rsi_calc <- function(type, } if (type == "S") { - found <- .Call(`_AMR_rsi_calc_S`, x, include_I) + found <- sum(as.integer(x) <= 1 + include_I, na.rm = TRUE) } else if (type == "I") { - found <- .Call(`_AMR_rsi_calc_I`, x) + found <- sum(as.integer(x) == 2, na.rm = TRUE) } else if (type == "R") { - found <- .Call(`_AMR_rsi_calc_R`, x, include_I) + found <- sum(as.integer(x) >= 3 - include_I, na.rm = TRUE) } else { stop("invalid type") } @@ -240,3 +244,29 @@ rsi_calc <- function(type, found / total } } + +#' @rdname portion +#' @importFrom dplyr bind_cols summarise_if mutate +#' @export +portion_df <- function(data, translate = getOption("get_antibiotic_names", TRUE)) { + resS <- bind_cols(data.frame(Interpretation = "S", stringsAsFactors = FALSE), + summarise_if(.tbl = data, + .predicate = is.rsi, + .funs = portion_S)) + resI <- bind_cols(data.frame(Interpretation = "I", stringsAsFactors = FALSE), + summarise_if(.tbl = data, + .predicate = is.rsi, + .funs = portion_I)) + resR <- bind_cols(data.frame(Interpretation = "R", stringsAsFactors = FALSE), + summarise_if(.tbl = data, + .predicate = is.rsi, + .funs = portion_R)) + + res <- bind_rows(resS, resI, resR) %>% + mutate(Interpretation = factor(Interpretation, levels = c("R", "I", "S"), ordered = TRUE)) %>% + tidyr::gather(Antibiotic, Percentage, -Interpretation) + if (translate == TRUE) { + res <- res %>% mutate(Antibiotic = abname(Antibiotic, from = "guess", to = "official")) + } + res +} diff --git a/R/zzz.R b/R/zzz.R index a04fedce..1460bf58 100755 --- a/R/zzz.R +++ b/R/zzz.R @@ -1,7 +1,3 @@ .onLoad <- function(libname, pkgname) { backports::import(pkgname) } - -#' @importFrom Rcpp evalCpp -#' @useDynLib AMR, .registration = TRUE -NULL diff --git a/README.md b/README.md index 054ca2d6..a8a55fa6 100755 --- a/README.md +++ b/README.md @@ -50,8 +50,6 @@ And it contains: With the `MDRO` function (abbreviation of Multi Drug Resistant Organisms), you can check your isolates for exceptional resistance with country-specific guidelines or EUCAST rules. Currently guidelines for Germany and the Netherlands are supported. Please suggest addition of your own country here: [https://github.com/msberends/AMR/issues/new](https://github.com/msberends/AMR/issues/new?title=New%20guideline%20for%20MDRO&body=%3C--%20Please%20add%20your%20country%20code,%20guideline%20name,%20version%20and%20source%20below%20and%20remove%20this%20line--%3E). -The functions to calculate microbial resistance use expressions that are not evaluated by R itself, but by alternative C++ code that is 25 to 30 times faster than R and uses less memory. This is called *hybrid evaluation*. - #### Read all changes and new functions in [NEWS.md](NEWS.md). ## How to get it? @@ -145,27 +143,21 @@ rsi_data <- as.rsi(c(rep("S", 474), rep("I", 36), rep("R", 370))) ``` These functions also try to coerce valid values. -Quick overviews when just printing objects: +Quick overviews with `summary`: ```r -mic_data -# Class 'mic': 7 isolates -# -# 0 -# -# <=0.128 1 8 16 >=32 -# 1 1 2 2 1 +summary(rsi_data) +# Mode :rsi +# :0 +# Sum S :474 +# Sum IR:406 +# -Sum R:370 +# -Sum I:36 -rsi_data -# Class 'rsi': 880 isolates -# -# : 0 -# Sum of S: 474 -# Sum of IR: 406 -# - Sum of R: 370 -# - Sum of I: 36 -# -# %S %IR %I %R -# 53.9 46.1 4.1 42.0 +summary(mic_data) +# Mode:mic +# :0 +# Min.:<=0.128 +# Max.:>=32 ``` A plot of `rsi_data`: diff --git a/man/abname.Rd b/man/abname.Rd index 9338498b..44e0a721 100755 --- a/man/abname.Rd +++ b/man/abname.Rd @@ -7,8 +7,8 @@ \code{\link{antibiotics}} } \usage{ -abname(abcode, from = c("guess", "atc", "molis", "umcg"), to = "official", - textbetween = " + ", tolower = FALSE) +abname(abcode, from = c("guess", "atc", "molis", "umcg"), + to = "official", textbetween = " + ", tolower = FALSE) } \arguments{ \item{abcode}{a code or name, like \code{"AMOX"}, \code{"AMCL"} or \code{"J01CA04"}} diff --git a/man/first_isolate.Rd b/man/first_isolate.Rd index c5b87cbe..629bd5e8 100755 --- a/man/first_isolate.Rd +++ b/man/first_isolate.Rd @@ -9,10 +9,11 @@ Methodology of this function is based on: \strong{M39 Analysis and Presentation \usage{ first_isolate(tbl, col_date, col_patient_id, col_bactid = NA, col_testcode = NA, col_specimen = NA, col_icu = NA, - col_keyantibiotics = NA, episode_days = 365, testcodes_exclude = "", - icu_exclude = FALSE, filter_specimen = NA, output_logical = TRUE, - type = "keyantibiotics", ignore_I = TRUE, points_threshold = 2, - info = TRUE, col_genus = NA, col_species = NA) + col_keyantibiotics = NA, episode_days = 365, + testcodes_exclude = "", icu_exclude = FALSE, filter_specimen = NA, + output_logical = TRUE, type = "keyantibiotics", ignore_I = TRUE, + points_threshold = 2, info = TRUE, col_genus = NA, + col_species = NA) } \arguments{ \item{tbl}{a \code{data.frame} containing isolates.} diff --git a/man/freq.Rd b/man/freq.Rd index 7bbc2099..7a63b85d 100755 --- a/man/freq.Rd +++ b/man/freq.Rd @@ -7,9 +7,9 @@ \alias{print.frequency_tbl} \title{Frequency table} \usage{ -frequency_tbl(x, ..., sort.count = TRUE, nmax = getOption("max.print.freq"), - na.rm = TRUE, row.names = TRUE, markdown = FALSE, digits = 2, - sep = " ") +frequency_tbl(x, ..., sort.count = TRUE, + nmax = getOption("max.print.freq"), na.rm = TRUE, row.names = TRUE, + markdown = FALSE, digits = 2, sep = " ") freq(x, ..., sort.count = TRUE, nmax = getOption("max.print.freq"), na.rm = TRUE, row.names = TRUE, markdown = FALSE, digits = 2, @@ -17,8 +17,8 @@ freq(x, ..., sort.count = TRUE, nmax = getOption("max.print.freq"), top_freq(f, n) -\method{print}{frequency_tbl}(x, nmax = getOption("max.print.freq", default = - 15), ...) +\method{print}{frequency_tbl}(x, nmax = getOption("max.print.freq", + default = 15), ...) } \arguments{ \item{x}{vector of any class or a \code{\link{data.frame}}, \code{\link{tibble}} or \code{\link{table}}} diff --git a/man/g.test.Rd b/man/g.test.Rd index 1a425e87..b2db1035 100644 --- a/man/g.test.Rd +++ b/man/g.test.Rd @@ -12,7 +12,8 @@ This code is almost identical to \code{\link{chisq.test}}, except that: } } \usage{ -g.test(x, y = NULL, p = rep(1/length(x), length(x)), rescale.p = FALSE) +g.test(x, y = NULL, p = rep(1/length(x), length(x)), + rescale.p = FALSE) } \arguments{ \item{x}{a numeric vector or matrix. \code{x} and \code{y} can also diff --git a/man/ggplot_rsi.Rd b/man/ggplot_rsi.Rd new file mode 100644 index 00000000..e2e7cffb --- /dev/null +++ b/man/ggplot_rsi.Rd @@ -0,0 +1,77 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/ggplot_rsi.R +\name{ggplot_rsi} +\alias{ggplot_rsi} +\alias{geom_rsi} +\alias{facet_rsi} +\alias{scale_y_percent} +\alias{scale_rsi_colours} +\alias{theme_rsi} +\title{AMR bar plots with \code{ggplot}} +\usage{ +ggplot_rsi(data, x = "Antibiotic", facet = NULL) + +geom_rsi(position = "stack", x = c("Antibiotic", "Interpretation")) + +facet_rsi(facet = c("Interpretation", "Antibiotic")) + +scale_y_percent() + +scale_rsi_colours() + +theme_rsi() +} +\arguments{ +\item{data}{a \code{data.frame} with column(s) of class \code{"rsi"} (see \code{\link{as.rsi}})} + +\item{x}{parameter to show on x axis, either \code{"Antibiotic"} (default) or \code{"Interpretation"}} + +\item{facet}{parameter to split plots by, either \code{"Interpretation"} (default) or \code{"Antibiotic"}} + +\item{position}{position adjustment of bars, either \code{"stack"} (default) or \code{"dodge"}} +} +\description{ +Use these functions to create bar plots for antimicrobial resistance analysis. All functions rely on internal \code{\link{ggplot}} functions. +} +\details{ +At default, the names of antibiotics will be shown on the plots using \code{\link{abname}}. This can be set with the option \code{get_antibiotic_names} (a logical value), so change it e.g. to \code{FALSE} with \code{options(get_antibiotic_names = FALSE)}. + +\strong{The functions}\cr +\code{geom_rsi} will take any variable from the data that has an \code{rsi} class (created with \code{\link{as.rsi}}) using \code{\link{portion_df}} and will plot bars with the percentage R, I and S. The default behaviour is to have the bars stacked and to have the different antibiotics on the x axis. + +\code{facet_rsi} creates 2d plots (at default based on S/I/R) using \code{\link{facet_wrap}}. + +\code{scale_y_percent} transforms the y axis to a 0 to 100% range. + +\code{scale_rsi_colours} sets colours to the bars: green for S, yellow for I and red for R. + +\code{theme_rsi} is a \code{\link{theme}} with minimal distraction. + +\code{ggplot_rsi} is a wrapper around all above functions that uses data as first input. This makes it possible to use this function after a pipe (\code{\%>\%}). See Examples. +} +\examples{ +library(dplyr) +library(ggplot2) + +# get antimicrobial results for drugs against a UTI: +ggplot(septic_patients \%>\% select(amox, nitr, fosf, trim, cipr)) + + geom_rsi() + +# prettify it using some additional functions +df <- septic_patients[, c("amox", "nitr", "fosf", "trim", "cipr")] +ggplot(df) + + geom_rsi(x = "Interpretation") + + facet_rsi(facet = "Antibiotic") + + scale_y_percent() + + scale_rsi_colours() + + theme_rsi() + +# or better yet, simplify this using the wrapper function - a single command: +septic_patients \%>\% + select(amox, nitr, fosf, trim, cipr) \%>\% + ggplot_rsi() + +septic_patients \%>\% + select(amox, nitr, fosf, trim, cipr) \%>\% + ggplot_rsi(x = "Interpretation", facet = "Antibiotic") +} diff --git a/man/portion.Rd b/man/portion.Rd index a54a544a..edd4b58f 100644 --- a/man/portion.Rd +++ b/man/portion.Rd @@ -7,6 +7,7 @@ \alias{portion_I} \alias{portion_SI} \alias{portion_S} +\alias{portion_df} \title{Calculate resistance of isolates} \source{ \strong{M39 Analysis and Presentation of Cumulative Antimicrobial Susceptibility Test Data, 4th Edition}, 2014, \emph{Clinical and Laboratory Standards Institute (CLSI)}. \url{https://clsi.org/standards/products/microbiology/documents/m39/}. @@ -21,6 +22,8 @@ portion_I(ab1, minimum = 30, as_percent = FALSE) portion_SI(ab1, ab2 = NULL, minimum = 30, as_percent = FALSE) portion_S(ab1, ab2 = NULL, minimum = 30, as_percent = FALSE) + +portion_df(data, translate = getOption("get_antibiotic_names", TRUE)) } \arguments{ \item{ab1}{vector of antibiotic interpretations, they will be transformed internally with \code{\link{as.rsi}} if needed} @@ -30,6 +33,10 @@ portion_S(ab1, ab2 = NULL, minimum = 30, as_percent = FALSE) \item{minimum}{minimal amount of available isolates. Any number lower than \code{minimum} will return \code{NA}. The default number of \code{30} isolates is advised by the CLSI as best practice, see Source.} \item{as_percent}{logical to indicate whether the output must be returned as percent (text), will else be a double} + +\item{data}{a code{data.frame} containing columns with class \code{rsi} (see \code{\link{as.rsi}})} + +\item{translate}{a logical value to indicate whether antibiotic abbreviations should be translated with \code{\link{abname}}} } \value{ Double or, when \code{as_percent = TRUE}, a character. @@ -42,6 +49,8 @@ These functions can be used to calculate the (co-)resistance of microbial isolat \details{ \strong{Remember that you should filter your table to let it contain only first isolates!} Use \code{\link{first_isolate}} to determine them in your data set. +\code{portion_df} takes any variable from \code{data} that has an \code{"rsi"} class (created with \code{\link{as.rsi}}) and calculates the portions R, I and S. The resulting \code{data.frame} will have three rows (for R/I/S) and a column for each variable with class \code{"rsi"}. + The old \code{\link{rsi}} function is still available for backwards compatibility but is deprecated. \if{html}{ \cr\cr diff --git a/man/resistance_predict.Rd b/man/resistance_predict.Rd index 9ad817eb..254aa29f 100644 --- a/man/resistance_predict.Rd +++ b/man/resistance_predict.Rd @@ -5,9 +5,10 @@ \alias{rsi_predict} \title{Predict antimicrobial resistance} \usage{ -resistance_predict(tbl, col_ab, col_date, year_min = NULL, year_max = NULL, - year_every = 1, minimum = 30, model = "binomial", I_as_R = TRUE, - preserve_measurements = TRUE, info = TRUE) +resistance_predict(tbl, col_ab, col_date, year_min = NULL, + year_max = NULL, year_every = 1, minimum = 30, + model = "binomial", I_as_R = TRUE, preserve_measurements = TRUE, + info = TRUE) rsi_predict(tbl, col_ab, col_date, year_min = NULL, year_max = NULL, year_every = 1, minimum = 30, model = "binomial", I_as_R = TRUE, diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp deleted file mode 100644 index bbcef50e..00000000 --- a/src/RcppExports.cpp +++ /dev/null @@ -1,54 +0,0 @@ -// Generated by using Rcpp::compileAttributes() -> do not edit by hand -// Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393 - -#include - -using namespace Rcpp; - -// rsi_calc_S -int rsi_calc_S(DoubleVector x, bool include_I); -RcppExport SEXP _AMR_rsi_calc_S(SEXP xSEXP, SEXP include_ISEXP) { -BEGIN_RCPP - Rcpp::RObject rcpp_result_gen; - Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< DoubleVector >::type x(xSEXP); - Rcpp::traits::input_parameter< bool >::type include_I(include_ISEXP); - rcpp_result_gen = Rcpp::wrap(rsi_calc_S(x, include_I)); - return rcpp_result_gen; -END_RCPP -} -// rsi_calc_I -int rsi_calc_I(DoubleVector x); -RcppExport SEXP _AMR_rsi_calc_I(SEXP xSEXP) { -BEGIN_RCPP - Rcpp::RObject rcpp_result_gen; - Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< DoubleVector >::type x(xSEXP); - rcpp_result_gen = Rcpp::wrap(rsi_calc_I(x)); - return rcpp_result_gen; -END_RCPP -} -// rsi_calc_R -int rsi_calc_R(DoubleVector x, bool include_I); -RcppExport SEXP _AMR_rsi_calc_R(SEXP xSEXP, SEXP include_ISEXP) { -BEGIN_RCPP - Rcpp::RObject rcpp_result_gen; - Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< DoubleVector >::type x(xSEXP); - Rcpp::traits::input_parameter< bool >::type include_I(include_ISEXP); - rcpp_result_gen = Rcpp::wrap(rsi_calc_R(x, include_I)); - return rcpp_result_gen; -END_RCPP -} - -static const R_CallMethodDef CallEntries[] = { - {"_AMR_rsi_calc_S", (DL_FUNC) &_AMR_rsi_calc_S, 2}, - {"_AMR_rsi_calc_I", (DL_FUNC) &_AMR_rsi_calc_I, 1}, - {"_AMR_rsi_calc_R", (DL_FUNC) &_AMR_rsi_calc_R, 2}, - {NULL, NULL, 0} -}; - -RcppExport void R_init_AMR(DllInfo *dll) { - R_registerRoutines(dll, NULL, CallEntries, NULL, NULL); - R_useDynamicSymbols(dll, FALSE); -} diff --git a/src/rsi_calc.cpp b/src/rsi_calc.cpp deleted file mode 100644 index 94d06839..00000000 --- a/src/rsi_calc.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include - -using namespace Rcpp; - -// [[Rcpp::export]] -int rsi_calc_S(DoubleVector x, bool include_I) { - return count_if(x.begin(), - x.end(), - bind2nd(std::less_equal(), - 1 + include_I)); -} - -// [[Rcpp::export]] -int rsi_calc_I(DoubleVector x) { - return count_if(x.begin(), - x.end(), - bind2nd(std::equal_to(), - 2)); -} - -// [[Rcpp::export]] -int rsi_calc_R(DoubleVector x, bool include_I) { - return count_if(x.begin(), - x.end(), - bind2nd(std::greater_equal(), - 3 - include_I)); -}