From 8b5e2b030b386dc232e1c9ad80758b11b5cc942c Mon Sep 17 00:00:00 2001 From: "Matthijs S. Berends" Date: Wed, 29 May 2019 19:56:17 +0200 Subject: [PATCH] added boxplot for (grouped) freq() --- DESCRIPTION | 4 +- NAMESPACE | 37 ++--- NEWS.md | 11 ++ R/eucast_rules.R | 20 ++- R/freq.R | 212 ++++++++++++++++++--------- docs/LICENSE-text.html | 2 +- docs/articles/freq.html | 4 +- docs/articles/index.html | 2 +- docs/authors.html | 2 +- docs/index.html | 2 +- docs/news/index.html | 239 ++++++++++++++++--------------- docs/reference/age_groups.html | 57 ++++---- docs/reference/eucast_rules.html | 24 ++-- docs/reference/freq.html | 36 +++-- docs/reference/index.html | 4 +- docs/reference/mdro.html | 24 ++-- man/eucast_rules.Rd | 20 ++- man/freq.Rd | 32 +++-- man/mdro.Rd | 20 ++- tests/testthat/test-freq.R | 9 +- 20 files changed, 464 insertions(+), 297 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 0e3d47cd..c0d32763 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: AMR -Version: 0.6.1.9040 -Date: 2019-05-28 +Version: 0.6.1.9042 +Date: 2019-05-29 Title: Antimicrobial Resistance Analysis Authors@R: c( person( diff --git a/NAMESPACE b/NAMESPACE index a84b5929..4387f6f2 100755 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,7 +2,7 @@ S3method(as.data.frame,ab) S3method(as.data.frame,atc) -S3method(as.data.frame,frequency_tbl) +S3method(as.data.frame,freq) S3method(as.data.frame,mo) S3method(as.double,mic) S3method(as.integer,mic) @@ -11,19 +11,20 @@ S3method(as.rsi,data.frame) S3method(as.rsi,default) S3method(as.rsi,disk) S3method(as.rsi,mic) -S3method(as.vector,frequency_tbl) -S3method(as_tibble,frequency_tbl) +S3method(as.vector,freq) +S3method(as_tibble,freq) S3method(barplot,mic) S3method(barplot,rsi) -S3method(diff,frequency_tbl) +S3method(boxplot,freq) +S3method(diff,freq) S3method(droplevels,mic) S3method(droplevels,rsi) -S3method(format,frequency_tbl) -S3method(hist,frequency_tbl) +S3method(format,freq) +S3method(hist,freq) S3method(kurtosis,data.frame) S3method(kurtosis,default) S3method(kurtosis,matrix) -S3method(plot,frequency_tbl) +S3method(plot,freq) S3method(plot,mic) S3method(plot,resistance_predict) S3method(plot,rsi) @@ -31,6 +32,7 @@ S3method(print,ab) S3method(print,atc) S3method(print,catalogue_of_life_version) S3method(print,disk) +S3method(print,freq) S3method(print,frequency_tbl) S3method(print,mic) S3method(print,mo) @@ -40,7 +42,7 @@ S3method(print,rsi) S3method(pull,ab) S3method(pull,atc) S3method(pull,mo) -S3method(select,frequency_tbl) +S3method(select,freq) S3method(skewness,data.frame) S3method(skewness,default) S3method(skewness,matrix) @@ -180,31 +182,33 @@ export(theme_rsi) export(top_freq) exportMethods(as.data.frame.ab) exportMethods(as.data.frame.atc) -exportMethods(as.data.frame.frequency_tbl) +exportMethods(as.data.frame.freq) exportMethods(as.data.frame.mo) exportMethods(as.double.mic) exportMethods(as.integer.mic) exportMethods(as.numeric.mic) -exportMethods(as.vector.frequency_tbl) -exportMethods(as_tibble.frequency_tbl) +exportMethods(as.vector.freq) +exportMethods(as_tibble.freq) exportMethods(barplot.mic) exportMethods(barplot.rsi) -exportMethods(diff.frequency_tbl) +exportMethods(boxplot.freq) +exportMethods(diff.freq) exportMethods(droplevels.mic) exportMethods(droplevels.rsi) -exportMethods(format.frequency_tbl) -exportMethods(hist.frequency_tbl) +exportMethods(format.freq) +exportMethods(hist.freq) exportMethods(kurtosis) exportMethods(kurtosis.data.frame) exportMethods(kurtosis.default) exportMethods(kurtosis.matrix) -exportMethods(plot.frequency_tbl) +exportMethods(plot.freq) exportMethods(plot.mic) exportMethods(plot.rsi) exportMethods(print.ab) exportMethods(print.atc) exportMethods(print.catalogue_of_life_version) exportMethods(print.disk) +exportMethods(print.freq) exportMethods(print.frequency_tbl) exportMethods(print.mic) exportMethods(print.mo) @@ -214,7 +218,7 @@ exportMethods(print.rsi) exportMethods(pull.ab) exportMethods(pull.atc) exportMethods(pull.mo) -exportMethods(select.frequency_tbl) +exportMethods(select.freq) exportMethods(skewness) exportMethods(skewness.data.frame) exportMethods(skewness.default) @@ -288,6 +292,7 @@ importFrom(grDevices,boxplot.stats) importFrom(graphics,arrows) importFrom(graphics,axis) importFrom(graphics,barplot) +importFrom(graphics,boxplot) importFrom(graphics,hist) importFrom(graphics,plot) importFrom(graphics,points) diff --git a/NEWS.md b/NEWS.md index 855acb63..f29b2984 100755 --- a/NEWS.md +++ b/NEWS.md @@ -28,6 +28,17 @@ * Frequency tables (`freq()`): * speed improvement for microbial IDs * fixed level names in markdown + * support for boxplots: + ```r + septic_patients %>% + freq(age) %>% + boxplot() + # grouped boxplots: + septic_patients %>% + group_by(hospital_id) %>% + freq(age) %>% + boxplot() + ``` * Removed all hardcoded EUCAST rules and replaced them with a new reference file: `./inst/eucast/eucast.tsv` * Added ceftazidim intrinsic resistance to *Streptococci* * Changed default settings for `age_groups()`, to let groups of fives and tens end with 100+ instead of 120+ diff --git a/R/eucast_rules.R b/R/eucast_rules.R index 64ac2ebf..1bb224f9 100755 --- a/R/eucast_rules.R +++ b/R/eucast_rules.R @@ -45,16 +45,18 @@ EUCAST_VERSION_EXPERT_RULES <- "3.1, 2016" #' @section Antibiotics: #' To define antibiotics column names, leave as it is to determine it automatically with \code{\link{guess_ab_col}} or input a text (case-insensitive), or use \code{NULL} to skip a column (e.g. \code{TIC = NULL} to skip ticarcillin). Manually defined but non-existing columns will be skipped with a warning. #' -#' Available abbrevations of the column containing antibiotics in the form '\strong{antimicrobial ID}: name (\emph{ATC code})': +#' The following antibiotics are used for the functions \code{\link{eucast_rules}} and \code{\link{mdro}}. These are shown in the format '\strong{antimicrobial ID}: name (\emph{ATC code})', sorted by name: #' -#' \strong{AMC}: amoxicillin/clavulanic acid (\href{https://www.whocc.no/atc_ddd_index/?code=J01CR02}{J01CR02}), #' \strong{AMK}: amikacin (\href{https://www.whocc.no/atc_ddd_index/?code=J01GB06}{J01GB06}), #' \strong{AMX}: amoxicillin (\href{https://www.whocc.no/atc_ddd_index/?code=J01CA04}{J01CA04}), +#' \strong{AMC}: amoxicillin/clavulanic acid (\href{https://www.whocc.no/atc_ddd_index/?code=J01CR02}{J01CR02}), #' \strong{AMP}: ampicillin (\href{https://www.whocc.no/atc_ddd_index/?code=J01CA01}{J01CA01}), #' \strong{AZM}: azithromycin (\href{https://www.whocc.no/atc_ddd_index/?code=J01FA10}{J01FA10}), #' \strong{AZL}: azlocillin (\href{https://www.whocc.no/atc_ddd_index/?code=J01CA09}{J01CA09}), #' \strong{ATM}: aztreonam (\href{https://www.whocc.no/atc_ddd_index/?code=J01DF01}{J01DF01}), +#' \strong{CAP}: capreomycin (\href{https://www.whocc.no/atc_ddd_index/?code=J04AB30}{J04AB30}), #' \strong{RID}: cefaloridine (\href{https://www.whocc.no/atc_ddd_index/?code=J01DB02}{J01DB02}), +#' \strong{CZO}: cefazolin (\href{https://www.whocc.no/atc_ddd_index/?code=J01DB04}{J01DB04}), #' \strong{FEP}: cefepime (\href{https://www.whocc.no/atc_ddd_index/?code=J01DE01}{J01DE01}), #' \strong{CTX}: cefotaxime (\href{https://www.whocc.no/atc_ddd_index/?code=J01DD01}{J01DD01}), #' \strong{FOX}: cefoxitin (\href{https://www.whocc.no/atc_ddd_index/?code=J01DC01}{J01DC01}), @@ -66,26 +68,28 @@ EUCAST_VERSION_EXPERT_RULES <- "3.1, 2016" #' \strong{CIP}: ciprofloxacin (\href{https://www.whocc.no/atc_ddd_index/?code=J01MA02}{J01MA02}), #' \strong{CLR}: clarithromycin (\href{https://www.whocc.no/atc_ddd_index/?code=J01FA09}{J01FA09}), #' \strong{CLI}: clindamycin (\href{https://www.whocc.no/atc_ddd_index/?code=J01FF01}{J01FF01}), -#' \strong{FLC}: flucloxacillin (\href{https://www.whocc.no/atc_ddd_index/?code=J01CF05}{J01CF05}), #' \strong{COL}: colistin (\href{https://www.whocc.no/atc_ddd_index/?code=J01XB01}{J01XB01}), -#' \strong{CZO}: cefazolin (\href{https://www.whocc.no/atc_ddd_index/?code=J01DB04}{J01DB04}), #' \strong{DAP}: daptomycin (\href{https://www.whocc.no/atc_ddd_index/?code=J01XX09}{J01XX09}), #' \strong{DOX}: doxycycline (\href{https://www.whocc.no/atc_ddd_index/?code=J01AA02}{J01AA02}), #' \strong{ETP}: ertapenem (\href{https://www.whocc.no/atc_ddd_index/?code=J01DH03}{J01DH03}), #' \strong{ERY}: erythromycin (\href{https://www.whocc.no/atc_ddd_index/?code=J01FA01}{J01FA01}), +#' \strong{ETH}: ethambutol (\href{https://www.whocc.no/atc_ddd_index/?code=J04AK02}{J04AK02}), +#' \strong{FLC}: flucloxacillin (\href{https://www.whocc.no/atc_ddd_index/?code=J01CF05}{J01CF05}), #' \strong{FOS}: fosfomycin (\href{https://www.whocc.no/atc_ddd_index/?code=J01XX01}{J01XX01}), #' \strong{FUS}: fusidic acid (\href{https://www.whocc.no/atc_ddd_index/?code=J01XC01}{J01XC01}), +#' \strong{GAT}: gatifloxacin (\href{https://www.whocc.no/atc_ddd_index/?code=J01MA16}{J01MA16}), #' \strong{GEN}: gentamicin (\href{https://www.whocc.no/atc_ddd_index/?code=J01GB03}{J01GB03}), #' \strong{IPM}: imipenem (\href{https://www.whocc.no/atc_ddd_index/?code=J01DH51}{J01DH51}), +#' \strong{INH}: isoniazid (\href{https://www.whocc.no/atc_ddd_index/?code=J04AC01}{J04AC01}), #' \strong{KAN}: kanamycin (\href{https://www.whocc.no/atc_ddd_index/?code=J01GB04}{J01GB04}), #' \strong{LVX}: levofloxacin (\href{https://www.whocc.no/atc_ddd_index/?code=J01MA12}{J01MA12}), #' \strong{LIN}: lincomycin (\href{https://www.whocc.no/atc_ddd_index/?code=J01FF02}{J01FF02}), #' \strong{LNZ}: linezolid (\href{https://www.whocc.no/atc_ddd_index/?code=J01XX08}{J01XX08}), #' \strong{MEM}: meropenem (\href{https://www.whocc.no/atc_ddd_index/?code=J01DH02}{J01DH02}), +#' \strong{MTR}: metronidazole (\href{https://www.whocc.no/atc_ddd_index/?code=J01XD01}{J01XD01}), #' \strong{MEZ}: mezlocillin (\href{https://www.whocc.no/atc_ddd_index/?code=J01CA10}{J01CA10}), #' \strong{MNO}: minocycline (\href{https://www.whocc.no/atc_ddd_index/?code=J01AA08}{J01AA08}), #' \strong{MFX}: moxifloxacin (\href{https://www.whocc.no/atc_ddd_index/?code=J01MA14}{J01MA14}), -#' \strong{MTR}: metronidazole (\href{https://www.whocc.no/atc_ddd_index/?code=J01MA14}{J01XD01}), #' \strong{NAL}: nalidixic acid (\href{https://www.whocc.no/atc_ddd_index/?code=J01MB02}{J01MB02}), #' \strong{NEO}: neomycin (\href{https://www.whocc.no/atc_ddd_index/?code=J01GB05}{J01GB05}), #' \strong{NET}: netilmicin (\href{https://www.whocc.no/atc_ddd_index/?code=J01GB07}{J01GB07}), @@ -93,14 +97,18 @@ EUCAST_VERSION_EXPERT_RULES <- "3.1, 2016" #' \strong{NOR}: norfloxacin (\href{https://www.whocc.no/atc_ddd_index/?code=J01MA06}{J01MA06}), #' \strong{NOV}: novobiocin (an ATCvet code: \href{https://www.whocc.no/atc_ddd_index/?code=QJ01XX95}{QJ01XX95}), #' \strong{OFX}: ofloxacin (\href{https://www.whocc.no/atc_ddd_index/?code=J01MA01}{J01MA01}), -#' \strong{OXA}: oxacillin (\href{https://www.whocc.no/atc_ddd_index/?code=J01MA01}{J01CF04}), +#' \strong{OXA}: oxacillin (\href{https://www.whocc.no/atc_ddd_index/?code=J01CF04}{J01CF04}), #' \strong{PEN}: penicillin G (\href{https://www.whocc.no/atc_ddd_index/?code=J01CE01}{J01CE01}), #' \strong{PIP}: piperacillin (\href{https://www.whocc.no/atc_ddd_index/?code=J01CA12}{J01CA12}), #' \strong{TZP}: piperacillin/tazobactam (\href{https://www.whocc.no/atc_ddd_index/?code=J01CR05}{J01CR05}), #' \strong{PLB}: polymyxin B (\href{https://www.whocc.no/atc_ddd_index/?code=J01XB02}{J01XB02}), #' \strong{PRI}: pristinamycin (\href{https://www.whocc.no/atc_ddd_index/?code=J01FG01}{J01FG01}), +#' \strong{PZA}: pyrazinamide (\href{https://www.whocc.no/atc_ddd_index/?code=J04AK01}{J04AK01}), #' \strong{QDA}: quinupristin/dalfopristin (\href{https://www.whocc.no/atc_ddd_index/?code=J01FG02}{J01FG02}), +#' \strong{RIB}: rifabutin (\href{https://www.whocc.no/atc_ddd_index/?code=J04AB04}{J04AB04}), #' \strong{RIF}: rifampicin (\href{https://www.whocc.no/atc_ddd_index/?code=J04AB02}{J04AB02}), +#' \strong{RIF}: rifampin (\href{https://www.whocc.no/atc_ddd_index/?code=J04AB02}{J04AB02}), +#' \strong{RFP}: rifapentine (\href{https://www.whocc.no/atc_ddd_index/?code=J04AB05}{J04AB05}), #' \strong{RXT}: roxithromycin (\href{https://www.whocc.no/atc_ddd_index/?code=J01FA06}{J01FA06}), #' \strong{SIS}: sisomicin (\href{https://www.whocc.no/atc_ddd_index/?code=J01GB08}{J01GB08}), #' \strong{TEC}: teicoplanin (\href{https://www.whocc.no/atc_ddd_index/?code=J01XA02}{J01XA02}), diff --git a/R/freq.R b/R/freq.R index c5e5653e..a3e4aec5 100755 --- a/R/freq.R +++ b/R/freq.R @@ -75,7 +75,7 @@ #' @keywords summary summarise frequency freq #' @rdname freq #' @name freq -#' @return A \code{data.frame} (with an additional class \code{"frequency_tbl"}) with five columns: \code{item}, \code{count}, \code{percent}, \code{cum_count} and \code{cum_percent}. +#' @return A \code{data.frame} (with an additional class \code{"freq"}) with five columns: \code{item}, \code{count}, \code{percent}, \code{cum_count} and \code{cum_percent}. #' @export #' @inheritSection AMR Read more on our website! #' @examples @@ -139,6 +139,16 @@ #' freq(age) %>% #' hist() #' +#' # or a boxplot of numeric values +#' septic_patients %>% +#' freq(age) %>% +#' boxplot() +#' +#' # or even a boxplot per group +#' septic_patients %>% +#' group_by(hospital_id) %>% +#' freq(age) %>% +#' boxplot() #' #' # or print all points to a regular plot #' septic_patients %>% @@ -183,22 +193,22 @@ #' # check differences between frequency tables #' diff(freq(septic_patients$TMP), #' freq(septic_patients$SXT)) -frequency_tbl <- function(x, - ..., - sort.count = TRUE, - nmax = getOption("max.print.freq"), - na.rm = TRUE, - row.names = TRUE, - markdown = !interactive(), - digits = 2, - quote = FALSE, - header = TRUE, - title = NULL, - na = "", - droplevels = TRUE, - sep = " ", - decimal.mark = getOption("OutDec"), - big.mark = ifelse(decimal.mark != ",", ",", ".")) { +freq <- function(x, + ..., + sort.count = TRUE, + nmax = getOption("max.print.freq"), + na.rm = TRUE, + row.names = TRUE, + markdown = !interactive(), + digits = 2, + quote = FALSE, + header = TRUE, + title = NULL, + na = "", + droplevels = TRUE, + sep = " ", + decimal.mark = getOption("OutDec"), + big.mark = ifelse(decimal.mark != ",", ",", ".")) { mult.columns <- 0 x.group = character(0) @@ -544,7 +554,7 @@ frequency_tbl <- function(x, # } structure(.Data = df, - class = c("frequency_tbl", class(df)), + class = unique(c("freq", class(df))), header = header_list, opt = list(title = title, data = x.name, @@ -565,7 +575,11 @@ frequency_tbl <- function(x, #' @rdname freq #' @export -freq <- frequency_tbl +frequency_tbl <- freq + +is.freq <- function(f) { + any(c("freq", "frequency_tbl") %in% class(f)) +} #' @importFrom crayon silver green red #' @importFrom dplyr %>% @@ -639,13 +653,7 @@ format_header <- function(x, markdown = FALSE, decimal.mark = ".", big.mark = ", # class and mode if (is.null(header$columns)) { - # if (markdown == TRUE) { - # header$class <- paste0("`", header$class, "`") - # } if (!header$mode %in% header$class) { - # if (markdown == TRUE) { - # header$mode <- paste0("`", header$mode, "`") - # } header$class <- header$class %>% rev() %>% paste(collapse = " > ") %>% paste0(silver(paste0(" (", header$mode, ")"))) } else { header$class <- header$class %>% rev() %>% paste(collapse = " > ") @@ -654,9 +662,6 @@ format_header <- function(x, markdown = FALSE, decimal.mark = ".", big.mark = ", } # levels if (!is.null(header$levels)) { - # if (markdown == TRUE) { - # header$levels <- paste0("`", header$levels, "`") - # } if (header$ordered == TRUE) { levels_text <- paste0(header$levels, collapse = " < ") } else { @@ -733,7 +738,7 @@ format_header <- function(x, markdown = FALSE, decimal.mark = ".", big.mark = ", #' @export #' @importFrom dplyr top_n pull top_freq <- function(f, n) { - if (!"frequency_tbl" %in% class(f)) { + if (!is.freq(f)) { stop("`top_freq` can only be applied to frequency tables", call. = FALSE) } if (!is.numeric(n) | length(n) != 1L) { @@ -751,7 +756,7 @@ top_freq <- function(f, n) { #' @rdname freq #' @export header <- function(f, property = NULL) { - if (!"frequency_tbl" %in% class(f)) { + if (!is.freq(f)) { stop("`header` can only be applied to frequency tables", call. = FALSE) } if (is.null(property)) { @@ -765,13 +770,12 @@ header <- function(f, property = NULL) { } #' @noRd -#' @exportMethod diff.frequency_tbl +#' @exportMethod diff.freq #' @importFrom dplyr %>% full_join mutate #' @export -diff.frequency_tbl <- function(x, y, ...) { +diff.freq <- function(x, y, ...) { # check classes - if (!"frequency_tbl" %in% class(x) - | !"frequency_tbl" %in% class(y)) { + if (!is.freq(x) | !is.freq(y)) { stop("Both x and y must be a frequency table.") } @@ -817,21 +821,34 @@ diff.frequency_tbl <- function(x, y, ...) { } #' @rdname freq -#' @exportMethod print.frequency_tbl +#' @exportMethod print.freq #' @importFrom knitr kable #' @importFrom dplyr n_distinct #' @importFrom crayon bold silver #' @export -print.frequency_tbl <- function(x, - nmax = getOption("max.print.freq", default = 15), - markdown = !interactive(), - header = TRUE, - decimal.mark = getOption("OutDec"), - big.mark = ifelse(decimal.mark != ",", ",", "."), - ...) { +print.freq <- function(x, + nmax = getOption("max.print.freq", default = 15), + markdown = !interactive(), + header = TRUE, + decimal.mark = getOption("OutDec"), + big.mark = ifelse(decimal.mark != ",", ",", "."), + ...) { opt <- attr(x, "opt") + if (is.null(opt)) { + # selection of frequency table, return original class + class(x) <- class(x)[!class(x) %in% c("freq", "frequency_tbl")] + print(x) + return(invisible()) + } + opt$header_txt <- header(x) + if (is.null(opt$nmax)) { + opt$nmax <- 0 + } + if (is.null(opt$tbl_format)) { + opt$tbl_format <- "pandoc" + } dots <- list(...) if ("markdown" %in% names(dots)) { @@ -854,7 +871,7 @@ print.frequency_tbl <- function(x, } if (is.null(opt$title)) { - if (isTRUE(opt$data %like% "^a data.frame") & opt$tbl_format == "markdown") { + if (isTRUE(opt$data %like% "^a data.frame") & isTRUE(opt$tbl_format == "markdown")) { opt$data <- gsub("data.frame", "`data.frame`", opt$data, fixed = TRUE) } if (!is.null(opt$data) & !is.null(opt$vars)) { @@ -883,21 +900,21 @@ print.frequency_tbl <- function(x, title <- opt$title } - if (!missing(nmax)) { + if (!missing(nmax) | is.null(opt$nmax)) { opt$nmax <- nmax opt$nmax.set <- TRUE } - if (opt$nmax %in% c(0, Inf, NA, NULL)) { + if (isTRUE(opt$nmax %in% c(0, Inf, NA, NULL))) { opt$nmax <- NROW(x) opt$nmax.set <- FALSE - } else if (opt$nmax >= NROW(x)) { + } else if (isTRUE(opt$nmax >= NROW(x))) { opt$nmax.set <- FALSE } - if (!missing(decimal.mark)) { + if (!missing(decimal.mark) | is.null(opt$decimal.mark)) { opt$decimal.mark <- decimal.mark } - if (!missing(big.mark)) { + if (!missing(big.mark) | is.null(opt$big.mark)) { opt$big.mark <- big.mark } if (!missing(header)) { @@ -905,9 +922,9 @@ print.frequency_tbl <- function(x, } # bold title - if (opt$tbl_format == "pandoc") { + if (isTRUE(opt$tbl_format == "pandoc")) { title <- bold(title) - } else if (opt$tbl_format == "markdown") { + } else if (isTRUE(opt$tbl_format == "markdown")) { title <- paste0("\n\n**", title, "** ") # two space for newline } @@ -915,7 +932,7 @@ print.frequency_tbl <- function(x, if (NROW(x) == 0 | isTRUE(all(is.na(x$item)))) { cat("No observations") - if (isTRUE(all(is.na(x$item)))) { + if (isTRUE(all(is.na(x$item) | identical(x$item, "") | identical(x$item, "(NA)")))) { cat(" - all values are missing ()") } cat(".\n") @@ -925,7 +942,7 @@ print.frequency_tbl <- function(x, return(invisible()) } - if (opt$header == TRUE) { + if (isTRUE(opt$header == TRUE)) { if (!is.null(opt$header_txt)) { if (is.null(opt$digits)) { opt$digits <- 2 @@ -940,7 +957,7 @@ print.frequency_tbl <- function(x, if (is.null(opt$na)) { opt$na <- "" } - if (opt$tbl_format == "markdown") { + if (isTRUE(opt$tbl_format == "markdown")) { # no HTML tags opt$na <- gsub("<", "(", opt$na, fixed = TRUE) opt$na <- gsub(">", ")", opt$na, fixed = TRUE) @@ -951,7 +968,7 @@ print.frequency_tbl <- function(x, x.unprinted <- base::sum(x[(opt$nmax + 1):nrow(x), "count"], na.rm = TRUE) x.printed <- base::sum(x$count) - x.unprinted - if (nrow(x) > opt$nmax & opt$tbl_format != "markdown") { + if (nrow(x) > opt$nmax & isTRUE(opt$tbl_format != "markdown")) { if (opt$nmax.set == TRUE) { nmax <- opt$nmax @@ -1029,6 +1046,13 @@ print.frequency_tbl <- function(x, cat("\n") } + if (is.null(opt$row_names)) { + opt$row_names <- TRUE + } + if (is.null(opt$column_names)) { + opt$column_names <- colnames(x) + } + print( knitr::kable(x, format = opt$tbl_format, @@ -1055,37 +1079,42 @@ print.frequency_tbl <- function(x, } #' @noRd -#' @exportMethod as.data.frame.frequency_tbl +#' @exportMethod print.frequency_tbl #' @export -as.data.frame.frequency_tbl <- function(x, ...) { +print.frequency_tbl <- print.freq + +#' @noRd +#' @exportMethod as.data.frame.freq +#' @export +as.data.frame.freq <- function(x, ...) { attr(x, "package") <- NULL attr(x, "opt") <- NULL as.data.frame.data.frame(x, ...) } -#' @exportMethod select.frequency_tbl +#' @exportMethod select.freq #' @export #' @importFrom dplyr select #' @noRd -select.frequency_tbl <- function(.data, ...) { +select.freq <- function(.data, ...) { select(as.data.frame(.data), ...) } #' @noRd -#' @exportMethod as_tibble.frequency_tbl +#' @exportMethod as_tibble.freq #' @export #' @importFrom dplyr as_tibble -as_tibble.frequency_tbl <- function(x, validate = TRUE, ..., rownames = NA) { +as_tibble.freq <- function(x, validate = TRUE, ..., rownames = NA) { attr(x, "package") <- NULL attr(x, "opt") <- NULL as_tibble(x = as.data.frame(x), validate = validate, ..., rownames = rownames) } #' @noRd -#' @exportMethod hist.frequency_tbl +#' @exportMethod hist.freq #' @export #' @importFrom graphics hist -hist.frequency_tbl <- function(x, breaks = "Sturges", main = NULL, xlab = NULL, ...) { +hist.freq <- function(x, breaks = "Sturges", main = NULL, xlab = NULL, ...) { opt <- attr(x, "opt") if (!class(x$item) %in% c("numeric", "double", "integer", "Date")) { stop("`x` must be numeric or Date.", call. = FALSE) @@ -1112,9 +1141,56 @@ hist.frequency_tbl <- function(x, breaks = "Sturges", main = NULL, xlab = NULL, } #' @noRd -#' @exportMethod plot.frequency_tbl +#' @exportMethod boxplot.freq #' @export -plot.frequency_tbl <- function(x, y, ...) { +#' @importFrom graphics boxplot +boxplot.freq <- function(x, main = NULL, xlab = NULL, ...) { + opt <- attr(x, "opt") + x.bak <- x + if (!class(x$item) %in% c("numeric", "double", "integer", "Date")) { + stop("`x` must be numeric or Date.", call. = FALSE) + } + if (!is.null(opt$vars)) { + title <- opt$vars + } else if (!is.null(opt$data)) { + title <- opt$data + } else { + title <- "frequency table" + } + if (class(x$item) == "Date") { + x <- as.Date(as.vector(x), origin = "1970-01-01") + } else { + x <- as.vector(x) + } + if (is.null(main)) { + main <- paste("Boxplot of", title) + } + if (is.null(xlab)) { + xlab <- title + } + if (!is.null(opt$group_var) & isTRUE(length(opt$group_var) > 0)) { + # support for grouped frequency table + x.new <- data.frame(group = character(0), item = character(0)) + for (i in 1:nrow(x.bak)) { + if (x.bak[i, "group"] == "") { + x.bak[i, "group"] <- x.bak[i - 1, "group"] + } + for (j in 1:x.bak[i, "count"]) { + x.new <- rbind(x.new, + data.frame(group = x.bak[i, "group"], + item = x.bak[i, "item"])) + } + } + boxplot(item ~ group, data = x.bak, main = main, ylab = xlab, xlab = opt$group_var, ...) + } else { + boxplot(x, main = main, xlab = xlab, ...) + } +} + +#' @noRd +#' @exportMethod plot.freq +#' @export +plot.freq <- function(x, y, ...) { opt <- attr(x, "opt") if (!is.null(opt$vars)) { title <- opt$vars @@ -1125,16 +1201,16 @@ plot.frequency_tbl <- function(x, y, ...) { } #' @noRd -#' @exportMethod as.vector.frequency_tbl +#' @exportMethod as.vector.freq #' @export -as.vector.frequency_tbl <- function(x, mode = "any") { +as.vector.freq <- function(x, mode = "any") { as.vector(rep(x$item, x$count), mode = mode) } #' @noRd -#' @exportMethod format.frequency_tbl +#' @exportMethod format.freq #' @export -format.frequency_tbl <- function(x, digits = 1, ...) { +format.freq <- function(x, digits = 1, ...) { opt <- attr(x, "opt") if (opt$nmax.set == TRUE) { nmax <- opt$nmax diff --git a/docs/LICENSE-text.html b/docs/LICENSE-text.html index 24593f31..fde2caf9 100644 --- a/docs/LICENSE-text.html +++ b/docs/LICENSE-text.html @@ -78,7 +78,7 @@ AMR (for R) - 0.6.1.9040 + 0.6.1.9042 diff --git a/docs/articles/freq.html b/docs/articles/freq.html index fe9e6433..b0a3dd52 100644 --- a/docs/articles/freq.html +++ b/docs/articles/freq.html @@ -40,7 +40,7 @@ AMR (for R) - 0.6.1.9040 + 0.6.1.9042 @@ -736,7 +736,7 @@ Median: 31 July 2009 (47.39%)

A frequency table is actually a regular data.frame, with the exception that it contains an additional class.

-

[1] “frequency_tbl” “data.frame”

+

[1] “freq” “data.frame”

Because of this additional class, a frequency table prints like the examples above. But the object itself contains the complete table without a row limitation:

dim(my_df)

[1] 74 5

diff --git a/docs/articles/index.html b/docs/articles/index.html index ce08e4eb..6ff3241f 100644 --- a/docs/articles/index.html +++ b/docs/articles/index.html @@ -78,7 +78,7 @@ AMR (for R) - 0.6.1.9040 + 0.6.1.9042 diff --git a/docs/authors.html b/docs/authors.html index 7ed4329f..002b35ca 100644 --- a/docs/authors.html +++ b/docs/authors.html @@ -78,7 +78,7 @@ AMR (for R) - 0.6.1.9040 + 0.6.1.9042 diff --git a/docs/index.html b/docs/index.html index 96339e43..766ee653 100644 --- a/docs/index.html +++ b/docs/index.html @@ -42,7 +42,7 @@ AMR (for R) - 0.6.1.9040 + 0.6.1.9042 diff --git a/docs/news/index.html b/docs/news/index.html index 245d89c3..fd7eb547 100644 --- a/docs/news/index.html +++ b/docs/news/index.html @@ -78,7 +78,7 @@ AMR (for R) - 0.6.1.9040 + 0.6.1.9042 @@ -287,6 +287,17 @@ Please create an issue in one of our repositories if you want additions in this
  • Removed all hardcoded EUCAST rules and replaced them with a new reference file: ./inst/eucast/eucast.tsv @@ -367,32 +378,32 @@ This data is updated annually - check the included version with the new function
  • New filters for antimicrobial classes. Use these functions to filter isolates on results in one of more antibiotics from a specific class:

    - +

    The antibiotics data set will be searched, after which the input data will be checked for column names with a value in any abbreviations, codes or official names found in the antibiotics data set. For example:

    - +
  • All ab_* functions are deprecated and replaced by atc_* functions:

    - + These functions use as.atc() internally. The old atc_property has been renamed atc_online_property(). This is done for two reasons: firstly, not all ATC codes are of antibiotics (ab) but can also be of antivirals or antifungals. Secondly, the input must have class atc or must be coerable to this class. Properties of these classes should start with the same class name, analogous to as.mo() and e.g. mo_genus.
  • New functions set_mo_source() and get_mo_source() to use your own predefined MO codes as input for as.mo() and consequently all mo_* functions
  • Support for the upcoming dplyr version 0.8.0
  • @@ -404,20 +415,20 @@ These functions use as.atc()
  • New function age_groups() to split ages into custom or predefined groups (like children or elderly). This allows for easier demographic antimicrobial resistance analysis per age group.
  • New function ggplot_rsi_predict() as well as the base R plot() function can now be used for resistance prediction calculated with resistance_predict():

    -
    x <- resistance_predict(septic_patients, col_ab = "amox")
    -plot(x)
    -ggplot_rsi_predict(x)
    +
    x <- resistance_predict(septic_patients, col_ab = "amox")
    +plot(x)
    +ggplot_rsi_predict(x)
  • Functions filter_first_isolate() and filter_first_weighted_isolate() to shorten and fasten filtering on data sets with antimicrobial results, e.g.:

    - +

    is equal to:

    -
    septic_patients %>%
    -  mutate(only_firsts = first_isolate(septic_patients, ...)) %>%
    -  filter(only_firsts == TRUE) %>%
    -  select(-only_firsts)
    +
    septic_patients %>%
    +  mutate(only_firsts = first_isolate(septic_patients, ...)) %>%
    +  filter(only_firsts == TRUE) %>%
    +  select(-only_firsts)
  • New function availability() to check the number of available (non-empty) results in a data.frame
  • @@ -446,33 +457,33 @@ These functions use as.atc()

    They also come with support for German, Dutch, French, Italian, Spanish and Portuguese:

    -
    mo_gramstain("E. coli")
    -# [1] "Gram negative"
    -mo_gramstain("E. coli", language = "de") # German
    -# [1] "Gramnegativ"
    -mo_gramstain("E. coli", language = "es") # Spanish
    -# [1] "Gram negativo"
    -mo_fullname("S. group A", language = "pt") # Portuguese
    -# [1] "Streptococcus grupo A"
    +
    mo_gramstain("E. coli")
    +# [1] "Gram negative"
    +mo_gramstain("E. coli", language = "de") # German
    +# [1] "Gramnegativ"
    +mo_gramstain("E. coli", language = "es") # Spanish
    +# [1] "Gram negativo"
    +mo_fullname("S. group A", language = "pt") # Portuguese
    +# [1] "Streptococcus grupo A"

    Furthermore, former taxonomic names will give a note about the current taxonomic name:

    - +
  • Functions count_R, count_IR, count_I, count_SI and count_S to selectively count resistant or susceptible isolates diff --git a/docs/reference/age_groups.html b/docs/reference/age_groups.html index 0b522293..5e25dba6 100644 --- a/docs/reference/age_groups.html +++ b/docs/reference/age_groups.html @@ -80,7 +80,7 @@ AMR (for R) - 0.6.1.9040 + 0.6.1.9042 @@ -273,7 +273,7 @@
  • A numeric vector. A vector of e.g. c(10, 20) will split on 0-9, 10-19 and 20+. A value of only 50 will split on 0-49 and 50+. The default is to split on young children (0-11), youth (12-24), young adults (25-54), middle-aged adults (55-74) and elderly (75+).

  • A character:

      -
    • "children", equivalent of: c(0, 1, 2, 4, 6, 13, 18). This will split on 0, 1, 2-3, 4-5, 6-12, 13-17 and 18+.

    • +
    • "children" or "kids", equivalent of: c(0, 1, 2, 4, 6, 13, 18). This will split on 0, 1, 2-3, 4-5, 6-12, 13-17 and 18+.

    • "elderly" or "seniors", equivalent of: c(65, 75, 85). This will split on 0-64, 65-74, 75-84, 85+.

    • "fives", equivalent of: 1:20 * 5. This will split on 0-4, 5-9, 10-14, ..., 90-94, 95-99, 100+.

    • "tens", equivalent of: 1:10 * 10. This will split on 0-9, 10-19, 20-29, ... 80-89, 90-99, 100+.

    • @@ -291,39 +291,38 @@

      Examples

      -
      # NOT RUN {
      -ages <- c(3, 8, 16, 54, 31, 76, 101, 43, 21)
      +    
      # NOT RUN {
      +ages <- c(3, 8, 16, 54, 31, 76, 101, 43, 21)
       
      -# split into 0-49 and 50+
      -age_groups(ages, 50)
      +# split into 0-49 and 50+
      +age_groups(ages, 50)
       
      -# split into 0-19, 20-49 and 50+
      -age_groups(ages, c(20, 50))
      +# split into 0-19, 20-49 and 50+
      +age_groups(ages, c(20, 50))
       
      -# split into groups of ten years
      -age_groups(ages, 1:10 * 10)
      -age_groups(ages, split_at = "tens")
      +# split into groups of ten years
      +age_groups(ages, 1:10 * 10)
      +age_groups(ages, split_at = "tens")
       
      -# split into groups of five years
      -age_groups(ages, 1:20 * 5)
      -age_groups(ages, split_at = "fives")
      +# split into groups of five years
      +age_groups(ages, 1:20 * 5)
      +age_groups(ages, split_at = "fives")
       
      -# split specifically for children
      -age_groups(ages, "children")
      -# same:
      -age_groups(ages, c(1, 2, 4, 6, 13, 17))
      +# split specifically for children
      +age_groups(ages, "children")
      +# same:
      +age_groups(ages, c(1, 2, 4, 6, 13, 17))
       
      -# resistance of ciprofloxacine per age group
      -library(dplyr)
      -septic_patients %>%
      -  mutate(first_isolate = first_isolate(.)) %>%
      -  filter(first_isolate == TRUE,
      -         mo == as.mo("E. coli")) %>%
      -  group_by(age_group = age_groups(age)) %>%
      -  select(age_group,
      -         CIP) %>%
      -  ggplot_rsi(x = "age_group")
      -# }
      +# resistance of ciprofloxacine per age group +library(dplyr) +septic_patients %>% + filter_first_isolate) %>% + filter(mo == as.mo("E. coli")) %>% + group_by(age_group = age_groups(age)) %>% + select(age_group, CIP) %>% + ggplot_rsi(x = "age_group") +# } +
      @@ -311,15 +311,17 @@

      To define antibiotics column names, leave as it is to determine it automatically with guess_ab_col or input a text (case-insensitive), or use NULL to skip a column (e.g. TIC = NULL to skip ticarcillin). Manually defined but non-existing columns will be skipped with a warning.

      -

      Available abbrevations of the column containing antibiotics in the form 'antimicrobial ID: name (ATC code)':

      -

      AMC: amoxicillin/clavulanic acid (J01CR02), - AMK: amikacin (J01GB06), +

      The following antibiotics are used for the functions eucast_rules and mdro. These are shown in the format 'antimicrobial ID: name (ATC code)', sorted by name:

      +

      AMK: amikacin (J01GB06), AMX: amoxicillin (J01CA04), + AMC: amoxicillin/clavulanic acid (J01CR02), AMP: ampicillin (J01CA01), AZM: azithromycin (J01FA10), AZL: azlocillin (J01CA09), ATM: aztreonam (J01DF01), + CAP: capreomycin (J04AB30), RID: cefaloridine (J01DB02), + CZO: cefazolin (J01DB04), FEP: cefepime (J01DE01), CTX: cefotaxime (J01DD01), FOX: cefoxitin (J01DC01), @@ -331,26 +333,28 @@ CIP: ciprofloxacin (J01MA02), CLR: clarithromycin (J01FA09), CLI: clindamycin (J01FF01), - FLC: flucloxacillin (J01CF05), COL: colistin (J01XB01), - CZO: cefazolin (J01DB04), DAP: daptomycin (J01XX09), DOX: doxycycline (J01AA02), ETP: ertapenem (J01DH03), ERY: erythromycin (J01FA01), + ETH: ethambutol (J04AK02), + FLC: flucloxacillin (J01CF05), FOS: fosfomycin (J01XX01), FUS: fusidic acid (J01XC01), + GAT: gatifloxacin (J01MA16), GEN: gentamicin (J01GB03), IPM: imipenem (J01DH51), + INH: isoniazid (J04AC01), KAN: kanamycin (J01GB04), LVX: levofloxacin (J01MA12), LIN: lincomycin (J01FF02), LNZ: linezolid (J01XX08), MEM: meropenem (J01DH02), + MTR: metronidazole (J01XD01), MEZ: mezlocillin (J01CA10), MNO: minocycline (J01AA08), MFX: moxifloxacin (J01MA14), - MTR: metronidazole (J01XD01), NAL: nalidixic acid (J01MB02), NEO: neomycin (J01GB05), NET: netilmicin (J01GB07), @@ -358,14 +362,18 @@ NOR: norfloxacin (J01MA06), NOV: novobiocin (an ATCvet code: QJ01XX95), OFX: ofloxacin (J01MA01), - OXA: oxacillin (J01CF04), + OXA: oxacillin (J01CF04), PEN: penicillin G (J01CE01), PIP: piperacillin (J01CA12), TZP: piperacillin/tazobactam (J01CR05), PLB: polymyxin B (J01XB02), PRI: pristinamycin (J01FG01), + PZA: pyrazinamide (J04AK01), QDA: quinupristin/dalfopristin (J01FG02), + RIB: rifabutin (J04AB04), RIF: rifampicin (J04AB02), + RIF: rifampin (J04AB02), + RFP: rifapentine (J04AB05), RXT: roxithromycin (J01FA06), SIS: sisomicin (J01GB08), TEC: teicoplanin (J01XA02), diff --git a/docs/reference/freq.html b/docs/reference/freq.html index ad7e1256..693eaa9a 100644 --- a/docs/reference/freq.html +++ b/docs/reference/freq.html @@ -81,7 +81,7 @@ top_freq can be used to get the top/bottom n items of a frequency table, with co AMR (for R) - 0.6.1.9040 + 0.6.1.9042 @@ -250,27 +250,27 @@ top_freq can be used to get the top/bottom n items of a frequency table, with co -

      frequency_tbl(x, ..., sort.count = TRUE,
      -  nmax = getOption("max.print.freq"), na.rm = TRUE, row.names = TRUE,
      -  markdown = !interactive(), digits = 2, quote = FALSE,
      -  header = TRUE, title = NULL, na = "<NA>", droplevels = TRUE,
      -  sep = " ", decimal.mark = getOption("OutDec"),
      -  big.mark = ifelse(decimal.mark != ",", ",", "."))
      -
      -freq(x, ..., sort.count = TRUE, nmax = getOption("max.print.freq"),
      +    
      freq(x, ..., sort.count = TRUE, nmax = getOption("max.print.freq"),
         na.rm = TRUE, row.names = TRUE, markdown = !interactive(),
         digits = 2, quote = FALSE, header = TRUE, title = NULL,
         na = "<NA>", droplevels = TRUE, sep = " ",
         decimal.mark = getOption("OutDec"), big.mark = ifelse(decimal.mark !=
         ",", ",", "."))
       
      +frequency_tbl(x, ..., sort.count = TRUE,
      +  nmax = getOption("max.print.freq"), na.rm = TRUE, row.names = TRUE,
      +  markdown = !interactive(), digits = 2, quote = FALSE,
      +  header = TRUE, title = NULL, na = "<NA>", droplevels = TRUE,
      +  sep = " ", decimal.mark = getOption("OutDec"),
      +  big.mark = ifelse(decimal.mark != ",", ",", "."))
      +
       top_freq(f, n)
       
       header(f, property = NULL)
       
      -# S3 method for frequency_tbl
      -print(x, nmax = getOption("max.print.freq",
      -  default = 15), markdown = !interactive(), header = TRUE,
      +# S3 method for freq
      +print(x, nmax = getOption("max.print.freq", default = 15),
      +  markdown = !interactive(), header = TRUE,
         decimal.mark = getOption("OutDec"), big.mark = ifelse(decimal.mark !=
         ",", ",", "."), ...)
      @@ -361,7 +361,7 @@ top_freq can be used to get the top/bottom n items of a frequency table, with co

      Value

      -

      A data.frame (with an additional class "frequency_tbl") with five columns: item, count, percent, cum_count and cum_percent.

      +

      A data.frame (with an additional class "freq") with five columns: item, count, percent, cum_count and cum_percent.

      Details

      @@ -452,6 +452,16 @@ top_freq can be used to get the top/bottom n items of a frequency table, with co freq(age) %>% hist() +# or a boxplot of numeric values +septic_patients %>% + freq(age) %>% + boxplot() + +# or even a boxplot per group +septic_patients %>% + group_by(hospital_id) %>% + freq(age) %>% + boxplot() # or print all points to a regular plot septic_patients %>% diff --git a/docs/reference/index.html b/docs/reference/index.html index 3c6b6f0a..a6b35ce7 100644 --- a/docs/reference/index.html +++ b/docs/reference/index.html @@ -78,7 +78,7 @@ AMR (for R) - 0.6.1.9040 + 0.6.1.9042 @@ -454,7 +454,7 @@ -

      frequency_tbl() freq() top_freq() header() print(<frequency_tbl>)

      +

      freq() frequency_tbl() top_freq() header() print(<freq>)

      Frequency table

      diff --git a/docs/reference/mdro.html b/docs/reference/mdro.html index ee4b59d6..23a31dea 100644 --- a/docs/reference/mdro.html +++ b/docs/reference/mdro.html @@ -80,7 +80,7 @@ AMR (for R) - 0.6.1.9040 + 0.6.1.9042 @@ -312,15 +312,17 @@

      To define antibiotics column names, leave as it is to determine it automatically with guess_ab_col or input a text (case-insensitive), or use NULL to skip a column (e.g. TIC = NULL to skip ticarcillin). Manually defined but non-existing columns will be skipped with a warning.

      -

      Available abbrevations of the column containing antibiotics in the form 'antimicrobial ID: name (ATC code)':

      -

      AMC: amoxicillin/clavulanic acid (J01CR02), - AMK: amikacin (J01GB06), +

      The following antibiotics are used for the functions eucast_rules and mdro. These are shown in the format 'antimicrobial ID: name (ATC code)', sorted by name:

      +

      AMK: amikacin (J01GB06), AMX: amoxicillin (J01CA04), + AMC: amoxicillin/clavulanic acid (J01CR02), AMP: ampicillin (J01CA01), AZM: azithromycin (J01FA10), AZL: azlocillin (J01CA09), ATM: aztreonam (J01DF01), + CAP: capreomycin (J04AB30), RID: cefaloridine (J01DB02), + CZO: cefazolin (J01DB04), FEP: cefepime (J01DE01), CTX: cefotaxime (J01DD01), FOX: cefoxitin (J01DC01), @@ -332,26 +334,28 @@ CIP: ciprofloxacin (J01MA02), CLR: clarithromycin (J01FA09), CLI: clindamycin (J01FF01), - FLC: flucloxacillin (J01CF05), COL: colistin (J01XB01), - CZO: cefazolin (J01DB04), DAP: daptomycin (J01XX09), DOX: doxycycline (J01AA02), ETP: ertapenem (J01DH03), ERY: erythromycin (J01FA01), + ETH: ethambutol (J04AK02), + FLC: flucloxacillin (J01CF05), FOS: fosfomycin (J01XX01), FUS: fusidic acid (J01XC01), + GAT: gatifloxacin (J01MA16), GEN: gentamicin (J01GB03), IPM: imipenem (J01DH51), + INH: isoniazid (J04AC01), KAN: kanamycin (J01GB04), LVX: levofloxacin (J01MA12), LIN: lincomycin (J01FF02), LNZ: linezolid (J01XX08), MEM: meropenem (J01DH02), + MTR: metronidazole (J01XD01), MEZ: mezlocillin (J01CA10), MNO: minocycline (J01AA08), MFX: moxifloxacin (J01MA14), - MTR: metronidazole (J01XD01), NAL: nalidixic acid (J01MB02), NEO: neomycin (J01GB05), NET: netilmicin (J01GB07), @@ -359,14 +363,18 @@ NOR: norfloxacin (J01MA06), NOV: novobiocin (an ATCvet code: QJ01XX95), OFX: ofloxacin (J01MA01), - OXA: oxacillin (J01CF04), + OXA: oxacillin (J01CF04), PEN: penicillin G (J01CE01), PIP: piperacillin (J01CA12), TZP: piperacillin/tazobactam (J01CR05), PLB: polymyxin B (J01XB02), PRI: pristinamycin (J01FG01), + PZA: pyrazinamide (J04AK01), QDA: quinupristin/dalfopristin (J01FG02), + RIB: rifabutin (J04AB04), RIF: rifampicin (J04AB02), + RIF: rifampin (J04AB02), + RFP: rifapentine (J04AB05), RXT: roxithromycin (J01FA06), SIS: sisomicin (J01GB08), TEC: teicoplanin (J01XA02), diff --git a/man/eucast_rules.Rd b/man/eucast_rules.Rd index d24c37a5..c6760c39 100644 --- a/man/eucast_rules.Rd +++ b/man/eucast_rules.Rd @@ -61,16 +61,18 @@ In the source code the file containing all rules is located \href{https://gitlab To define antibiotics column names, leave as it is to determine it automatically with \code{\link{guess_ab_col}} or input a text (case-insensitive), or use \code{NULL} to skip a column (e.g. \code{TIC = NULL} to skip ticarcillin). Manually defined but non-existing columns will be skipped with a warning. -Available abbrevations of the column containing antibiotics in the form '\strong{antimicrobial ID}: name (\emph{ATC code})': +The following antibiotics are used for the functions \code{\link{eucast_rules}} and \code{\link{mdro}}. These are shown in the format '\strong{antimicrobial ID}: name (\emph{ATC code})', sorted by name: - \strong{AMC}: amoxicillin/clavulanic acid (\href{https://www.whocc.no/atc_ddd_index/?code=J01CR02}{J01CR02}), \strong{AMK}: amikacin (\href{https://www.whocc.no/atc_ddd_index/?code=J01GB06}{J01GB06}), \strong{AMX}: amoxicillin (\href{https://www.whocc.no/atc_ddd_index/?code=J01CA04}{J01CA04}), + \strong{AMC}: amoxicillin/clavulanic acid (\href{https://www.whocc.no/atc_ddd_index/?code=J01CR02}{J01CR02}), \strong{AMP}: ampicillin (\href{https://www.whocc.no/atc_ddd_index/?code=J01CA01}{J01CA01}), \strong{AZM}: azithromycin (\href{https://www.whocc.no/atc_ddd_index/?code=J01FA10}{J01FA10}), \strong{AZL}: azlocillin (\href{https://www.whocc.no/atc_ddd_index/?code=J01CA09}{J01CA09}), \strong{ATM}: aztreonam (\href{https://www.whocc.no/atc_ddd_index/?code=J01DF01}{J01DF01}), + \strong{CAP}: capreomycin (\href{https://www.whocc.no/atc_ddd_index/?code=J04AB30}{J04AB30}), \strong{RID}: cefaloridine (\href{https://www.whocc.no/atc_ddd_index/?code=J01DB02}{J01DB02}), + \strong{CZO}: cefazolin (\href{https://www.whocc.no/atc_ddd_index/?code=J01DB04}{J01DB04}), \strong{FEP}: cefepime (\href{https://www.whocc.no/atc_ddd_index/?code=J01DE01}{J01DE01}), \strong{CTX}: cefotaxime (\href{https://www.whocc.no/atc_ddd_index/?code=J01DD01}{J01DD01}), \strong{FOX}: cefoxitin (\href{https://www.whocc.no/atc_ddd_index/?code=J01DC01}{J01DC01}), @@ -82,26 +84,28 @@ Available abbrevations of the column containing antibiotics in the form '\strong \strong{CIP}: ciprofloxacin (\href{https://www.whocc.no/atc_ddd_index/?code=J01MA02}{J01MA02}), \strong{CLR}: clarithromycin (\href{https://www.whocc.no/atc_ddd_index/?code=J01FA09}{J01FA09}), \strong{CLI}: clindamycin (\href{https://www.whocc.no/atc_ddd_index/?code=J01FF01}{J01FF01}), - \strong{FLC}: flucloxacillin (\href{https://www.whocc.no/atc_ddd_index/?code=J01CF05}{J01CF05}), \strong{COL}: colistin (\href{https://www.whocc.no/atc_ddd_index/?code=J01XB01}{J01XB01}), - \strong{CZO}: cefazolin (\href{https://www.whocc.no/atc_ddd_index/?code=J01DB04}{J01DB04}), \strong{DAP}: daptomycin (\href{https://www.whocc.no/atc_ddd_index/?code=J01XX09}{J01XX09}), \strong{DOX}: doxycycline (\href{https://www.whocc.no/atc_ddd_index/?code=J01AA02}{J01AA02}), \strong{ETP}: ertapenem (\href{https://www.whocc.no/atc_ddd_index/?code=J01DH03}{J01DH03}), \strong{ERY}: erythromycin (\href{https://www.whocc.no/atc_ddd_index/?code=J01FA01}{J01FA01}), + \strong{ETH}: ethambutol (\href{https://www.whocc.no/atc_ddd_index/?code=J04AK02}{J04AK02}), + \strong{FLC}: flucloxacillin (\href{https://www.whocc.no/atc_ddd_index/?code=J01CF05}{J01CF05}), \strong{FOS}: fosfomycin (\href{https://www.whocc.no/atc_ddd_index/?code=J01XX01}{J01XX01}), \strong{FUS}: fusidic acid (\href{https://www.whocc.no/atc_ddd_index/?code=J01XC01}{J01XC01}), + \strong{GAT}: gatifloxacin (\href{https://www.whocc.no/atc_ddd_index/?code=J01MA16}{J01MA16}), \strong{GEN}: gentamicin (\href{https://www.whocc.no/atc_ddd_index/?code=J01GB03}{J01GB03}), \strong{IPM}: imipenem (\href{https://www.whocc.no/atc_ddd_index/?code=J01DH51}{J01DH51}), + \strong{INH}: isoniazid (\href{https://www.whocc.no/atc_ddd_index/?code=J04AC01}{J04AC01}), \strong{KAN}: kanamycin (\href{https://www.whocc.no/atc_ddd_index/?code=J01GB04}{J01GB04}), \strong{LVX}: levofloxacin (\href{https://www.whocc.no/atc_ddd_index/?code=J01MA12}{J01MA12}), \strong{LIN}: lincomycin (\href{https://www.whocc.no/atc_ddd_index/?code=J01FF02}{J01FF02}), \strong{LNZ}: linezolid (\href{https://www.whocc.no/atc_ddd_index/?code=J01XX08}{J01XX08}), \strong{MEM}: meropenem (\href{https://www.whocc.no/atc_ddd_index/?code=J01DH02}{J01DH02}), + \strong{MTR}: metronidazole (\href{https://www.whocc.no/atc_ddd_index/?code=J01XD01}{J01XD01}), \strong{MEZ}: mezlocillin (\href{https://www.whocc.no/atc_ddd_index/?code=J01CA10}{J01CA10}), \strong{MNO}: minocycline (\href{https://www.whocc.no/atc_ddd_index/?code=J01AA08}{J01AA08}), \strong{MFX}: moxifloxacin (\href{https://www.whocc.no/atc_ddd_index/?code=J01MA14}{J01MA14}), - \strong{MTR}: metronidazole (\href{https://www.whocc.no/atc_ddd_index/?code=J01MA14}{J01XD01}), \strong{NAL}: nalidixic acid (\href{https://www.whocc.no/atc_ddd_index/?code=J01MB02}{J01MB02}), \strong{NEO}: neomycin (\href{https://www.whocc.no/atc_ddd_index/?code=J01GB05}{J01GB05}), \strong{NET}: netilmicin (\href{https://www.whocc.no/atc_ddd_index/?code=J01GB07}{J01GB07}), @@ -109,14 +113,18 @@ Available abbrevations of the column containing antibiotics in the form '\strong \strong{NOR}: norfloxacin (\href{https://www.whocc.no/atc_ddd_index/?code=J01MA06}{J01MA06}), \strong{NOV}: novobiocin (an ATCvet code: \href{https://www.whocc.no/atc_ddd_index/?code=QJ01XX95}{QJ01XX95}), \strong{OFX}: ofloxacin (\href{https://www.whocc.no/atc_ddd_index/?code=J01MA01}{J01MA01}), - \strong{OXA}: oxacillin (\href{https://www.whocc.no/atc_ddd_index/?code=J01MA01}{J01CF04}), + \strong{OXA}: oxacillin (\href{https://www.whocc.no/atc_ddd_index/?code=J01CF04}{J01CF04}), \strong{PEN}: penicillin G (\href{https://www.whocc.no/atc_ddd_index/?code=J01CE01}{J01CE01}), \strong{PIP}: piperacillin (\href{https://www.whocc.no/atc_ddd_index/?code=J01CA12}{J01CA12}), \strong{TZP}: piperacillin/tazobactam (\href{https://www.whocc.no/atc_ddd_index/?code=J01CR05}{J01CR05}), \strong{PLB}: polymyxin B (\href{https://www.whocc.no/atc_ddd_index/?code=J01XB02}{J01XB02}), \strong{PRI}: pristinamycin (\href{https://www.whocc.no/atc_ddd_index/?code=J01FG01}{J01FG01}), + \strong{PZA}: pyrazinamide (\href{https://www.whocc.no/atc_ddd_index/?code=J04AK01}{J04AK01}), \strong{QDA}: quinupristin/dalfopristin (\href{https://www.whocc.no/atc_ddd_index/?code=J01FG02}{J01FG02}), + \strong{RIB}: rifabutin (\href{https://www.whocc.no/atc_ddd_index/?code=J04AB04}{J04AB04}), \strong{RIF}: rifampicin (\href{https://www.whocc.no/atc_ddd_index/?code=J04AB02}{J04AB02}), + \strong{RIF}: rifampin (\href{https://www.whocc.no/atc_ddd_index/?code=J04AB02}{J04AB02}), + \strong{RFP}: rifapentine (\href{https://www.whocc.no/atc_ddd_index/?code=J04AB05}{J04AB05}), \strong{RXT}: roxithromycin (\href{https://www.whocc.no/atc_ddd_index/?code=J01FA06}{J01FA06}), \strong{SIS}: sisomicin (\href{https://www.whocc.no/atc_ddd_index/?code=J01GB08}{J01GB08}), \strong{TEC}: teicoplanin (\href{https://www.whocc.no/atc_ddd_index/?code=J01XA02}{J01XA02}), diff --git a/man/freq.Rd b/man/freq.Rd index 2e0c4be2..e2212969 100755 --- a/man/freq.Rd +++ b/man/freq.Rd @@ -5,16 +5,9 @@ \alias{frequency_tbl} \alias{top_freq} \alias{header} -\alias{print.frequency_tbl} +\alias{print.freq} \title{Frequency table} \usage{ -frequency_tbl(x, ..., sort.count = TRUE, - nmax = getOption("max.print.freq"), na.rm = TRUE, row.names = TRUE, - markdown = !interactive(), digits = 2, quote = FALSE, - header = TRUE, title = NULL, na = "", droplevels = TRUE, - sep = " ", decimal.mark = getOption("OutDec"), - big.mark = ifelse(decimal.mark != ",", ",", ".")) - freq(x, ..., sort.count = TRUE, nmax = getOption("max.print.freq"), na.rm = TRUE, row.names = TRUE, markdown = !interactive(), digits = 2, quote = FALSE, header = TRUE, title = NULL, @@ -22,12 +15,19 @@ freq(x, ..., sort.count = TRUE, nmax = getOption("max.print.freq"), decimal.mark = getOption("OutDec"), big.mark = ifelse(decimal.mark != ",", ",", ".")) +frequency_tbl(x, ..., sort.count = TRUE, + nmax = getOption("max.print.freq"), na.rm = TRUE, row.names = TRUE, + markdown = !interactive(), digits = 2, quote = FALSE, + header = TRUE, title = NULL, na = "", droplevels = TRUE, + sep = " ", decimal.mark = getOption("OutDec"), + big.mark = ifelse(decimal.mark != ",", ",", ".")) + top_freq(f, n) header(f, property = NULL) -\method{print}{frequency_tbl}(x, nmax = getOption("max.print.freq", - default = 15), markdown = !interactive(), header = TRUE, +\method{print}{freq}(x, nmax = getOption("max.print.freq", default = 15), + markdown = !interactive(), header = TRUE, decimal.mark = getOption("OutDec"), big.mark = ifelse(decimal.mark != ",", ",", "."), ...) } @@ -75,7 +75,7 @@ header(f, property = NULL) \item{property}{property in header to return this value directly} } \value{ -A \code{data.frame} (with an additional class \code{"frequency_tbl"}) with five columns: \code{item}, \code{count}, \code{percent}, \code{cum_count} and \code{cum_percent}. +A \code{data.frame} (with an additional class \code{"freq"}) with five columns: \code{item}, \code{count}, \code{percent}, \code{cum_count} and \code{cum_percent}. } \description{ Create a frequency table of a vector with items or a \code{data.frame}. Supports quasiquotation and markdown for reports. Best practice is: \code{data \%>\% freq(var)}.\cr @@ -173,6 +173,16 @@ septic_patients \%>\% freq(age) \%>\% hist() +# or a boxplot of numeric values +septic_patients \%>\% + freq(age) \%>\% + boxplot() + +# or even a boxplot per group +septic_patients \%>\% + group_by(hospital_id) \%>\% + freq(age) \%>\% + boxplot() # or print all points to a regular plot septic_patients \%>\% diff --git a/man/mdro.Rd b/man/mdro.Rd index 4bc09b9d..ab97b154 100644 --- a/man/mdro.Rd +++ b/man/mdro.Rd @@ -59,16 +59,18 @@ Other currently supported guidelines are: To define antibiotics column names, leave as it is to determine it automatically with \code{\link{guess_ab_col}} or input a text (case-insensitive), or use \code{NULL} to skip a column (e.g. \code{TIC = NULL} to skip ticarcillin). Manually defined but non-existing columns will be skipped with a warning. -Available abbrevations of the column containing antibiotics in the form '\strong{antimicrobial ID}: name (\emph{ATC code})': +The following antibiotics are used for the functions \code{\link{eucast_rules}} and \code{\link{mdro}}. These are shown in the format '\strong{antimicrobial ID}: name (\emph{ATC code})', sorted by name: - \strong{AMC}: amoxicillin/clavulanic acid (\href{https://www.whocc.no/atc_ddd_index/?code=J01CR02}{J01CR02}), \strong{AMK}: amikacin (\href{https://www.whocc.no/atc_ddd_index/?code=J01GB06}{J01GB06}), \strong{AMX}: amoxicillin (\href{https://www.whocc.no/atc_ddd_index/?code=J01CA04}{J01CA04}), + \strong{AMC}: amoxicillin/clavulanic acid (\href{https://www.whocc.no/atc_ddd_index/?code=J01CR02}{J01CR02}), \strong{AMP}: ampicillin (\href{https://www.whocc.no/atc_ddd_index/?code=J01CA01}{J01CA01}), \strong{AZM}: azithromycin (\href{https://www.whocc.no/atc_ddd_index/?code=J01FA10}{J01FA10}), \strong{AZL}: azlocillin (\href{https://www.whocc.no/atc_ddd_index/?code=J01CA09}{J01CA09}), \strong{ATM}: aztreonam (\href{https://www.whocc.no/atc_ddd_index/?code=J01DF01}{J01DF01}), + \strong{CAP}: capreomycin (\href{https://www.whocc.no/atc_ddd_index/?code=J04AB30}{J04AB30}), \strong{RID}: cefaloridine (\href{https://www.whocc.no/atc_ddd_index/?code=J01DB02}{J01DB02}), + \strong{CZO}: cefazolin (\href{https://www.whocc.no/atc_ddd_index/?code=J01DB04}{J01DB04}), \strong{FEP}: cefepime (\href{https://www.whocc.no/atc_ddd_index/?code=J01DE01}{J01DE01}), \strong{CTX}: cefotaxime (\href{https://www.whocc.no/atc_ddd_index/?code=J01DD01}{J01DD01}), \strong{FOX}: cefoxitin (\href{https://www.whocc.no/atc_ddd_index/?code=J01DC01}{J01DC01}), @@ -80,26 +82,28 @@ Available abbrevations of the column containing antibiotics in the form '\strong \strong{CIP}: ciprofloxacin (\href{https://www.whocc.no/atc_ddd_index/?code=J01MA02}{J01MA02}), \strong{CLR}: clarithromycin (\href{https://www.whocc.no/atc_ddd_index/?code=J01FA09}{J01FA09}), \strong{CLI}: clindamycin (\href{https://www.whocc.no/atc_ddd_index/?code=J01FF01}{J01FF01}), - \strong{FLC}: flucloxacillin (\href{https://www.whocc.no/atc_ddd_index/?code=J01CF05}{J01CF05}), \strong{COL}: colistin (\href{https://www.whocc.no/atc_ddd_index/?code=J01XB01}{J01XB01}), - \strong{CZO}: cefazolin (\href{https://www.whocc.no/atc_ddd_index/?code=J01DB04}{J01DB04}), \strong{DAP}: daptomycin (\href{https://www.whocc.no/atc_ddd_index/?code=J01XX09}{J01XX09}), \strong{DOX}: doxycycline (\href{https://www.whocc.no/atc_ddd_index/?code=J01AA02}{J01AA02}), \strong{ETP}: ertapenem (\href{https://www.whocc.no/atc_ddd_index/?code=J01DH03}{J01DH03}), \strong{ERY}: erythromycin (\href{https://www.whocc.no/atc_ddd_index/?code=J01FA01}{J01FA01}), + \strong{ETH}: ethambutol (\href{https://www.whocc.no/atc_ddd_index/?code=J04AK02}{J04AK02}), + \strong{FLC}: flucloxacillin (\href{https://www.whocc.no/atc_ddd_index/?code=J01CF05}{J01CF05}), \strong{FOS}: fosfomycin (\href{https://www.whocc.no/atc_ddd_index/?code=J01XX01}{J01XX01}), \strong{FUS}: fusidic acid (\href{https://www.whocc.no/atc_ddd_index/?code=J01XC01}{J01XC01}), + \strong{GAT}: gatifloxacin (\href{https://www.whocc.no/atc_ddd_index/?code=J01MA16}{J01MA16}), \strong{GEN}: gentamicin (\href{https://www.whocc.no/atc_ddd_index/?code=J01GB03}{J01GB03}), \strong{IPM}: imipenem (\href{https://www.whocc.no/atc_ddd_index/?code=J01DH51}{J01DH51}), + \strong{INH}: isoniazid (\href{https://www.whocc.no/atc_ddd_index/?code=J04AC01}{J04AC01}), \strong{KAN}: kanamycin (\href{https://www.whocc.no/atc_ddd_index/?code=J01GB04}{J01GB04}), \strong{LVX}: levofloxacin (\href{https://www.whocc.no/atc_ddd_index/?code=J01MA12}{J01MA12}), \strong{LIN}: lincomycin (\href{https://www.whocc.no/atc_ddd_index/?code=J01FF02}{J01FF02}), \strong{LNZ}: linezolid (\href{https://www.whocc.no/atc_ddd_index/?code=J01XX08}{J01XX08}), \strong{MEM}: meropenem (\href{https://www.whocc.no/atc_ddd_index/?code=J01DH02}{J01DH02}), + \strong{MTR}: metronidazole (\href{https://www.whocc.no/atc_ddd_index/?code=J01XD01}{J01XD01}), \strong{MEZ}: mezlocillin (\href{https://www.whocc.no/atc_ddd_index/?code=J01CA10}{J01CA10}), \strong{MNO}: minocycline (\href{https://www.whocc.no/atc_ddd_index/?code=J01AA08}{J01AA08}), \strong{MFX}: moxifloxacin (\href{https://www.whocc.no/atc_ddd_index/?code=J01MA14}{J01MA14}), - \strong{MTR}: metronidazole (\href{https://www.whocc.no/atc_ddd_index/?code=J01MA14}{J01XD01}), \strong{NAL}: nalidixic acid (\href{https://www.whocc.no/atc_ddd_index/?code=J01MB02}{J01MB02}), \strong{NEO}: neomycin (\href{https://www.whocc.no/atc_ddd_index/?code=J01GB05}{J01GB05}), \strong{NET}: netilmicin (\href{https://www.whocc.no/atc_ddd_index/?code=J01GB07}{J01GB07}), @@ -107,14 +111,18 @@ Available abbrevations of the column containing antibiotics in the form '\strong \strong{NOR}: norfloxacin (\href{https://www.whocc.no/atc_ddd_index/?code=J01MA06}{J01MA06}), \strong{NOV}: novobiocin (an ATCvet code: \href{https://www.whocc.no/atc_ddd_index/?code=QJ01XX95}{QJ01XX95}), \strong{OFX}: ofloxacin (\href{https://www.whocc.no/atc_ddd_index/?code=J01MA01}{J01MA01}), - \strong{OXA}: oxacillin (\href{https://www.whocc.no/atc_ddd_index/?code=J01MA01}{J01CF04}), + \strong{OXA}: oxacillin (\href{https://www.whocc.no/atc_ddd_index/?code=J01CF04}{J01CF04}), \strong{PEN}: penicillin G (\href{https://www.whocc.no/atc_ddd_index/?code=J01CE01}{J01CE01}), \strong{PIP}: piperacillin (\href{https://www.whocc.no/atc_ddd_index/?code=J01CA12}{J01CA12}), \strong{TZP}: piperacillin/tazobactam (\href{https://www.whocc.no/atc_ddd_index/?code=J01CR05}{J01CR05}), \strong{PLB}: polymyxin B (\href{https://www.whocc.no/atc_ddd_index/?code=J01XB02}{J01XB02}), \strong{PRI}: pristinamycin (\href{https://www.whocc.no/atc_ddd_index/?code=J01FG01}{J01FG01}), + \strong{PZA}: pyrazinamide (\href{https://www.whocc.no/atc_ddd_index/?code=J04AK01}{J04AK01}), \strong{QDA}: quinupristin/dalfopristin (\href{https://www.whocc.no/atc_ddd_index/?code=J01FG02}{J01FG02}), + \strong{RIB}: rifabutin (\href{https://www.whocc.no/atc_ddd_index/?code=J04AB04}{J04AB04}), \strong{RIF}: rifampicin (\href{https://www.whocc.no/atc_ddd_index/?code=J04AB02}{J04AB02}), + \strong{RIF}: rifampin (\href{https://www.whocc.no/atc_ddd_index/?code=J04AB02}{J04AB02}), + \strong{RFP}: rifapentine (\href{https://www.whocc.no/atc_ddd_index/?code=J04AB05}{J04AB05}), \strong{RXT}: roxithromycin (\href{https://www.whocc.no/atc_ddd_index/?code=J01FA06}{J01FA06}), \strong{SIS}: sisomicin (\href{https://www.whocc.no/atc_ddd_index/?code=J01GB08}{J01GB08}), \strong{TEC}: teicoplanin (\href{https://www.whocc.no/atc_ddd_index/?code=J01XA02}{J01XA02}), diff --git a/tests/testthat/test-freq.R b/tests/testthat/test-freq.R index e757e731..60ea4265 100755 --- a/tests/testthat/test-freq.R +++ b/tests/testthat/test-freq.R @@ -47,7 +47,10 @@ test_that("frequency table works", { expect_output(print(freq(septic_patients$age, markdown = TRUE, title = "TITLE"))) # character - expect_output(suppressWarnings(print(freq(microorganisms$fullname)))) + expect_output(print(freq(microorganisms$genus))) + expect_output(print(structure(freq(microorganisms$genus), + # check printing of old class: + class = c("frequency_tbl", "data.frame")))) # mo expect_output(print(freq(septic_patients$mo))) # rsi @@ -121,9 +124,11 @@ test_that("frequency table works", { # input must be freq tbl expect_error(septic_patients %>% top_freq(1)) - # charts from plot and hist, should not raise errors + # charts from plot, hist and boxplot, should not raise errors plot(freq(septic_patients, age)) hist(freq(septic_patients, age)) + boxplot(freq(septic_patients, age)) + boxplot(freq(dplyr::group_by(septic_patients, gender), age)) # check vector expect_identical(septic_patients %>%