diff --git a/.Rbuildignore b/.Rbuildignore index 1bcf3840..ad2cea0a 100755 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -24,8 +24,9 @@ ^\.lintr$ ^tests/testthat/_snaps$ ^vignettes/AMR\.Rmd$ +^vignettes/AMR_intro\.png$ ^vignettes/benchmarks\.Rmd$ -^vignettes/*\.not$ +^vignettes/benchmarks\.Rmd\.not$ ^vignettes/datasets\.Rmd$ ^vignettes/EUCAST\.Rmd$ ^vignettes/MDR\.Rmd$ diff --git a/AMR.Rproj b/AMR.Rproj index 1604a99f..72aaa5f7 100644 --- a/AMR.Rproj +++ b/AMR.Rproj @@ -1,7 +1,7 @@ Version: 1.0 RestoreWorkspace: No -SaveWorkspace: Ask +SaveWorkspace: No AlwaysSaveHistory: Yes EnableCodeIndexing: Yes @@ -13,6 +13,7 @@ RnwWeave: Sweave LaTeX: pdfLaTeX AutoAppendNewline: Yes +LineEndingConversion: Posix BuildType: Package PackageUseDevtools: Yes @@ -20,3 +21,5 @@ PackageInstallArgs: --no-multiarch --with-keep.source PackageBuildArgs: --no-build-vignettes PackageCheckArgs: --no-build-vignettes --as-cran PackageRoxygenize: rd,collate,namespace + +UseNativePipeOperator: No diff --git a/DESCRIPTION b/DESCRIPTION index 0bbcb928..09597b04 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,35 +1,36 @@ Package: AMR -Version: 1.8.1.9030 -Date: 2022-08-26 +Version: 1.8.1.9031 +Date: 2022-08-27 Title: Antimicrobial Resistance Data Analysis Description: Functions to simplify and standardise antimicrobial resistance (AMR) data analysis and to work with microbial and antimicrobial properties by using evidence-based methods and reliable reference data such as LPSN . Authors@R: c( - person(c("Matthijs", "S."), "Berends", role = c("aut", "cre"), comment = c(ORCID = "0000-0001-7620-1800"), email = "m.berends@certe.nl"), - person(c("Christian", "F."), "Luz", role = c("aut", "ctb"), comment = c(ORCID = "0000-0001-5809-5995")), - person("Dennis", "Souverein", role = c("aut", "ctb"), comment = c(ORCID = "0000-0003-0455-0336")), - person(c("Erwin", "E.", "A."), "Hassing", role = c("aut", "ctb")), - person("Casper", "Albers", role = "ths", comment = c(ORCID = "0000-0002-9213-6743")), - person("Peter", "Dutey-Magni", role = "ctb", comment = c(ORCID = "0000-0002-8942-9836")), - person("Judith", "Fonville", role = "ctb"), - person("Alex", "Friedrich", role = "ths", comment = c(ORCID = "0000-0003-4881-038X")), - person("Corinna", "Glasner", role = "ths", comment = c(ORCID = "0000-0003-1241-1328")), - person("Eric", "Hazenberg", role = "ctb"), - person("Gwen", "Knight", role = "ctb", comment = c(ORCID = "0000-0002-7263-9896")), - person("Annick", "Lenglet", role = "ctb", comment = c(ORCID = "0000-0003-2013-8405")), - person("Bart", "Meijer", role = "ctb"), - person("Anton", "Mymrikov", role = "ctb"), - person("Sofia", "Ny", role = "ctb", comment = c(ORCID = "0000-0002-2017-1363")), - person("Rogier", "Schade", role = "ctb"), - person("Bhanu", "Sinha", role = "ths", comment = c(ORCID = "0000-0003-1634-0010")), - person("Anthony", "Underwood", role = "ctb", comment = c(ORCID = "0000-0002-8547-4277"))) + person(family = "Berends", c("Matthijs", "S."), role = c("aut", "cre"), comment = c(ORCID = "0000-0001-7620-1800"), email = "m.berends@certe.nl"), + person(family = "Luz", c("Christian", "F."), role = c("aut", "ctb"), comment = c(ORCID = "0000-0001-5809-5995")), + person(family = "Souverein", c("Dennis"), role = c("aut", "ctb"), comment = c(ORCID = "0000-0003-0455-0336")), + person(family = "Hassing", c("Erwin", "E.", "A."), role = c("aut", "ctb")), + person(family = "Albers", c("Casper", "J."), role = "ths", comment = c(ORCID = "0000-0002-9213-6743")), + person(family = "Dutey-Magni", c("Peter"), role = "ctb", comment = c(ORCID = "0000-0002-8942-9836")), + person(family = "Fonville", c("Judith", "M"), role = "ctb"), + person(family = "Friedrich", c("Alex", "W."), role = "ths", comment = c(ORCID = "0000-0003-4881-038X")), + person(family = "Glasner", c("Corinna"), role = "ths", comment = c(ORCID = "0000-0003-1241-1328")), + person(family = "Hazenberg", c("Eric", "H.", "L.", "C.", "M."), role = "ctb"), + person(family = "Knight", c("Gwen"), role = "ctb", comment = c(ORCID = "0000-0002-7263-9896")), + person(family = "Lenglet", c("Annick"), role = "ctb", comment = c(ORCID = "0000-0003-2013-8405")), + person(family = "Meijer", c("Bart", "C."), role = "ctb"), + person(family = "Mymrikov", c("Anton"), role = "ctb"), + person(family = "Ny", c("Sofia"), role = "ctb", comment = c(ORCID = "0000-0002-2017-1363")), + person(family = "Schade", c("Rogier", "P."), role = "ctb"), + person(family = "Sinha", c("Bhanu", "N.", "M."), role = "ths", comment = c(ORCID = "0000-0003-1634-0010")), + person(family = "Underwood", c("Anthony"), role = "ctb", comment = c(ORCID = "0000-0002-8547-4277"))) Depends: R (>= 3.0.0) Enhances: cleaner, skimr, ggplot2, + tibble, tidyselect Suggests: curl, @@ -41,7 +42,7 @@ Suggests: rvest, tinytest, xml2 -VignetteBuilder: knitr,rmarkdown +VignetteBuilder: knitr URL: https://msberends.github.io/AMR/, https://github.com/msberends/AMR BugReports: https://github.com/msberends/AMR/issues License: GPL-2 | file LICENSE diff --git a/NEWS.md b/NEWS.md index 2d3ee2b3..8e65073d 100755 --- a/NEWS.md +++ b/NEWS.md @@ -1,8 +1,10 @@ -# AMR 1.8.1.9030 +# AMR 1.8.1.9031 ### New * EUCAST 2022 and CLSI 2022 guidelines have been added for `as.rsi()`. EUCAST 2022 is now the new default guideline for all MIC and disks diffusion interpretations. -* Support for the following languages: Chinese, Greek, Japanese, Polish, Turkish and Ukrainian. The `AMR` package is now available in 16 languages. +* Support for `data.frame`-enhancing R packages, more specifically: `data.table`, `tibble`, and `tsibble`. AMR package functions that have a data set as output (such as `rsi_df()` and `bug_drug_combinations()`), will now return the same data type as the input. Furthermore, all our data sets are now in `tibble` format. +* Our data sets are now also continually exported to Apache Feather and Apache Parquet formats. You can find more info [in this article on our website](https://msberends.github.io/AMR/articles/datasets.html). +* Support for the following languages: Chinese, Greek, Japanese, Polish, Turkish and Ukrainian. We are very grateful for the valuable input by our colleagues from other countries. The `AMR` package is now available in 16 languages. ### Changed * Fix for using `as.rsi()` on certain EUCAST breakpoints for MIC values @@ -16,11 +18,12 @@ * Changed value in column `prevalence` of the `microorganisms` data set from 3 to 2 for these genera: *Acholeplasma*, *Alistipes*, *Alloprevotella*, *Bergeyella*, *Borrelia*, *Brachyspira*, *Butyricimonas*, *Cetobacterium*, *Chlamydia*, *Chlamydophila*, *Deinococcus*, *Dysgonomonas*, *Elizabethkingia*, *Empedobacter*, *Haloarcula*, *Halobacterium*, *Halococcus*, *Myroides*, *Odoribacter*, *Ornithobacterium*, *Parabacteroides*, *Pedobacter*, *Phocaeicola*, *Porphyromonas*, *Riemerella*, *Sphingobacterium*, *Streptobacillus*, *Tenacibaculum*, *Terrimonas*, *Victivallis*, *Wautersiella*, *Weeksella* * Fix for using the form `df[carbapenems() == "R", ]` using the latest `vctrs` package * Fix for using `info = FALSE` in `mdro()` +* All data sets in this package are now exported as `tibble`, instead of base R `data.frame`s. Older R versions are still supported. +* Automatic language determination will give a note once a session ### Other * New website to make use of the new Bootstrap 5 and pkgdown v2.0. The website now contains results for all examples and will be automatically regenerated with every change to our repository, using GitHub Actions * Added Peter Dutey-Magni and Anton Mymrikov as contributors, to thank them for their valuable input -* Our data sets are now also continually exported to Apache Feather and Apache Parquet formats * Set up Git Large File Storage (Git LFS) for the large SAS and SPSS file formats @@ -210,7 +213,7 @@ * Functions `oxazolidinones()` (an antibiotic selector function) and `filter_oxazolidinones()` (an antibiotic filter function) to select/filter on e.g. linezolid and tedizolid ```r library(dplyr) - x <- example_isolates %>% select(date, hospital_id, oxazolidinones()) + x <- example_isolates %>% select(date, ward, oxazolidinones()) #> Selecting oxazolidinones: column 'LNZ' (linezolid) x <- example_isolates %>% filter_oxazolidinones() @@ -295,7 +298,7 @@ ```r library(dplyr) example_isolates %>% - group_by(patient_id, hospital_id) %>% + group_by(patient_id, ward) %>% filter(is_new_episode(date, episode_days = 60)) ``` * Functions `mo_is_gram_negative()` and `mo_is_gram_positive()` as wrappers around `mo_gramstain()`. They always return `TRUE` or `FALSE` (except when the input is `NA` or the MO code is `UNKNOWN`), thus always return `FALSE` for species outside the taxonomic kingdom of Bacteria. @@ -909,7 +912,7 @@ This software is now out of beta and considered stable. Nonetheless, this packag boxplot() # grouped boxplots: septic_patients %>% - group_by(hospital_id) %>% + group_by(ward) %>% freq(age) %>% boxplot() ``` @@ -1162,13 +1165,13 @@ We've got a new website: [https://msberends.gitlab.io/AMR](https://msberends.git * Support for grouping variables, test with: ```r septic_patients %>% - group_by(hospital_id) %>% + group_by(ward) %>% freq(gender) ``` * Support for (un)selecting columns: ```r septic_patients %>% - freq(hospital_id) %>% + freq(ward) %>% select(-count, -cum_count) # only get item, percent, cum_percent ``` * Check for `hms::is.hms` diff --git a/R/aa_helper_functions.R b/R/aa_helper_functions.R index 86ecc08e..c1572ced 100755 --- a/R/aa_helper_functions.R +++ b/R/aa_helper_functions.R @@ -565,7 +565,7 @@ return_after_integrity_check <- function(value, type, check_vector) { value } -# transforms data set to data.frame with only ASCII values, to comply with CRAN policies +# transforms data set to a tibble with only ASCII values, to comply with CRAN policies dataset_UTF8_to_ASCII <- function(df) { trans <- function(vect) { iconv(vect, from = "UTF-8", to = "ASCII//TRANSLIT") @@ -587,7 +587,7 @@ dataset_UTF8_to_ASCII <- function(df) { df[, i] <- col } } - df + tibble::as_tibble(df) } # for eucast_rules() and mdro(), creates markdown output with URLs and names @@ -711,7 +711,7 @@ meet_criteria <- function(object, if (!is.null(pkg_env$meet_criteria_error_txt)) { error_txt <- pkg_env$meet_criteria_error_txt pkg_env$meet_criteria_error_txt <- NULL - stop(error_txt, call. = FALSE) # don't use stop_() here, pkg may not be loaded yet + stop(error_txt, call. = FALSE) # don't use stop_() here, our pkg may not be loaded yet } pkg_env$meet_criteria_error_txt <- NULL @@ -932,7 +932,7 @@ unique_call_id <- function(entire_session = FALSE, match_fn = NULL) { #' @param entire_session show message once per session message_not_thrown_before <- function(fn, ..., entire_session = FALSE) { # this is to prevent that messages/notes will be printed for every dplyr group or more than once per session - # e.g. this would show a msg 4 times: example_isolates %>% group_by(hospital_id) %>% filter(mo_is_gram_negative()) + # e.g. this would show a msg 4 times: example_isolates %>% group_by(ward) %>% filter(mo_is_gram_negative()) salt <- gsub("[^a-zA-Z0-9|_-]", "?", paste(c(...), sep = "|", collapse = "|"), perl = TRUE) not_thrown_before <- is.null(pkg_env[[paste0("thrown_msg.", fn, ".", salt)]]) || !identical(pkg_env[[paste0("thrown_msg.", fn, ".", salt)]], @@ -1153,6 +1153,19 @@ create_pillar_column <- function(x, ...) { new_pillar_shaft_simple(x, ...) } +as_original_data_class <- function(df, old_class = NULL) { + if ("tbl_df" %in% old_class && pkg_is_available("tibble", also_load = FALSE)) { + fn <- import_fn("as_tibble", "tibble") + } else if ("tbl_ts" %in% old_class && pkg_is_available("tsibble", also_load = FALSE)) { + fn <- import_fn("as_tsibble", "tsibble") + } else if ("data.table" %in% old_class && pkg_is_available("data.table", also_load = FALSE)) { + fn <- import_fn("as.data.table", "data.table") + } else { + fn <- base::as.data.frame + } + fn(df) +} + # copied from vctrs::s3_register by their permission: # https://github.com/r-lib/vctrs/blob/05968ce8e669f73213e3e894b5f4424af4f46316/R/register-s3.R s3_register <- function(generic, class, method = NULL) { diff --git a/R/ab.R b/R/ab.R index 0a19ee8d..9d7affd0 100755 --- a/R/ab.R +++ b/R/ab.R @@ -260,13 +260,13 @@ as.ab <- function(x, flag_multiple_results = TRUE, info = interactive(), ...) { } # try if name starts with it - found <- antibiotics[which(AB_lookup$generalised_name %like% paste0("^", x_spelling)), ]$ab + found <- antibiotics[which(AB_lookup$generalised_name %like% paste0("^", x_spelling)), "ab", drop = TRUE] if (length(found) > 0) { x_new[i] <- note_if_more_than_one_found(found, i, from_text) next } # try if name ends with it - found <- antibiotics[which(AB_lookup$generalised_name %like% paste0(x_spelling, "$")), ]$ab + found <- antibiotics[which(AB_lookup$generalised_name %like% paste0(x_spelling, "$")), "ab", drop = TRUE] if (nchar(x[i]) >= 4 & length(found) > 0) { x_new[i] <- note_if_more_than_one_found(found, i, from_text) next diff --git a/R/ab_property.R b/R/ab_property.R index 2c257493..2fe1dd93 100644 --- a/R/ab_property.R +++ b/R/ab_property.R @@ -429,10 +429,10 @@ ab_validate <- function(x, property, ...) { } else { # try to catch an error when inputting an invalid argument # so the 'call.' can be set to FALSE - tryCatch(x[1L] %in% antibiotics[1, property], + tryCatch(x[1L] %in% antibiotics[1, property, drop = TRUE], error = function(e) stop(e$message, call. = FALSE)) - if (!all(x %in% AB_lookup[, property])) { + if (!all(x %in% AB_lookup[, property, drop = TRUE])) { x <- as.ab(x, ...) x <- AB_lookup[match(x, AB_lookup$ab), property, drop = TRUE] } diff --git a/R/ab_selectors.R b/R/ab_selectors.R index 98de5044..ac56d66e 100644 --- a/R/ab_selectors.R +++ b/R/ab_selectors.R @@ -49,115 +49,147 @@ #' @examples #' # `example_isolates` is a data set available in the AMR package. #' # See ?example_isolates. -#' df <- example_isolates[ , c("hospital_id", "mo", -#' "AMP", "AMC", "TZP", "CXM", "CRO", "GEN", -#' "TOB", "COL", "IPM", "MEM", "TEC", "VAN")] +#' example_isolates #' #' # base R ------------------------------------------------------------------ #' #' # select columns 'IPM' (imipenem) and 'MEM' (meropenem) -#' df[, carbapenems()] +#' example_isolates[, carbapenems()] #' #' # select columns 'mo', 'AMK', 'GEN', 'KAN' and 'TOB' -#' df[, c("mo", aminoglycosides())] +#' example_isolates[, c("mo", aminoglycosides())] #' #' # select only antibiotic columns with DDDs for oral treatment -#' df[, administrable_per_os()] +#' example_isolates[, administrable_per_os()] #' #' # filter using any() or all() -#' df[any(carbapenems() == "R"), ] -#' subset(df, any(carbapenems() == "R")) +#' example_isolates[any(carbapenems() == "R"), ] +#' subset(example_isolates, any(carbapenems() == "R")) #' #' # filter on any or all results in the carbapenem columns (i.e., IPM, MEM): -#' df[any(carbapenems()), ] -#' df[all(carbapenems()), ] +#' example_isolates[any(carbapenems()), ] +#' example_isolates[all(carbapenems()), ] #' #' # filter with multiple antibiotic selectors using c() -#' df[all(c(carbapenems(), aminoglycosides()) == "R"), ] +#' example_isolates[all(c(carbapenems(), aminoglycosides()) == "R"), ] #' #' # filter + select in one go: get penicillins in carbapenems-resistant strains -#' df[any(carbapenems() == "R"), penicillins()] +#' example_isolates[any(carbapenems() == "R"), penicillins()] #' #' # You can combine selectors with '&' to be more specific. For example, #' # penicillins() would select benzylpenicillin ('peni G') and #' # administrable_per_os() would select erythromycin. Yet, when combined these #' # drugs are both omitted since benzylpenicillin is not administrable per os #' # and erythromycin is not a penicillin: -#' df[, penicillins() & administrable_per_os()] +#' example_isolates[, penicillins() & administrable_per_os()] #' #' # ab_selector() applies a filter in the `antibiotics` data set and is thus very #' # flexible. For instance, to select antibiotic columns with an oral DDD of at #' # least 1 gram: -#' df[, ab_selector(oral_ddd > 1 & oral_units == "g")] +#' example_isolates[, ab_selector(oral_ddd > 1 & oral_units == "g")] #' #' # dplyr ------------------------------------------------------------------- #' \donttest{ #' if (require("dplyr")) { #' -#' # get AMR for all aminoglycosides e.g., per hospital: -#' df %>% -#' group_by(hospital_id) %>% +#' # get AMR for all aminoglycosides e.g., per ward: +#' example_isolates %>% +#' group_by(ward) %>% #' summarise(across(aminoglycosides(), resistance)) #' +#' } +#' if (require("dplyr")) { +#' #' # You can combine selectors with '&' to be more specific: -#' df %>% +#' example_isolates %>% #' select(penicillins() & administrable_per_os()) +#' +#' } +#' if (require("dplyr")) { #' #' # get AMR for only drugs that matter - no intrinsic resistance: -#' df %>% +#' example_isolates %>% #' filter(mo_genus() %in% c("Escherichia", "Klebsiella")) %>% -#' group_by(hospital_id) %>% +#' group_by(ward) %>% #' summarise(across(not_intrinsic_resistant(), resistance)) #' +#' } +#' if (require("dplyr")) { +#' #' # get susceptibility for antibiotics whose name contains "trim": -#' df %>% +#' example_isolates %>% #' filter(first_isolate()) %>% -#' group_by(hospital_id) %>% +#' group_by(ward) %>% #' summarise(across(ab_selector(name %like% "trim"), susceptibility)) +#' +#' } +#' if (require("dplyr")) { #' #' # this will select columns 'IPM' (imipenem) and 'MEM' (meropenem): -#' df %>% +#' example_isolates %>% #' select(carbapenems()) #' +#' } +#' if (require("dplyr")) { +#' #' # this will select columns 'mo', 'AMK', 'GEN', 'KAN' and 'TOB': -#' df %>% +#' example_isolates %>% #' select(mo, aminoglycosides()) #' +#' } +#' if (require("dplyr")) { +#' #' # any() and all() work in dplyr's filter() too: -#' df %>% +#' example_isolates %>% #' filter(any(aminoglycosides() == "R"), #' all(cephalosporins_2nd() == "R")) #' +#' } +#' if (require("dplyr")) { +#' #' # also works with c(): -#' df %>% +#' example_isolates %>% #' filter(any(c(carbapenems(), aminoglycosides()) == "R")) #' +#' } +#' if (require("dplyr")) { +#' #' # not setting any/all will automatically apply all(): -#' df %>% +#' example_isolates %>% #' filter(aminoglycosides() == "R") #' +#' } +#' if (require("dplyr")) { +#' #' # this will select columns 'mo' and all antimycobacterial drugs ('RIF'): -#' df %>% +#' example_isolates %>% #' select(mo, ab_class("mycobact")) #' +#' } +#' if (require("dplyr")) { +#' #' # get bug/drug combinations for only glycopeptides in Gram-positives: -#' df %>% +#' example_isolates %>% #' filter(mo_is_gram_positive()) %>% #' select(mo, glycopeptides()) %>% #' bug_drug_combinations() %>% #' format() #' +#' } +#' if (require("dplyr")) { +#' #' data.frame(some_column = "some_value", #' J01CA01 = "S") %>% # ATC code of ampicillin #' select(penicillins()) # only the 'J01CA01' column will be selected #' -#' +#' } +#' if (require("dplyr")) { +#' #' # with recent versions of dplyr this is all equal: -#' x <- df[carbapenems() == "R", ] -#' y <- df %>% filter(carbapenems() == "R") -#' z <- df %>% filter(if_all(carbapenems(), ~.x == "R")) -#' identical(x, y) -#' identical(y, z) +#' x <- example_isolates[carbapenems() == "R", ] +#' y <- example_isolates %>% filter(carbapenems() == "R") +#' z <- example_isolates %>% filter(if_all(carbapenems(), ~.x == "R")) +#' identical(x, y) && identical(y, z) #' } #' } ab_class <- function(ab_class, diff --git a/R/age.R b/R/age.R index e9bfb424..3eb91d91 100755 --- a/R/age.R +++ b/R/age.R @@ -160,7 +160,7 @@ age <- function(x, reference = Sys.Date(), exact = FALSE, na.rm = FALSE, ...) { #' if (require("dplyr")) { #' example_isolates %>% #' filter_first_isolate() %>% -#' filter(mo == as.mo("E. coli")) %>% +#' filter(mo == as.mo("Escherichia coli")) %>% #' group_by(age_group = age_groups(age)) %>% #' select(age_group, CIP) %>% #' ggplot_rsi(x = "age_group", diff --git a/R/amr.R b/R/amr.R index a21c3266..822ad18e 100644 --- a/R/amr.R +++ b/R/amr.R @@ -52,13 +52,11 @@ #' - Principal component analysis for AMR #' #' @section Reference Data Publicly Available: -#' All reference data sets (about microorganisms, antibiotics, R/SI interpretation, EUCAST rules, etc.) in this `AMR` package are publicly and freely available. We continually export our data sets to formats for use in R, SPSS, SAS, Stata and Excel. We also supply flat files that are machine-readable and suitable for input in any software program, such as laboratory information systems. Please find [all download links on our website](https://msberends.github.io/AMR/articles/datasets.html), which is automatically updated with every code change. -#' @section Read more on Our Website!: -#' On our website you can find [a comprehensive tutorial](https://msberends.github.io/AMR/articles/AMR.html) about how to conduct AMR data analysis, the [complete documentation of all functions](https://msberends.github.io/AMR/reference/) and [an example analysis using WHONET data](https://msberends.github.io/AMR/articles/WHONET.html). +#' All data sets in this `AMR` package (about microorganisms, antibiotics, R/SI interpretation, EUCAST rules, etc.) are publicly and freely available for download in the following formats: R, MS Excel, Apache Feather, Apache Parquet, SPSS, SAS, and Stata. We also provide tab-separated plain text files that are machine-readable and suitable for input in any software program, such as laboratory information systems. Please visit [our website for the download links](https://msberends.github.io/AMR/articles/datasets.html). The actual files are of course available on [our GitHub repository](https://github.com/msberends/AMR/tree/main/data-raw). #' @section Contact Us: -#' For suggestions, comments or questions, please contact us at: +#' For suggestions, comments or questions, please contact us via: #' -#' Matthijs S. Berends \cr +#' Dr. Matthijs S. Berends \cr #' m.s.berends \[at\] umcg \[dot\] nl \cr #' University of Groningen #' Department of Medical Microbiology and Infection Prevention \cr diff --git a/R/atc_online.R b/R/atc_online.R index 7be614db..914e9774 100644 --- a/R/atc_online.R +++ b/R/atc_online.R @@ -180,15 +180,15 @@ atc_online_property <- function(atc_code, if (property %in% c("atc", "name")) { # ATC and name are only in first row - returnvalue[i] <- out[1, property] + returnvalue[i] <- out[1, property, drop = TRUE] } else { - if (!"adm.r" %in% colnames(out) | is.na(out[1, "adm.r"])) { + if (!"adm.r" %in% colnames(out) | is.na(out[1, "adm.r", drop = TRUE])) { returnvalue[i] <- NA next } else { for (j in seq_len(nrow(out))) { if (out[j, "adm.r"] == administration) { - returnvalue[i] <- out[j, property] + returnvalue[i] <- out[j, property, drop = TRUE] } } } diff --git a/R/availability.R b/R/availability.R index f4c56791..3ea9a8ed 100644 --- a/R/availability.R +++ b/R/availability.R @@ -36,7 +36,7 @@ #' \donttest{ #' if (require("dplyr")) { #' example_isolates %>% -#' filter(mo == as.mo("E. coli")) %>% +#' filter(mo == as.mo("Escherichia coli")) %>% #' select_if(is.rsi) %>% #' availability() #' } @@ -45,6 +45,8 @@ availability <- function(tbl, width = NULL) { meet_criteria(tbl, allow_class = "data.frame") meet_criteria(width, allow_class = c("numeric", "integer"), has_length = 1, allow_NULL = TRUE, is_positive = TRUE, is_finite = TRUE) + tbl <- as.data.frame(tbl, stringsAsFactors = FALSE) + x <- vapply(FUN.VALUE = double(1), tbl, function(x) { 1 - sum(is.na(x)) / length(x) }) @@ -87,7 +89,7 @@ availability <- function(tbl, width = NULL) { visual_resistance = vis_resistance, stringsAsFactors = FALSE) if (length(R[is.na(R)]) == ncol(tbl)) { - df[, 1:3] + df[, 1:3, drop = FALSE] } else { df } diff --git a/R/bug_drug_combinations.R b/R/bug_drug_combinations.R index fa049b51..35f598b0 100644 --- a/R/bug_drug_combinations.R +++ b/R/bug_drug_combinations.R @@ -51,7 +51,7 @@ #' FUN = mo_gramstain) #' #' bug_drug_combinations(example_isolates, -#' FUN = function(x) ifelse(x == as.mo("E. coli"), +#' FUN = function(x) ifelse(x == as.mo("Escherichia coli"), #' "E. coli", #' "Others")) #' } @@ -144,15 +144,12 @@ bug_drug_combinations <- function(x, if (data_has_groups) { out <- apply_group(x, "run_it", groups) - rownames(out) <- NULL - set_clean_class(out, - new_class = c("grouped", "bug_drug_combinations", "data.frame")) } else { out <- run_it(x) - rownames(out) <- NULL - set_clean_class(out, - new_class = c("bug_drug_combinations", "data.frame")) } + rownames(out) <- NULL + out <- as_original_data_class(out, class(x.bak)) + structure(out, class = c("bug_drug_combinations", ifelse(data_has_groups, "grouped", character(0)), class(out))) } #' @method format bug_drug_combinations @@ -180,18 +177,20 @@ format.bug_drug_combinations <- function(x, meet_criteria(decimal.mark, allow_class = "character", has_length = 1) meet_criteria(big.mark, allow_class = "character", has_length = 1) + x.bak <- x if (inherits(x, "grouped")) { # bug_drug_combinations() has been run on groups, so de-group here warning_("in `format()`: formatting the output of `bug_drug_combinations()` does not support grouped variables, they were ignored") + x <- as.data.frame(x, stringsAsFactors = FALSE) idx <- split(seq_len(nrow(x)), paste0(x$mo, "%%", x$ab)) x <- data.frame(mo = gsub("(.*)%%(.*)", "\\1", names(idx)), ab = gsub("(.*)%%(.*)", "\\2", names(idx)), - S = sapply(idx, function(i) sum(y$S[i], na.rm = TRUE)), - I = sapply(idx, function(i) sum(y$I[i], na.rm = TRUE)), - R = sapply(idx, function(i) sum(y$R[i], na.rm = TRUE)), - total = sapply(idx, function(i) sum(y$S[i], na.rm = TRUE) + - sum(y$I[i], na.rm = TRUE) + - sum(y$R[i], na.rm = TRUE)), + S = sapply(idx, function(i) sum(x$S[i], na.rm = TRUE)), + I = sapply(idx, function(i) sum(x$I[i], na.rm = TRUE)), + R = sapply(idx, function(i) sum(x$R[i], na.rm = TRUE)), + total = sapply(idx, function(i) sum(x$S[i], na.rm = TRUE) + + sum(x$I[i], na.rm = TRUE) + + sum(x$R[i], na.rm = TRUE)), stringsAsFactors = FALSE) } @@ -256,7 +255,7 @@ format.bug_drug_combinations <- function(x, # replace tidyr::pivot_wider() from here for (i in unique(y$mo)) { - mo_group <- y[which(y$mo == i), c("ab", "txt")] + mo_group <- y[which(y$mo == i), c("ab", "txt"), drop = FALSE] colnames(mo_group) <- c("ab", i) rownames(mo_group) <- NULL y <- y %pm>% @@ -269,7 +268,7 @@ format.bug_drug_combinations <- function(x, remove_NAs() select_ab_vars <- function(.data) { - .data[, c("ab_group", "ab_txt", colnames(.data)[!colnames(.data) %in% c("ab_group", "ab_txt", "ab")])] + .data[, c("ab_group", "ab_txt", colnames(.data)[!colnames(.data) %in% c("ab_group", "ab_txt", "ab")]), drop = FALSE] } y <- y %pm>% @@ -299,7 +298,7 @@ format.bug_drug_combinations <- function(x, } rownames(y) <- NULL - y + as_original_data_class(y, class(x.bak)) } #' @method print bug_drug_combinations diff --git a/R/catalogue_of_life.R b/R/catalogue_of_life.R index c7b348b3..c47da3a2 100755 --- a/R/catalogue_of_life.R +++ b/R/catalogue_of_life.R @@ -49,9 +49,9 @@ format_included_data_number <- function(data) { #' [Click here][catalogue_of_life] for more information about the included taxa. Check which versions of the CoL and LPSN were included in this package with [catalogue_of_life_version()]. #' @section Included Taxa: #' Included are: -#' - All `r format_included_data_number(microorganisms[which(microorganisms$kingdom %in% c("Archeae", "Bacteria", "Chromista", "Protozoa")), ])` (sub)species from the kingdoms of Archaea, Bacteria, Chromista and Protozoa -#' - All `r format_included_data_number(microorganisms[which(microorganisms$kingdom == "Fungi" & microorganisms$order %in% c("Eurotiales", "Microascales", "Mucorales", "Onygenales", "Pneumocystales", "Saccharomycetales", "Schizosaccharomycetales", "Tremellales")), ])` (sub)species from these orders of the kingdom of Fungi: Eurotiales, Microascales, Mucorales, Onygenales, Pneumocystales, Saccharomycetales, Schizosaccharomycetales and Tremellales, as well as `r format_included_data_number(microorganisms[which(microorganisms$kingdom == "Fungi" & !microorganisms$order %in% c("Eurotiales", "Microascales", "Mucorales", "Onygenales", "Pneumocystales", "Saccharomycetales", "Schizosaccharomycetales", "Tremellales")), ])` other fungal (sub)species. The kingdom of Fungi is a very large taxon with almost 300,000 different (sub)species, of which most are not microbial (but rather macroscopic, like mushrooms). Because of this, not all fungi fit the scope of this package and including everything would tremendously slow down our algorithms too. By only including the aforementioned taxonomic orders, the most relevant fungi are covered (such as all species of *Aspergillus*, *Candida*, *Cryptococcus*, *Histplasma*, *Pneumocystis*, *Saccharomyces* and *Trichophyton*). -#' - All `r format_included_data_number(microorganisms[which(microorganisms$kingdom == "Animalia"), ])` (sub)species from `r format_included_data_number(microorganisms[which(microorganisms$kingdom == "Animalia"), "genus"])` other relevant genera from the kingdom of Animalia (such as *Strongyloides* and *Taenia*) +#' - All `r format_included_data_number(microorganisms[which(microorganisms$kingdom %in% c("Archeae", "Bacteria", "Chromista", "Protozoa")), , drop = FALSE])` (sub)species from the kingdoms of Archaea, Bacteria, Chromista and Protozoa +#' - All `r format_included_data_number(microorganisms[which(microorganisms$kingdom == "Fungi" & microorganisms$order %in% c("Eurotiales", "Microascales", "Mucorales", "Onygenales", "Pneumocystales", "Saccharomycetales", "Schizosaccharomycetales", "Tremellales")), , drop = FALSE])` (sub)species from these orders of the kingdom of Fungi: Eurotiales, Microascales, Mucorales, Onygenales, Pneumocystales, Saccharomycetales, Schizosaccharomycetales and Tremellales, as well as `r format_included_data_number(microorganisms[which(microorganisms$kingdom == "Fungi" & !microorganisms$order %in% c("Eurotiales", "Microascales", "Mucorales", "Onygenales", "Pneumocystales", "Saccharomycetales", "Schizosaccharomycetales", "Tremellales")), ])` other fungal (sub)species. The kingdom of Fungi is a very large taxon with almost 300,000 different (sub)species, of which most are not microbial (but rather macroscopic, like mushrooms). Because of this, not all fungi fit the scope of this package and including everything would tremendously slow down our algorithms too. By only including the aforementioned taxonomic orders, the most relevant fungi are covered (such as all species of *Aspergillus*, *Candida*, *Cryptococcus*, *Histplasma*, *Pneumocystis*, *Saccharomyces* and *Trichophyton*). +#' - All `r format_included_data_number(microorganisms[which(microorganisms$kingdom == "Animalia"), , drop = FALSE])` (sub)species from `r format_included_data_number(microorganisms[which(microorganisms$kingdom == "Animalia"), "genus", drop = TRUE])` other relevant genera from the kingdom of Animalia (such as *Strongyloides* and *Taenia*) #' - All `r format_included_data_number(microorganisms.old)` previously accepted names of all included (sub)species (these were taxonomically renamed) #' - The complete taxonomic tree of all included (sub)species: from kingdom to subspecies #' - The responsible author(s) and year of scientific publication @@ -72,13 +72,13 @@ format_included_data_number <- function(data) { #' mo_shortname("Chlamydophila psittaci") #' #' # Get any property from the entire taxonomic tree for all included species -#' mo_class("E. coli") +#' mo_class("Escherichia coli") #' -#' mo_family("E. coli") +#' mo_family("Escherichia coli") #' -#' mo_gramstain("E. coli") # based on kingdom and phylum, see ?mo_gramstain +#' mo_gramstain("Escherichia coli") # based on kingdom and phylum, see ?mo_gramstain #' -#' mo_ref("E. coli") +#' mo_ref("Escherichia coli") #' #' # Do not get mistaken - this package is about microorganisms #' mo_kingdom("C. elegans") diff --git a/R/count.R b/R/count.R index 6f9051df..7fb3865f 100755 --- a/R/count.R +++ b/R/count.R @@ -75,7 +75,7 @@ #' \donttest{ #' if (require("dplyr")) { #' example_isolates %>% -#' group_by(hospital_id) %>% +#' group_by(ward) %>% #' summarise(R = count_R(CIP), #' I = count_I(CIP), #' S = count_S(CIP), @@ -86,7 +86,7 @@ #' # Number of available isolates for a whole antibiotic class #' # (i.e., in this data set columns GEN, TOB, AMK, KAN) #' example_isolates %>% -#' group_by(hospital_id) %>% +#' group_by(ward) %>% #' summarise(across(aminoglycosides(), n_rsi)) #' #' # Count co-resistance between amoxicillin/clav acid and gentamicin, @@ -108,8 +108,8 @@ #' #' # It also supports grouping variables #' example_isolates %>% -#' select(hospital_id, AMX, CIP) %>% -#' group_by(hospital_id) %>% +#' select(ward, AMX, CIP) %>% +#' group_by(ward) %>% #' count_df(translate = FALSE) #' } #' } diff --git a/R/custom_eucast_rules.R b/R/custom_eucast_rules.R index 9d37c107..b237094e 100644 --- a/R/custom_eucast_rules.R +++ b/R/custom_eucast_rules.R @@ -26,7 +26,7 @@ #' Define Custom EUCAST Rules #' #' Define custom EUCAST rules for your organisation or specific analysis and use the output of this function in [eucast_rules()]. -#' @param ... rules in [formula][`~`()] notation, see *Examples* +#' @param ... rules in [formula][base::tilde] notation, see *Examples* #' @details #' Some organisations have their own adoption of EUCAST rules. This function can be used to define custom EUCAST rules to be used in the [eucast_rules()] function. #' @section How it works: diff --git a/R/data.R b/R/data.R index 22911ba2..ae6dd4bc 100755 --- a/R/data.R +++ b/R/data.R @@ -27,7 +27,7 @@ #' #' Two data sets containing all antibiotics/antimycotics and antivirals. Use [as.ab()] or one of the [`ab_*`][ab_property()] functions to retrieve values from the [antibiotics] data set. Three identifiers are included in this data set: an antibiotic ID (`ab`, primarily used in this package) as defined by WHONET/EARS-Net, an ATC code (`atc`) as defined by the WHO, and a Compound ID (`cid`) as found in PubChem. Other properties in this data set are derived from one or more of these codes. Note that some drugs have multiple ATC codes. #' @format -#' ## For the [antibiotics] data set: a [data.frame] with `r nrow(antibiotics)` observations and `r ncol(antibiotics)` variables: +#' ## For the [antibiotics] data set: a [tibble[tibble::tibble] with `r nrow(antibiotics)` observations and `r ncol(antibiotics)` variables: #' - `ab`\cr Antibiotic ID as used in this package (such as `AMC`), using the official EARS-Net (European Antimicrobial Resistance Surveillance Network) codes where available #' - `cid`\cr Compound ID as found in PubChem #' - `name`\cr Official name as used by WHONET/EARS-Net or the WHO @@ -43,7 +43,7 @@ #' - `iv_units`\cr Units of `iv_ddd` #' - `loinc`\cr All LOINC codes (Logical Observation Identifiers Names and Codes) associated with the name of the antimicrobial agent. Use [ab_loinc()] to retrieve them quickly, see [ab_property()]. #' -#' ## For the [antivirals] data set: a [data.frame] with `r nrow(antivirals)` observations and `r ncol(antivirals)` variables: +#' ## For the [antivirals] data set: a [tibble[tibble::tibble] with `r nrow(antivirals)` observations and `r ncol(antivirals)` variables: #' - `atc`\cr ATC codes (Anatomical Therapeutic Chemical) as defined by the WHOCC #' - `cid`\cr Compound ID as found in PubChem #' - `name`\cr Official name as used by WHONET/EARS-Net or the WHO @@ -58,24 +58,15 @@ #' Synonyms (i.e. trade names) were derived from the Compound ID (`cid`) and consequently only available where a CID is available. #' #' ## Direct download -#' These data sets are available as 'flat files' for use even without \R - you can find the files here: -#' -#' * -#' * -#' -#' Files in \R format (with preserved data structure) can be found here: -#' -#' * -#' * +#' Like all data sets in this package, these data sets are publicly available for download in the following formats: R, MS Excel, Apache Feather, Apache Parquet, SPSS, SAS, and Stata. Please visit [our website for the download links](https://msberends.github.io/AMR/articles/datasets.html). The actual files are of course available on [our GitHub repository](https://github.com/msberends/AMR/tree/main/data-raw). #' @source World Health Organization (WHO) Collaborating Centre for Drug Statistics Methodology (WHOCC): #' #' European Commission Public Health PHARMACEUTICALS - COMMUNITY REGISTER: -#' @inheritSection AMR Reference Data Publicly Available #' @inheritSection WHOCC WHOCC #' @seealso [microorganisms], [intrinsic_resistant] #' @examples -#' head(antibiotics) -#' head(antivirals) +#' antibiotics +#' antivirals "antibiotics" #' @rdname antibiotics @@ -85,7 +76,7 @@ #' #' A data set containing the full microbial taxonomy (**last updated: `r CATALOGUE_OF_LIFE$yearmonth_LPSN`**) of `r nr2char(length(unique(microorganisms$kingdom[!microorganisms$kingdom %like% "unknown"])))` kingdoms from the Catalogue of Life (CoL) and the List of Prokaryotic names with Standing in Nomenclature (LPSN). MO codes can be looked up using [as.mo()]. #' @inheritSection catalogue_of_life Catalogue of Life -#' @format A [data.frame] with `r format(nrow(microorganisms), big.mark = ",")` observations and `r ncol(microorganisms)` variables: +#' @format A [tibble[tibble::tibble] with `r format(nrow(microorganisms), big.mark = ",")` observations and `r ncol(microorganisms)` variables: #' - `mo`\cr ID of microorganism as used by this package #' - `fullname`\cr Full name, like `"Escherichia coli"` #' - `kingdom`, `phylum`, `class`, `order`, `family`, `genus`, `species`, `subspecies`\cr Taxonomic rank of the microorganism @@ -114,10 +105,7 @@ #' - 6 families under the Enterobacterales order, according to Adeolu *et al.* (2016, PMID 27620848), that are not (yet) in the Catalogue of Life #' #' ## Direct download -#' This data set is available as 'flat file' for use even without \R - you can find the file here: . -#' -#' The file in \R format (with preserved data structure) can be found here: . -#' +#' Like all data sets in this package, this data set is publicly available for download in the following formats: R, MS Excel, Apache Feather, Apache Parquet, SPSS, SAS, and Stata. Please visit [our website for the download links](https://msberends.github.io/AMR/articles/datasets.html). The actual files are of course available on [our GitHub repository](https://github.com/msberends/AMR/tree/main/data-raw). #' @section About the Records from LPSN (see *Source*): #' The List of Prokaryotic names with Standing in Nomenclature (LPSN) provides comprehensive information on the nomenclature of prokaryotes. LPSN is a free to use service founded by Jean P. Euzeby in 1997 and later on maintained by Aidan C. Parte. #' @@ -137,80 +125,80 @@ #' `r SNOMED_VERSION$current_source` as currently implemented in this `AMR` package: #' #' * Retrieved from the `r SNOMED_VERSION$title`, OID `r SNOMED_VERSION$current_oid`, version `r SNOMED_VERSION$current_version`; url: <`r SNOMED_VERSION$url`> -#' @inheritSection AMR Reference Data Publicly Available #' @seealso [as.mo()], [mo_property()], [microorganisms.codes], [intrinsic_resistant] #' @examples -#' head(microorganisms) +#' microorganisms "microorganisms" #' Data Set with Previously Accepted Taxonomic Names #' #' A data set containing old (previously valid or accepted) taxonomic names according to the Catalogue of Life. This data set is used internally by [as.mo()]. #' @inheritSection catalogue_of_life Catalogue of Life -#' @format A [data.frame] with `r format(nrow(microorganisms.old), big.mark = ",")` observations and `r ncol(microorganisms.old)` variables: +#' @format A [tibble[tibble::tibble] with `r format(nrow(microorganisms.old), big.mark = ",")` observations and `r ncol(microorganisms.old)` variables: #' - `fullname`\cr Old full taxonomic name of the microorganism #' - `fullname_new`\cr New full taxonomic name of the microorganism #' - `ref`\cr Author(s) and year of concerning scientific publication #' - `prevalence`\cr Prevalence of the microorganism, see [as.mo()] +#' @details +#' Like all data sets in this package, this data set is publicly available for download in the following formats: R, MS Excel, Apache Feather, Apache Parquet, SPSS, SAS, and Stata. Please visit [our website for the download links](https://msberends.github.io/AMR/articles/datasets.html). The actual files are of course available on [our GitHub repository](https://github.com/msberends/AMR/tree/main/data-raw). #' @source Catalogue of Life: Annual Checklist (public online taxonomic database), (check included annual version with [catalogue_of_life_version()]). #' #' Parte, A.C. (2018). LPSN - List of Prokaryotic names with Standing in Nomenclature (bacterio.net), 20 years on. International Journal of Systematic and Evolutionary Microbiology, 68, 1825-1829; \doi{10.1099/ijsem.0.002786} -#' @inheritSection AMR Reference Data Publicly Available #' @seealso [as.mo()] [mo_property()] [microorganisms] #' @examples -#' head(microorganisms.old) +#' microorganisms.old "microorganisms.old" #' Data Set with `r format(nrow(microorganisms.codes), big.mark = ",")` Common Microorganism Codes #' #' A data set containing commonly used codes for microorganisms, from laboratory systems and WHONET. Define your own with [set_mo_source()]. They will all be searched when using [as.mo()] and consequently all the [`mo_*`][mo_property()] functions. -#' @format A [data.frame] with `r format(nrow(microorganisms.codes), big.mark = ",")` observations and `r ncol(microorganisms.codes)` variables: +#' @format A [tibble[tibble::tibble] with `r format(nrow(microorganisms.codes), big.mark = ",")` observations and `r ncol(microorganisms.codes)` variables: #' - `code`\cr Commonly used code of a microorganism #' - `mo`\cr ID of the microorganism in the [microorganisms] data set -#' @inheritSection AMR Reference Data Publicly Available +#' @details +#' Like all data sets in this package, this data set is publicly available for download in the following formats: R, MS Excel, Apache Feather, Apache Parquet, SPSS, SAS, and Stata. Please visit [our website for the download links](https://msberends.github.io/AMR/articles/datasets.html). The actual files are of course available on [our GitHub repository](https://github.com/msberends/AMR/tree/main/data-raw). #' @inheritSection catalogue_of_life Catalogue of Life #' @seealso [as.mo()] [microorganisms] #' @examples -#' head(microorganisms.codes) +#' microorganisms.codes "microorganisms.codes" #' Data Set with `r format(nrow(example_isolates), big.mark = ",")` Example Isolates #' -#' A data set containing `r format(nrow(example_isolates), big.mark = ",")` microbial isolates with their full antibiograms. The data set reflects reality and can be used to practice AMR data analysis. For examples, please read [the tutorial on our website](https://msberends.github.io/AMR/articles/AMR.html). -#' @format A [data.frame] with `r format(nrow(example_isolates), big.mark = ",")` observations and `r ncol(example_isolates)` variables: -#' - `date`\cr date of receipt at the laboratory -#' - `hospital_id`\cr ID of the hospital, from A to D -#' - `ward_icu`\cr [logical] to determine if ward is an intensive care unit -#' - `ward_clinical`\cr [logical] to determine if ward is a regular clinical ward -#' - `ward_outpatient`\cr [logical] to determine if ward is an outpatient clinic -#' - `age`\cr age of the patient -#' - `gender`\cr gender of the patient -#' - `patient_id`\cr ID of the patient -#' - `mo`\cr ID of microorganism created with [as.mo()], see also [microorganisms] -#' - `PEN:RIF`\cr `r sum(vapply(FUN.VALUE = logical(1), example_isolates, is.rsi))` different antibiotics with class [`rsi`] (see [as.rsi()]); these column names occur in the [antibiotics] data set and can be translated with [ab_name()] -#' @inheritSection AMR Reference Data Publicly Available +#' A data set containing `r format(nrow(example_isolates), big.mark = ",")` microbial isolates with their full antibiograms. This data set contains randomised fictitious data, but reflects reality and can be used to practise AMR data analysis. For examples, please read [the tutorial on our website](https://msberends.github.io/AMR/articles/AMR.html). +#' @format A [tibble[tibble::tibble] with `r format(nrow(example_isolates), big.mark = ",")` observations and `r ncol(example_isolates)` variables: +#' - `date`\cr Date of receipt at the laboratory +#' - `patient`\cr ID of the patient +#' - `age`\cr Age of the patient +#' - `gender`\cr Gender of the patient, either `r vector_or(example_isolates$gender)` +#' - `ward`\cr Ward type where the patient was admitted, either `r vector_or(example_isolates$ward)` +#' - `mo`\cr ID of microorganism created with [as.mo()], see also the [microorganisms] data set +#' - `PEN:RIF`\cr `r sum(vapply(FUN.VALUE = logical(1), example_isolates, is.rsi))` different antibiotics with class [`rsi`] (see [as.rsi()]); these column names occur in the [antibiotics] data set and can be translated with [set_ab_names()] or [ab_name()] +#' @details +#' Like all data sets in this package, this data set is publicly available for download in the following formats: R, MS Excel, Apache Feather, Apache Parquet, SPSS, SAS, and Stata. Please visit [our website for the download links](https://msberends.github.io/AMR/articles/datasets.html). The actual files are of course available on [our GitHub repository](https://github.com/msberends/AMR/tree/main/data-raw). #' @examples -#' head(example_isolates) +#' example_isolates "example_isolates" #' Data Set with Unclean Data #' #' A data set containing `r format(nrow(example_isolates_unclean), big.mark = ",")` microbial isolates that are not cleaned up and consequently not ready for AMR data analysis. This data set can be used for practice. -#' @format A [data.frame] with `r format(nrow(example_isolates_unclean), big.mark = ",")` observations and `r ncol(example_isolates_unclean)` variables: +#' @format A [tibble[tibble::tibble] with `r format(nrow(example_isolates_unclean), big.mark = ",")` observations and `r ncol(example_isolates_unclean)` variables: #' - `patient_id`\cr ID of the patient #' - `date`\cr date of receipt at the laboratory #' - `hospital`\cr ID of the hospital, from A to C #' - `bacteria`\cr info about microorganism that can be transformed with [as.mo()], see also [microorganisms] #' - `AMX:GEN`\cr 4 different antibiotics that have to be transformed with [as.rsi()] -#' @inheritSection AMR Reference Data Publicly Available +#' @details +#' Like all data sets in this package, this data set is publicly available for download in the following formats: R, MS Excel, Apache Feather, Apache Parquet, SPSS, SAS, and Stata. Please visit [our website for the download links](https://msberends.github.io/AMR/articles/datasets.html). The actual files are of course available on [our GitHub repository](https://github.com/msberends/AMR/tree/main/data-raw). #' @examples -#' head(example_isolates_unclean) +#' example_isolates_unclean "example_isolates_unclean" #' Data Set with `r format(nrow(WHONET), big.mark = ",")` Isolates - WHONET Example #' #' This example data set has the exact same structure as an export file from WHONET. Such files can be used with this package, as this example data set shows. The antibiotic results are from our [example_isolates] data set. All patient names are created using online surname generators and are only in place for practice purposes. -#' @format A [data.frame] with `r format(nrow(WHONET), big.mark = ",")` observations and `r ncol(WHONET)` variables: +#' @format A [tibble[tibble::tibble] with `r format(nrow(WHONET), big.mark = ",")` observations and `r ncol(WHONET)` variables: #' - `Identification number`\cr ID of the sample #' - `Specimen number`\cr ID of the specimen #' - `Organism`\cr Name of the microorganism. Before analysis, you should transform this to a valid microbial class, using [as.mo()]. @@ -237,15 +225,16 @@ #' - `Comment`\cr Other comments #' - `Date of data entry`\cr [Date] this data was entered in WHONET #' - `AMP_ND10:CIP_EE`\cr `r sum(vapply(FUN.VALUE = logical(1), WHONET, is.rsi))` different antibiotics. You can lookup the abbreviations in the [antibiotics] data set, or use e.g. [`ab_name("AMP")`][ab_name()] to get the official name immediately. Before analysis, you should transform this to a valid antibiotic class, using [as.rsi()]. -#' @inheritSection AMR Reference Data Publicly Available +#' @details +#' Like all data sets in this package, this data set is publicly available for download in the following formats: R, MS Excel, Apache Feather, Apache Parquet, SPSS, SAS, and Stata. Please visit [our website for the download links](https://msberends.github.io/AMR/articles/datasets.html). The actual files are of course available on [our GitHub repository](https://github.com/msberends/AMR/tree/main/data-raw). #' @examples -#' head(WHONET) +#' WHONET "WHONET" #' Data Set for R/SI Interpretation #' #' Data set containing reference data to interpret MIC and disk diffusion to R/SI values, according to international guidelines. Currently implemented guidelines are EUCAST (`r min(as.integer(gsub("[^0-9]", "", subset(rsi_translation, guideline %like% "EUCAST")$guideline)))`-`r max(as.integer(gsub("[^0-9]", "", subset(rsi_translation, guideline %like% "EUCAST")$guideline)))`) and CLSI (`r min(as.integer(gsub("[^0-9]", "", subset(rsi_translation, guideline %like% "CLSI")$guideline)))`-`r max(as.integer(gsub("[^0-9]", "", subset(rsi_translation, guideline %like% "CLSI")$guideline)))`). Use [as.rsi()] to transform MICs or disks measurements to R/SI values. -#' @format A [data.frame] with `r format(nrow(rsi_translation), big.mark = ",")` observations and `r ncol(rsi_translation)` variables: +#' @format A [tibble[tibble::tibble] with `r format(nrow(rsi_translation), big.mark = ",")` observations and `r ncol(rsi_translation)` variables: #' - `guideline`\cr Name of the guideline #' - `method`\cr Either `r vector_or(rsi_translation$method)` #' - `site`\cr Body site, e.g. "Oral" or "Respiratory" @@ -258,31 +247,35 @@ #' - `breakpoint_R`\cr Highest MIC value or lowest number of millimetres that leads to "R" #' - `uti`\cr A [logical] value (`TRUE`/`FALSE`) to indicate whether the rule applies to a urinary tract infection (UTI) #' @details -#' The repository of this `AMR` package contains a file comprising this exact data set: . This file **allows for machine reading EUCAST and CLSI guidelines**, which is almost impossible with the Excel and PDF files distributed by EUCAST and CLSI. The file is updated automatically and the `mo` and `ab` columns have been transformed to contain the full official names instead of codes. -#' @inheritSection AMR Reference Data Publicly Available +#' Like all data sets in this package, this data set is publicly available for download in the following formats: R, MS Excel, Apache Feather, Apache Parquet, SPSS, SAS, and Stata. Please visit [our website for the download links](https://msberends.github.io/AMR/articles/datasets.html). The actual files are of course available on [our GitHub repository](https://github.com/msberends/AMR/tree/main/data-raw). +#' +#' They **allow for machine reading EUCAST and CLSI guidelines**, which is almost impossible with the MS Excel and PDF files distributed by EUCAST and CLSI. #' @seealso [intrinsic_resistant] #' @examples -#' head(rsi_translation) +#' rsi_translation "rsi_translation" #' Data Set with Bacterial Intrinsic Resistance #' #' Data set containing defined intrinsic resistance by EUCAST of all bug-drug combinations. -#' @format A [data.frame] with `r format(nrow(intrinsic_resistant), big.mark = ",")` observations and `r ncol(intrinsic_resistant)` variables: +#' @format A [tibble[tibble::tibble] with `r format(nrow(intrinsic_resistant), big.mark = ",")` observations and `r ncol(intrinsic_resistant)` variables: #' - `mo`\cr Microorganism ID #' - `ab`\cr Antibiotic ID -#' @details The repository of this `AMR` package contains a file comprising this data set with full taxonomic and antibiotic names: . This file **allows for machine reading EUCAST guidelines about intrinsic resistance**, which is almost impossible with the Excel and PDF files distributed by EUCAST. The file is updated automatically. -#' +#' @details #' This data set is based on `r format_eucast_version_nr(3.3)`. -#' @inheritSection AMR Reference Data Publicly Available +#' +#' ## Direct download +#' Like all data sets in this package, this data set is publicly available for download in the following formats: R, MS Excel, Apache Feather, Apache Parquet, SPSS, SAS, and Stata. Please visit [our website for the download links](https://msberends.github.io/AMR/articles/datasets.html). The actual files are of course available on [our GitHub repository](https://github.com/msberends/AMR/tree/main/data-raw). +#' +#' They **allow for machine reading EUCAST and CLSI guidelines**, which is almost impossible with the MS Excel and PDF files distributed by EUCAST and CLSI. #' @examples -#' head(intrinsic_resistant) +#' intrinsic_resistant "intrinsic_resistant" #' Data Set with Treatment Dosages as Defined by EUCAST #' #' EUCAST breakpoints used in this package are based on the dosages in this data set. They can be retrieved with [eucast_dosage()]. -#' @format A [data.frame] with `r format(nrow(dosage), big.mark = ",")` observations and `r ncol(dosage)` variables: +#' @format A [tibble[tibble::tibble] with `r format(nrow(dosage), big.mark = ",")` observations and `r ncol(dosage)` variables: #' - `ab`\cr Antibiotic ID as used in this package (such as `AMC`), using the official EARS-Net (European Antimicrobial Resistance Surveillance Network) codes where available #' - `name`\cr Official name of the antimicrobial agent as used by WHONET/EARS-Net or the WHO #' - `type`\cr Type of the dosage, either `r vector_or(dosage$type)` @@ -292,8 +285,11 @@ #' - `notes`\cr Additional dosage notes #' - `original_txt`\cr Original text in the PDF file of EUCAST #' - `eucast_version`\cr Version number of the EUCAST Clinical Breakpoints guideline to which these dosages apply -#' @details `r format_eucast_version_nr(11.0)` are based on the dosages in this data set. -#' @inheritSection AMR Reference Data Publicly Available +#' @details +#' This data set is based on `r format_eucast_version_nr(11.0)`. +#' +#' ## Direct download +#' Like all data sets in this package, this data set is publicly available for download in the following formats: R, MS Excel, Apache Feather, Apache Parquet, SPSS, SAS, and Stata. Please visit [our website for the download links](https://msberends.github.io/AMR/articles/datasets.html). The actual files are of course available on [our GitHub repository](https://github.com/msberends/AMR/tree/main/data-raw). #' @examples -#' head(dosage) +#' dosage "dosage" diff --git a/R/episode.R b/R/episode.R index 1e524cf0..4a49438c 100644 --- a/R/episode.R +++ b/R/episode.R @@ -63,26 +63,34 @@ #' # episodes based on any (combination of) grouping variables: #' df %>% #' mutate(condition = sample(x = c("A", "B", "C"), -#' size = 2000, +#' size = 200, #' replace = TRUE)) %>% #' group_by(condition) %>% #' mutate(new_episode = is_new_episode(date, 365)) %>% -#' select(patient_id, date, condition, new_episode) +#' select(patient, date, condition, new_episode) +#' +#' } +#' if (require("dplyr")) { #' #' df %>% -#' group_by(hospital_id, patient_id) %>% +#' group_by(ward, patient) %>% #' transmute(date, -#' patient_id, +#' patient, #' new_index = get_episode(date, 60), #' new_logical = is_new_episode(date, 60)) +#' +#' } +#' if (require("dplyr")) { #' #' df %>% -#' group_by(hospital_id) %>% -#' summarise(n_patients = n_distinct(patient_id), +#' group_by(ward) %>% +#' summarise(n_patients = n_distinct(patient), #' n_episodes_365 = sum(is_new_episode(date, episode_days = 365)), #' n_episodes_60 = sum(is_new_episode(date, episode_days = 60)), #' n_episodes_30 = sum(is_new_episode(date, episode_days = 30))) #' +#' } +#' if (require("dplyr")) { #' #' # grouping on patients and microorganisms leads to the same #' # results as first_isolate() when using 'episode-based': @@ -91,16 +99,19 @@ #' method = "episode-based") #' #' y <- df %>% -#' group_by(patient_id, mo) %>% +#' group_by(patient, mo) %>% #' filter(is_new_episode(date, 365)) %>% #' ungroup() #' #' identical(x, y) +#' +#' } +#' if (require("dplyr")) { #' #' # but is_new_episode() has a lot more flexibility than first_isolate(), #' # since you can now group on anything that seems relevant: #' df %>% -#' group_by(patient_id, mo, hospital_id, ward_icu) %>% +#' group_by(patient, mo, ward) %>% #' mutate(flag_episode = is_new_episode(date, 365)) %>% #' select(group_vars(.), flag_episode) #' } diff --git a/R/eucast_rules.R b/R/eucast_rules.R index e2475b2c..63d85fdf 100755 --- a/R/eucast_rules.R +++ b/R/eucast_rules.R @@ -52,7 +52,7 @@ format_eucast_version_nr <- function(version, markdown = TRUE) { #' Apply rules for clinical breakpoints and intrinsic resistance as defined by the European Committee on Antimicrobial Susceptibility Testing (EUCAST, ), see *Source*. Use [eucast_dosage()] to get a [data.frame] with advised dosages of a certain bug-drug combination, which is based on the [dosage] data set. #' #' To improve the interpretation of the antibiogram before EUCAST rules are applied, some non-EUCAST rules can applied at default, see *Details*. -#' @param x data with antibiotic columns, such as `amox`, `AMX` and `AMC` +#' @param x a data set with antibiotic columns, such as `amox`, `AMX` and `AMC` #' @param info a [logical] to indicate whether progress should be printed to the console, defaults to only print while in interactive sessions #' @param rules a [character] vector that specifies which rules should be applied. Must be one or more of `"breakpoints"`, `"expert"`, `"other"`, `"custom"`, `"all"`, and defaults to `c("breakpoints", "expert")`. The default value can be set to another value, e.g. using `options(AMR_eucastrules = "all")`. If using `"custom"`, be sure to fill in argument `custom_rules` too. Custom rules can be created with [custom_eucast_rules()]. #' @param verbose a [logical] to turn Verbose mode on and off (default is off). In Verbose mode, the function does not apply rules to the data, but instead returns a data set in logbook form with extensive info about which rows and columns would be effected and in which way. Using Verbose mode takes a lot more time. @@ -412,11 +412,11 @@ eucast_rules <- function(x, } if (any(x$genus == "Staphylococcus", na.rm = TRUE)) { - all_staph <- MO_lookup[which(MO_lookup$genus == "Staphylococcus"), ] + all_staph <- MO_lookup[which(MO_lookup$genus == "Staphylococcus"), , drop = FALSE] all_staph$CNS_CPS <- suppressWarnings(mo_name(all_staph$mo, Becker = "all", language = NULL)) } if (any(x$genus == "Streptococcus", na.rm = TRUE)) { - all_strep <- MO_lookup[which(MO_lookup$genus == "Streptococcus"), ] + all_strep <- MO_lookup[which(MO_lookup$genus == "Streptococcus"), , drop = FALSE] all_strep$Lancefield <- suppressWarnings(mo_name(all_strep$mo, Lancefield = TRUE, language = NULL)) } @@ -432,7 +432,7 @@ eucast_rules <- function(x, font_red(paste0("v", utils::packageDescription("AMR")$Version, ", ", format(as.Date(utils::packageDescription("AMR")$Date), format = "%Y"))), "), see ?eucast_rules\n")))) } - ab_enzyme <- subset(antibiotics, name %like% "/")[, c("ab", "name")] + ab_enzyme <- subset(antibiotics, name %like% "/")[, c("ab", "name"), drop = FALSE] colnames(ab_enzyme) <- c("enzyme_ab", "enzyme_name") ab_enzyme$base_name <- gsub("^([a-zA-Z0-9]+).*", "\\1", ab_enzyme$enzyme_name) ab_enzyme$base_ab <- antibiotics[match(ab_enzyme$base_name, antibiotics$name), "ab", drop = TRUE] @@ -446,7 +446,7 @@ eucast_rules <- function(x, amox$base_name <- ab_name("AMX", language = NULL) # merge and sort ab_enzyme <- rbind(ab_enzyme, ampi, amox) - ab_enzyme <- ab_enzyme[order(ab_enzyme$enzyme_name), ] + ab_enzyme <- ab_enzyme[order(ab_enzyme$enzyme_name), , drop = FALSE] for (i in seq_len(nrow(ab_enzyme))) { # check if both base and base + enzyme inhibitor are part of the data set @@ -917,7 +917,7 @@ eucast_rules <- function(x, # Return data set --------------------------------------------------------- if (verbose == TRUE) { - verbose_info + as_original_data_class(verbose_info, old_attributes$class) } else { # x was analysed with only unique rows, so join everything together again x <- x[, c(cols_ab, ".rowid"), drop = FALSE] @@ -925,7 +925,7 @@ eucast_rules <- function(x, x.bak <- x.bak %pm>% pm_left_join(x, by = ".rowid") x.bak <- x.bak[, old_cols, drop = FALSE] - # reset original attributes + # reset original attributes, no need for as_original_data_class() here attributes(x.bak) <- old_attributes x.bak } @@ -1060,15 +1060,19 @@ eucast_dosage <- function(ab, administration = "iv", version_breakpoints = 11.0) lst[[i]] <- list(ab = "", name = "", standard_dosage = ifelse("standard_dosage" %in% df$type, - df[which(df$type == "standard_dosage"), ]$original_txt, + df[which(df$type == "standard_dosage"), "original_txt", drop = TRUE], NA_character_), high_dosage = ifelse("high_dosage" %in% df$type, - df[which(df$type == "high_dosage"), ]$original_txt, + df[which(df$type == "high_dosage"), "original_txt", drop = TRUE], NA_character_)) } out <- do.call("rbind", lapply(lst, as.data.frame, stringsAsFactors = FALSE)) rownames(out) <- NULL out$ab <- ab out$name <- ab_name(ab, language = NULL) - out + if (pkg_is_available("tibble", also_load = FALSE)) { + import_fn("as_tibble", "tibble")(out) + } else { + out + } } diff --git a/R/first_isolate.R b/R/first_isolate.R index 2bd1a974..4842c96a 100755 --- a/R/first_isolate.R +++ b/R/first_isolate.R @@ -32,7 +32,7 @@ #' @param col_mo column name of the IDs of the microorganisms (see [as.mo()]), defaults to the first column of class [`mo`]. Values will be coerced using [as.mo()]. #' @param col_testcode column name of the test codes. Use `col_testcode = NULL` to **not** exclude certain test codes (such as test codes for screening). In that case `testcodes_exclude` will be ignored. #' @param col_specimen column name of the specimen type or group -#' @param col_icu column name of the logicals (`TRUE`/`FALSE`) whether a ward or department is an Intensive Care Unit (ICU) +#' @param col_icu column name of the logicals (`TRUE`/`FALSE`) whether a ward or department is an Intensive Care Unit (ICU). This can also be a [logical] vector with the same length as rows in `x`. #' @param col_keyantimicrobials (only useful when `method = "phenotype-based"`) column name of the key antimicrobials to determine first isolates, see [key_antimicrobials()]. Defaults to the first column that starts with 'key' followed by 'ab' or 'antibiotics' or 'antimicrobials' (case insensitive). Use `col_keyantimicrobials = FALSE` to prevent this. Can also be the output of [key_antimicrobials()]. #' @param episode_days episode in days after which a genus/species combination will be determined as 'first isolate' again. The default of 365 days is based on the guideline by CLSI, see *Source*. #' @param testcodes_exclude a [character] vector with test codes that should be excluded (case-insensitive) @@ -119,7 +119,7 @@ #' @rdname first_isolate #' @seealso [key_antimicrobials()] #' @export -#' @return A [`logical`] vector +#' @return A [logical] vector #' @source Methodology of this function is strictly based on: #' #' - **M39 Analysis and Presentation of Cumulative Antimicrobial Susceptibility Test Data, 4th Edition**, 2014, *Clinical and Laboratory Standards Institute (CLSI)*. . @@ -138,36 +138,22 @@ #' # filter on first isolates using dplyr: #' example_isolates %>% #' filter(first_isolate()) +#' +#' } +#' if (require("dplyr")) { #' #' # short-hand version: #' example_isolates %>% #' filter_first_isolate(info = FALSE) #' +#' } +#' if (require("dplyr")) { +#' #' # flag the first isolates per group: #' example_isolates %>% -#' group_by(hospital_id) %>% +#' group_by(ward) %>% #' mutate(first = first_isolate()) %>% -#' select(hospital_id, date, patient_id, mo, first) -#' -#' # now let's see if first isolates matter: -#' A <- example_isolates %>% -#' group_by(hospital_id) %>% -#' summarise(count = n_rsi(GEN), # gentamicin availability -#' resistance = resistance(GEN)) # gentamicin resistance -#' -#' B <- example_isolates %>% -#' filter_first_isolate() %>% # the 1st isolate filter -#' group_by(hospital_id) %>% -#' summarise(count = n_rsi(GEN), # gentamicin availability -#' resistance = resistance(GEN)) # gentamicin resistance -#' -#' # Have a look at A and B. -#' A -#' B -#' -#' # B is more reliable because every isolate is counted only once. -#' # Gentamicin resistance in hospital D appears to be 4.2% higher than -#' # when you (erroneously) would have used all isolates for analysis. +#' select(ward, date, patient, mo, first) #' } #' } first_isolate <- function(x = NULL, @@ -217,7 +203,15 @@ first_isolate <- function(x = NULL, col_specimen <- NULL } meet_criteria(col_specimen, allow_class = "character", has_length = 1, allow_NULL = TRUE, is_in = colnames(x)) - meet_criteria(col_icu, allow_class = "character", has_length = 1, allow_NULL = TRUE, is_in = colnames(x)) + if (is.logical(col_icu)) { + meet_criteria(col_icu, allow_class = "logical", has_length = c(1, nrow(x)), allow_NULL = TRUE) + if (length(col_icu) == 1) { + col_icu <- rep(col_icu, nrow(x)) + } + } else { + meet_criteria(col_icu, allow_class = c("character", "logical"), has_length = 1, allow_NULL = TRUE, is_in = colnames(x)) + col_icu <- x[, col_icu, drop = TRUE] + } # method method <- coerce_method(method) meet_criteria(method, allow_class = "character", has_length = 1, is_in = c("phenotype-based", "episode-based", "patient-based", "isolate-based")) @@ -343,7 +337,6 @@ first_isolate <- function(x = NULL, check_columns_existance(col_patient_id) check_columns_existance(col_mo) check_columns_existance(col_testcode) - check_columns_existance(col_icu) check_columns_existance(col_keyantimicrobials) # convert dates to Date @@ -508,10 +501,10 @@ first_isolate <- function(x = NULL, } if (!is.null(col_icu)) { if (icu_exclude == TRUE) { - message_("Excluding isolates from ICU.", + message_("Excluding ", format(sum(!col_icu, na.rm = TRUE), big.mark = ","), " isolates from ICU.", add_fn = font_black, as_note = FALSE) - x[which(as.logical(x[, col_icu, drop = TRUE])), "newvar_first_isolate"] <- FALSE + x[which(col_icu), "newvar_first_isolate"] <- FALSE } else { message_("Including isolates from ICU.", add_fn = font_black, diff --git a/R/ggplot_rsi.R b/R/ggplot_rsi.R index 23abcb5d..8be36d2d 100755 --- a/R/ggplot_rsi.R +++ b/R/ggplot_rsi.R @@ -66,11 +66,14 @@ #' @export #' @examples #' \donttest{ -#' if (require("ggplot2") & require("dplyr")) { +#' if (require("ggplot2") && require("dplyr")) { #' #' # get antimicrobial results for drugs against a UTI: #' ggplot(example_isolates %>% select(AMX, NIT, FOS, TMP, CIP)) + #' geom_rsi() +#' +#' } +#' if (require("ggplot2") && require("dplyr")) { #' #' # prettify the plot using some additional functions: #' df <- example_isolates %>% select(AMX, NIT, FOS, TMP, CIP) @@ -80,16 +83,25 @@ #' scale_rsi_colours() + #' labels_rsi_count() + #' theme_rsi() +#' +#' } +#' if (require("ggplot2") && require("dplyr")) { #' #' # or better yet, simplify this using the wrapper function - a single command: #' example_isolates %>% #' select(AMX, NIT, FOS, TMP, CIP) %>% #' ggplot_rsi() +#' +#' } +#' if (require("ggplot2") && require("dplyr")) { #' #' # get only proportions and no counts: #' example_isolates %>% #' select(AMX, NIT, FOS, TMP, CIP) %>% #' ggplot_rsi(datalabels = FALSE) +#' +#' } +#' if (require("ggplot2") && require("dplyr")) { #' #' # add other ggplot2 arguments as you like: #' example_isolates %>% @@ -99,11 +111,17 @@ #' size = 1, #' linetype = 2, #' alpha = 0.25) +#' +#' } +#' if (require("ggplot2") && require("dplyr")) { #' #' # you can alter the colours with colour names: #' example_isolates %>% #' select(AMX) %>% #' ggplot_rsi(colours = c(SI = "yellow")) +#' +#' } +#' if (require("ggplot2") && require("dplyr")) { #' #' # but you can also use the built-in colour-blind friendly colours for #' # your plots, where "S" is green, "I" is yellow and "R" is red: @@ -113,34 +131,42 @@ #' ggplot() + #' geom_col(aes(x = x, y = y, fill = z)) + #' scale_rsi_colours(Value4 = "S", Value5 = "I", Value6 = "R") +#' +#' } +#' if (require("ggplot2") && require("dplyr")) { #' #' # resistance of ciprofloxacine per age group #' example_isolates %>% #' mutate(first_isolate = first_isolate()) %>% #' filter(first_isolate == TRUE, -#' mo == as.mo("E. coli")) %>% +#' mo == as.mo("Escherichia coli")) %>% #' # age_groups() is also a function in this AMR package: #' group_by(age_group = age_groups(age)) %>% #' select(age_group, CIP) %>% #' ggplot_rsi(x = "age_group") +#' +#' } +#' if (require("ggplot2") && require("dplyr")) { #' #' # a shorter version which also adjusts data label colours: #' example_isolates %>% #' select(AMX, NIT, FOS, TMP, CIP) %>% #' ggplot_rsi(colours = FALSE) -#' +#' +#' } +#' if (require("ggplot2") && require("dplyr")) { #' #' # it also supports groups (don't forget to use the group var on `x` or `facet`): #' example_isolates %>% -#' filter(mo_is_gram_negative()) %>% +#' filter(mo_is_gram_negative(), ward != "Outpatient") %>% #' # select only UTI-specific drugs -#' select(hospital_id, AMX, NIT, FOS, TMP, CIP) %>% -#' group_by(hospital_id) %>% -#' ggplot_rsi(x = "hospital_id", +#' select(ward, AMX, NIT, FOS, TMP, CIP) %>% +#' group_by(ward) %>% +#' ggplot_rsi(x = "ward", #' facet = "antibiotic", #' nrow = 1, -#' title = "AMR of Anti-UTI Drugs Per Hospital", -#' x.title = "Hospital", +#' title = "AMR of Anti-UTI Drugs Per Ward", +#' x.title = "Ward", #' datalabels = FALSE) #' } #' } diff --git a/R/join_microorganisms.R b/R/join_microorganisms.R index 553d1584..47448d11 100755 --- a/R/join_microorganisms.R +++ b/R/join_microorganisms.R @@ -42,21 +42,22 @@ #' left_join_microorganisms(as.mo("K. pneumoniae")) #' left_join_microorganisms("B_KLBSL_PNMN") #' +#' df <- data.frame(date = seq(from = as.Date("2018-01-01"), +#' to = as.Date("2018-01-07"), +#' by = 1), +#' bacteria = as.mo(c("S. aureus", "MRSA", "MSSA", "STAAUR", +#' "E. coli", "E. coli", "E. coli")), +#' stringsAsFactors = FALSE) +#' colnames(df) +#' +#' df_joined <- left_join_microorganisms(df, "bacteria") +#' colnames(df_joined) +#' #' \donttest{ #' if (require("dplyr")) { #' example_isolates %>% #' left_join_microorganisms() %>% #' colnames() -#' -#' df <- data.frame(date = seq(from = as.Date("2018-01-01"), -#' to = as.Date("2018-01-07"), -#' by = 1), -#' bacteria = as.mo(c("S. aureus", "MRSA", "MSSA", "STAAUR", -#' "E. coli", "E. coli", "E. coli")), -#' stringsAsFactors = FALSE) -#' colnames(df) -#' df_joined <- left_join_microorganisms(df, "bacteria") -#' colnames(df_joined) #' } #' } inner_join_microorganisms <- function(x, by = NULL, suffix = c("2", ""), ...) { @@ -119,9 +120,14 @@ join_microorganisms <- function(type, x, by, suffix, ...) { check_dataset_integrity() if (!is.data.frame(x)) { - x <- data.frame(mo = x, stringsAsFactors = FALSE) + if (pkg_is_available("tibble", also_load = FALSE)) { + x <- import_fn("tibble", "tibble")(mo = x) + } else { + x <- data.frame(mo = x, stringsAsFactors = FALSE) + } by <- "mo" } + x.bak <- x if (is.null(by)) { by <- search_type_in_df(x, "mo", info = FALSE) if (is.null(by) && NCOL(x) == 1) { @@ -171,5 +177,5 @@ join_microorganisms <- function(type, x, by, suffix, ...) { warning_("in `", type, "_join()`: the newly joined data set contains ", nrow(joined) - nrow(x), " rows more than the number of rows of `x`.") } - joined + as_original_data_class(joined, class(x.bak)) } diff --git a/R/like.R b/R/like.R index 8029bfdd..7cb4021f 100755 --- a/R/like.R +++ b/R/like.R @@ -59,11 +59,9 @@ #' a[1] %like% b #' a %like% b[1] #' -#' # get isolates whose name start with 'Ent' or 'ent' -#' example_isolates[which(mo_name(example_isolates$mo) %like% "^ent"), ] #' \donttest{ -#' # faster way, since mo_name() is context-aware: -#' example_isolates[which(mo_name() %like% "^ent"), ] +#' # get isolates whose name start with 'Entero' (case-insensitive) +#' example_isolates[which(mo_name() %like% "^entero"), ] #' #' if (require("dplyr")) { #' example_isolates %>% diff --git a/R/mdro.R b/R/mdro.R index ad419d46..0ae413ae 100755 --- a/R/mdro.R +++ b/R/mdro.R @@ -1740,7 +1740,7 @@ run_custom_mdro_guideline <- function(df, guideline, info) { out <- factor(out, levels = attributes(guideline)$values, ordered = TRUE) } - columns_nonsusceptible <- as.data.frame(t(df[, is.rsi(df)] == "R")) + columns_nonsusceptible <- as.data.frame(t(df[, is.rsi(df), drop = FALSE] == "R")) columns_nonsusceptible <- vapply(FUN.VALUE = character(1), columns_nonsusceptible, function(x) paste0(rownames(columns_nonsusceptible)[which(x)], collapse = " ")) diff --git a/R/mic.R b/R/mic.R index c629561f..6ffadfd2 100755 --- a/R/mic.R +++ b/R/mic.R @@ -120,9 +120,16 @@ valid_mic_levels <- c(c(t(vapply(FUN.VALUE = character(9), ops, #' # plot MIC values, see ?plot #' plot(mic_data) #' plot(mic_data, mo = "E. coli", ab = "cipro") -#' autoplot(mic_data, mo = "E. coli", ab = "cipro") -#' autoplot(mic_data, mo = "E. coli", ab = "cipro", language = "nl") # Dutch -#' autoplot(mic_data, mo = "E. coli", ab = "cipro", language = "uk") # Ukrainian +#' +#' if (require("ggplot2")) { +#' autoplot(mic_data, mo = "E. coli", ab = "cipro") +#' } +#' if (require("ggplot2")) { +#' autoplot(mic_data, mo = "E. coli", ab = "cipro", language = "nl") # Dutch +#' } +#' if (require("ggplot2")) { +#' autoplot(mic_data, mo = "E. coli", ab = "cipro", language = "uk") # Ukrainian +#' } as.mic <- function(x, na.rm = FALSE) { meet_criteria(x, allow_class = c("mic", "character", "numeric", "integer", "factor"), allow_NA = TRUE) meet_criteria(na.rm, allow_class = "logical", has_length = 1) diff --git a/R/mo.R b/R/mo.R index db6fd454..cd4005ff 100755 --- a/R/mo.R +++ b/R/mo.R @@ -419,7 +419,7 @@ exec_as.mo <- function(x, return(rep(NA_character_, length(x_input))) } - } else if (all(x %in% reference_df[, 1][[1]])) { + } else if (all(x %in% reference_df[, 1, drop = TRUE][[1]])) { # all in reference df colnames(reference_df)[1] <- "x" suppressWarnings( @@ -449,7 +449,7 @@ exec_as.mo <- function(x, property, drop = TRUE] - } else if (!all(x %in% microorganisms[, property])) { + } else if (!all(x %in% microorganisms[, property, drop = TRUE])) { strip_whitespace <- function(x, dyslexia_mode) { # all whitespaces (tab, new lines, etc.) should be one space @@ -669,7 +669,7 @@ exec_as.mo <- function(x, FALSE })) if (sum(snomed_found, na.rm = TRUE) > 0) { - found <- reference_data_to_use[snomed_found == TRUE, property][[1]] + found <- reference_data_to_use[snomed_found == TRUE, property, drop = TRUE][[1]] if (!is.na(found)) { x[i] <- found[1L] next @@ -694,9 +694,9 @@ exec_as.mo <- function(x, # user-defined reference ---- if (!is.null(reference_df)) { - if (x_backup[i] %in% reference_df[, 1]) { + if (x_backup[i] %in% reference_df[, 1, drop = TRUE]) { # already checked integrity of reference_df, all MOs are valid - ref_mo <- reference_df[reference_df[, 1] == x_backup[i], "mo"][[1L]] + ref_mo <- reference_df[reference_df[, 1, drop = TRUE] == x_backup[i], "mo", drop = TRUE][[1L]] x[i] <- lookup(mo == ref_mo) next } @@ -1555,11 +1555,11 @@ exec_as.mo <- function(x, # nolint start # comment below code if all staphylococcal species are categorised as CoNS/CoPS - if (any(x %in% MO_lookup[which(MO_lookup$species %in% post_Becker), property])) { + if (any(x %in% MO_lookup[which(MO_lookup$species %in% post_Becker), property, drop = TRUE])) { if (message_not_thrown_before("as.mo", "becker")) { warning_("in `as.mo()`: Becker ", font_italic("et al."), " (2014, 2019, 2020) does not contain these species named after their publication: ", font_italic(paste("S.", - sort(mo_species(unique(x[x %in% MO_lookup[which(MO_lookup$species %in% post_Becker), property]]))), + sort(mo_species(unique(x[x %in% MO_lookup[which(MO_lookup$species %in% post_Becker), property, drop = TRUE]]))), collapse = ", ")), ". Categorisation to CoNS/CoPS was taken from the original scientific publication(s).", immediate = TRUE) @@ -1820,7 +1820,7 @@ summary.mo <- function(object, ...) { # unique and top 1-3 x <- as.mo(object) # force again, could be mo from older pkg version top <- as.data.frame(table(x), responseName = "n", stringsAsFactors = FALSE) - top_3 <- top[order(-top$n), 1][1:3] + top_3 <- top[order(-top$n), 1, drop = TRUE][1:3] value <- c("Class" = "mo", "" = length(x[is.na(x)]), "Unique" = pm_n_distinct(x[!is.na(x)]), @@ -2206,282 +2206,3 @@ strip_words <- function(text, n, side = "right") { }) vapply(FUN.VALUE = character(1), out, paste, collapse = " ") } - - -as.mo2 <- function(x, - Becker = FALSE, - Lancefield = FALSE, - allow_uncertain = TRUE, - reference_df = get_mo_source(), - info = interactive(), - property = "mo", - initial_search = TRUE, - dyslexia_mode = FALSE, - debug = FALSE, - ignore_pattern = getOption("AMR_ignore_pattern"), - reference_data_to_use = MO_lookup, - actual_uncertainty = 1, - actual_input = NULL, - language = get_AMR_locale()) { - meet_criteria(x, allow_class = c("mo", "data.frame", "list", "character", "numeric", "integer", "factor"), allow_NA = TRUE) - meet_criteria(Becker, allow_class = c("logical", "character"), has_length = 1) - meet_criteria(Lancefield, allow_class = c("logical", "character"), has_length = 1) - meet_criteria(allow_uncertain, allow_class = c("logical", "numeric", "integer"), has_length = 1) - meet_criteria(reference_df, allow_class = "data.frame", allow_NULL = TRUE) - meet_criteria(property, allow_class = "character", has_length = 1, is_in = colnames(microorganisms)) - meet_criteria(initial_search, allow_class = "logical", has_length = 1) - meet_criteria(dyslexia_mode, allow_class = "logical", has_length = 1) - meet_criteria(debug, allow_class = "logical", has_length = 1) - meet_criteria(ignore_pattern, allow_class = "character", has_length = 1, allow_NULL = TRUE) - meet_criteria(reference_data_to_use, allow_class = "data.frame") - meet_criteria(actual_uncertainty, allow_class = "numeric", has_length = 1) - meet_criteria(actual_input, allow_class = "character", allow_NULL = TRUE) - meet_criteria(language, has_length = 1, is_in = c(LANGUAGES_SUPPORTED, ""), allow_NULL = TRUE, allow_NA = TRUE) - - check_dataset_integrity() - - if (isTRUE(debug) && initial_search == TRUE) { - time_start_tracking() - } - - lookup <- function(needle, - column = property, - haystack = reference_data_to_use, - n = 1, - debug_mode = debug, - initial = initial_search, - uncertainty = actual_uncertainty, - input_actual = actual_input) { - - if (!is.null(input_actual)) { - input <- input_actual - } else { - input <- tryCatch(x_backup[i], error = function(e) "") - } - - # `column` can be NULL for all columns, or a selection - # returns a [character] (vector) - if `column` > length 1 then with columns as names - if (isTRUE(debug_mode)) { - cat(font_silver("Looking up: ", substitute(needle), collapse = ""), - "\n ", time_track()) - } - if (length(column) == 1) { - res_df <- haystack[which(eval(substitute(needle), envir = haystack, enclos = parent.frame())), , drop = FALSE] - if (NROW(res_df) > 1 & uncertainty != -1) { - # sort the findings on matching score - scores <- mo_matching_score(x = input, - n = res_df[, "fullname", drop = TRUE]) - res_df <- res_df[order(scores, decreasing = TRUE), , drop = FALSE] - } - res <- as.character(res_df[, column, drop = TRUE]) - if (length(res) == 0) { - if (isTRUE(debug_mode)) { - cat(font_red(" (no match)\n")) - } - NA_character_ - } else { - if (isTRUE(debug_mode)) { - cat(font_green(paste0(" MATCH (", NROW(res_df), " results)\n"))) - } - if ((length(res) > n | uncertainty > 1) & uncertainty != -1) { - # save the other possible results as well, but not for forced certain results (then uncertainty == -1) - uncertainties <<- rbind(uncertainties, - format_uncertainty_as_df(uncertainty_level = uncertainty, - input = input, - result_mo = res_df[1, "mo", drop = TRUE], - candidates = as.character(res_df[, "fullname", drop = TRUE])), - stringsAsFactors = FALSE) - } - res[seq_len(min(n, length(res)))] - } - } else { - if (is.null(column)) { - column <- names(haystack) - } - res <- haystack[which(eval(substitute(needle), envir = haystack, enclos = parent.frame())), , drop = FALSE] - res <- res[seq_len(min(n, nrow(res))), column, drop = TRUE] - if (NROW(res) == 0) { - if (isTRUE(debug_mode)) { - cat(font_red(" (no rows)\n")) - } - res <- rep(NA_character_, length(column)) - } else { - if (isTRUE(debug_mode)) { - cat(font_green(paste0(" MATCH (", NROW(res), " rows)\n"))) - } - } - res <- as.character(res) - names(res) <- column - res - } - } - - # start off with replaced language-specific non-ASCII characters with ASCII characters - x <- parse_and_convert(x) - # replace mo codes used in older package versions - x <- replace_old_mo_codes(x, property) - # ignore cases that match the ignore pattern - x <- replace_ignore_pattern(x, ignore_pattern) - - # WHONET: xxx = no growth - x[tolower(as.character(paste0(x, ""))) %in% c("", "xxx", "na", "nan")] <- NA_character_ - # Laboratory systems: remove (translated) entries like "no growth", etc. - x[trimws2(x) %like% translate_into_language("no .*growth", language = language)] <- NA_character_ - x[trimws2(x) %like% paste0("^(", translate_into_language("no|not", language = language), ") [a-z]+")] <- "UNKNOWN" - - if (initial_search == TRUE) { - # keep track of time - give some hints to improve speed if it takes a long time - start_time <- Sys.time() - - pkg_env$mo_failures <- NULL - pkg_env$mo_uncertainties <- NULL - pkg_env$mo_renamed <- NULL - } - pkg_env$mo_renamed_last_run <- NULL - - failures <- character(0) - uncertainty_level <- translate_allow_uncertain(allow_uncertain) - uncertainties <- data.frame(uncertainty = integer(0), - input = character(0), - fullname = character(0), - renamed_to = character(0), - mo = character(0), - candidates = character(0), - stringsAsFactors = FALSE) - - x_input <- x - # already strip leading and trailing spaces - x <- trimws(x) - # only check the uniques, which is way faster - x <- unique(x) - # remove empty values (to later fill them in again with NAs) - # ("xxx" is WHONET code for 'no growth') - x <- x[!is.na(x) - & !is.null(x) - & !identical(x, "") - & !identical(x, "xxx")] - - # defined df to check for - if (!is.null(reference_df)) { - check_validity_mo_source(reference_df) - reference_df <- repair_reference_df(reference_df) - } - - # all empty - if (all(identical(trimws(x_input), "") | is.na(x_input) | length(x) == 0)) { - if (property == "mo") { - return(set_clean_class(rep(NA_character_, length(x_input)), - new_class = c("mo", "character"))) - } else { - return(rep(NA_character_, length(x_input))) - } - - } else if (all(x %in% reference_df[, 1][[1]])) { - # all in reference df - colnames(reference_df)[1] <- "x" - suppressWarnings( - x <- MO_lookup[match(reference_df[match(x, reference_df$x), "mo", drop = TRUE], MO_lookup$mo), property, drop = TRUE] - ) - - } else if (all(x %in% reference_data_to_use$mo)) { - x <- MO_lookup[match(x, MO_lookup$mo), property, drop = TRUE] - - } else if (all(tolower(x) %in% reference_data_to_use$fullname_lower)) { - # we need special treatment for very prevalent full names, they are likely! - # e.g. as.mo("Staphylococcus aureus") - x <- MO_lookup[match(tolower(x), MO_lookup$fullname_lower), property, drop = TRUE] - - } else if (all(x %in% reference_data_to_use$fullname)) { - # we need special treatment for very prevalent full names, they are likely! - # e.g. as.mo("Staphylococcus aureus") - x <- MO_lookup[match(x, MO_lookup$fullname), property, drop = TRUE] - - } else if (all(toupper(x) %in% microorganisms.codes$code)) { - # commonly used MO codes - x <- MO_lookup[match(microorganisms.codes[match(toupper(x), - microorganisms.codes$code), - "mo", - drop = TRUE], - MO_lookup$mo), - property, - drop = TRUE] - - } else if (!all(x %in% microorganisms[, property])) { - - strip_whitespace <- function(x, dyslexia_mode) { - # all whitespaces (tab, new lines, etc.) should be one space - # and spaces before and after should be left blank - trimmed <- trimws2(x) - # also, make sure the trailing and leading characters are a-z or 0-9 - # in case of non-regex - if (dyslexia_mode == FALSE) { - trimmed <- gsub("^[^a-zA-Z0-9)(]+", "", trimmed, perl = TRUE) - trimmed <- gsub("[^a-zA-Z0-9)(]+$", "", trimmed, perl = TRUE) - } - trimmed - } - - x_backup_untouched <- x - x <- strip_whitespace(x, dyslexia_mode) - # translate 'unknown' names back to English - if (any(tolower(x) %like_case% "unbekannt|onbekend|desconocid|sconosciut|iconnu|desconhecid", na.rm = TRUE)) { - trns <- subset(TRANSLATIONS, pattern %like% "unknown") - langs <- LANGUAGES_SUPPORTED[LANGUAGES_SUPPORTED != "en"] - for (l in langs) { - for (i in seq_len(nrow(trns))) { - if (!is.na(trns[i, l, drop = TRUE])) { - x <- gsub(pattern = trns[i, l, drop = TRUE], - replacement = trns$pattern[i], - x = x, - ignore.case = TRUE, - perl = TRUE) - } - } - } - } - - # remove spp and species - x <- gsub("(^| )[ .]*(spp|ssp|ss|sp|subsp|subspecies|biovar|biotype|serovar|species)[ .]*( |$)", "", x, ignore.case = TRUE, perl = TRUE) - x <- strip_whitespace(x, dyslexia_mode) - - x_backup <- x - - # from here on case-insensitive - x <- tolower(x) - - x_backup[x %like_case% "^(fungus|fungi)$"] <- "(unknown fungus)" # will otherwise become the kingdom - x_backup[x_backup_untouched == "Fungi"] <- "Fungi" # is literally the kingdom - - # Fill in fullnames and MO codes directly - known_names <- tolower(x_backup) %in% MO_lookup$fullname_lower - x[known_names] <- MO_lookup[match(tolower(x_backup)[known_names], MO_lookup$fullname_lower), property, drop = TRUE] - known_codes_mo <- toupper(x_backup) %in% MO_lookup$mo - x[known_codes_mo] <- MO_lookup[match(toupper(x_backup)[known_codes_mo], MO_lookup$mo), property, drop = TRUE] - known_codes_lis <- toupper(x_backup) %in% microorganisms.codes$code - x[known_codes_lis] <- MO_lookup[match(microorganisms.codes[match(toupper(x_backup)[known_codes_lis], - microorganisms.codes$code), "mo", drop = TRUE], - MO_lookup$mo), property, drop = TRUE] - already_known <- known_names | known_codes_mo | known_codes_lis - - # now only continue where the right taxonomic output is not already known - if (any(!already_known)) { - x_unknown <- x[!already_known] - x_unknown <- gsub(" ?[(].*[)] ?", "", x_unknown, perl = TRUE) - x_unknown <- gsub("[^a-z ]", " ", x_unknown, perl = TRUE) - x_unknown <- gsub(" +", " ", x_unknown, perl = TRUE) - print(x_unknown) - x_search <- gsub("([a-z])[a-z]*( ([a-z])[a-z]*)?( ([a-z])[a-z]*)?", "^\\1.* \\3.* \\5.*", x_unknown, perl = TRUE) - x_search <- gsub("( [.][*])+$", "", x_search, perl = TRUE) - print(x_search) - for (i in seq_len(length(x_unknown))) { - # search first, second and third part - mos_to_search <- MO_lookup[which(MO_lookup$fullname_lower %like_case% x_search[i]), "fullname", drop = TRUE] - score <- mo_matching_score(x_unknown[i], mos_to_search) - out <- mos_to_search[order(score, decreasing = TRUE)][1:25] # keep first 25 - print(score[order(score, decreasing = TRUE)][1]) - x[!already_known][i] <- MO_lookup$mo[match(out[1], MO_lookup$fullname)] - } - } - } - x -} diff --git a/R/mo_property.R b/R/mo_property.R index abcd4ab4..eeb87b29 100755 --- a/R/mo_property.R +++ b/R/mo_property.R @@ -601,7 +601,7 @@ mo_synonyms <- function(x, language = get_AMR_locale(), ...) { IDs <- mo_name(x = x, language = NULL) syns <- lapply(IDs, function(newname) { - res <- sort(microorganisms.old[which(microorganisms.old$fullname_new == newname), "fullname"]) + res <- sort(microorganisms.old[which(microorganisms.old$fullname_new == newname), "fullname", drop = TRUE]) if (length(res) == 0) { NULL } else { @@ -664,7 +664,7 @@ mo_url <- function(x, open = FALSE, language = get_AMR_locale(), ...) { x.mo <- as.mo(x = x, language = language, ... = ...) metadata <- get_mo_failures_uncertainties_renamed() - df <- microorganisms[match(x.mo, microorganisms$mo), c("mo", "fullname", "source", "kingdom", "rank")] + df <- microorganisms[match(x.mo, microorganisms$mo), c("mo", "fullname", "source", "kingdom", "rank"), drop = FALSE] df$url <- ifelse(df$source == "LPSN", paste0(CATALOGUE_OF_LIFE$url_LPSN, "/species/", gsub(" ", "-", tolower(df$fullname), fixed = TRUE)), paste0(CATALOGUE_OF_LIFE$url_CoL, "/data/search?type=EXACT&q=", gsub(" ", "%20", df$fullname, fixed = TRUE))) diff --git a/R/pca.R b/R/pca.R index 599fd59f..fc169d67 100755 --- a/R/pca.R +++ b/R/pca.R @@ -114,7 +114,7 @@ pca <- function(x, error = function(e) warning("column names could not be set")) # keep only numeric columns - x <- x[, vapply(FUN.VALUE = logical(1), x, function(y) is.numeric(y))] + x <- x[, vapply(FUN.VALUE = logical(1), x, function(y) is.numeric(y)), drop = FALSE] # bind the data set with the non-numeric columns x <- cbind(x.bak[, vapply(FUN.VALUE = logical(1), x.bak, function(y) !is.numeric(y) & !all(is.na(y))), drop = FALSE], x) } @@ -122,7 +122,7 @@ pca <- function(x, x <- pm_ungroup(x) # would otherwise select the grouping vars x <- x[rowSums(is.na(x)) == 0, ] # remove columns containing NAs - pca_data <- x[, which(vapply(FUN.VALUE = logical(1), x, function(x) is.numeric(x)))] + pca_data <- x[, which(vapply(FUN.VALUE = logical(1), x, function(x) is.numeric(x))), drop = FALSE] message_("Columns selected for PCA: ", vector_and(font_bold(colnames(pca_data), collapse = NULL), quotes = TRUE), ". Total observations available: ", nrow(pca_data), ".") diff --git a/R/proportion.R b/R/proportion.R index c46d06d4..78f4a979 100755 --- a/R/proportion.R +++ b/R/proportion.R @@ -106,12 +106,12 @@ #' \donttest{ #' if (require("dplyr")) { #' example_isolates %>% -#' group_by(hospital_id) %>% +#' group_by(ward) %>% #' summarise(r = resistance(CIP), #' n = n_rsi(CIP)) # n_rsi works like n_distinct in dplyr, see ?n_rsi #' #' example_isolates %>% -#' group_by(hospital_id) %>% +#' group_by(ward) %>% #' summarise(R = resistance(CIP, as_percent = TRUE), #' SI = susceptibility(CIP, as_percent = TRUE), #' n1 = count_all(CIP), # the actual total; sum of all three @@ -143,7 +143,7 @@ #' #' #' example_isolates %>% -#' group_by(hospital_id) %>% +#' group_by(ward) %>% #' summarise(cipro_p = susceptibility(CIP, as_percent = TRUE), #' cipro_n = count_all(CIP), #' genta_p = susceptibility(GEN, as_percent = TRUE), @@ -159,8 +159,8 @@ #' # It also supports grouping variables #' # (use rsi_df to also include the count) #' example_isolates %>% -#' select(hospital_id, AMX, CIP) %>% -#' group_by(hospital_id) %>% +#' select(ward, AMX, CIP) %>% +#' group_by(ward) %>% #' rsi_df(translate = FALSE) #' } #' } diff --git a/R/resistance_predict.R b/R/resistance_predict.R index 1b6a592d..3cde201c 100755 --- a/R/resistance_predict.R +++ b/R/resistance_predict.R @@ -80,7 +80,7 @@ #' filter_first_isolate() %>% #' filter(mo_genus(mo) == "Staphylococcus") %>% #' resistance_predict("PEN", model = "binomial") -#' plot(x) +#' print(plot(x)) #' #' # get the model from the object #' mymodel <- attributes(x)$model @@ -88,7 +88,7 @@ #' } #' #' # create nice plots with ggplot2 yourself -#' if (require("dplyr") & require("ggplot2")) { +#' if (require("dplyr") && require("ggplot2")) { #' #' data <- example_isolates %>% #' filter(mo == as.mo("E. coli")) %>% @@ -127,6 +127,9 @@ resistance_predict <- function(x, stop_if(is.null(model), 'choose a regression model with the `model` argument, e.g. resistance_predict(..., model = "binomial")') + x.bak <- x + x <- as.data.frame(x, stringsAsFactors = FALSE) + dots <- unlist(list(...)) if (length(dots) != 0) { # backwards compatibility with old arguments @@ -147,9 +150,6 @@ resistance_predict <- function(x, stop_ifnot(col_date %in% colnames(x), "column '", col_date, "' not found") - # no grouped tibbles - x <- as.data.frame(x, stringsAsFactors = FALSE) - year <- function(x) { # don't depend on lubridate or so, would be overkill for only this function if (all(grepl("^[0-9]{4}$", x))) { @@ -173,7 +173,7 @@ resistance_predict <- function(x, # remove rows with NAs df <- subset(df, !is.na(df[, col_ab, drop = TRUE])) df$year <- year(df[, col_date, drop = TRUE]) - df <- as.data.frame(rbind(table(df[, c("year", col_ab)])), + df <- as.data.frame(rbind(table(df[, c("year", col_ab), drop = FALSE])), stringsAsFactors = FALSE) df$year <- as.integer(rownames(df)) rownames(df) <- NULL @@ -275,15 +275,15 @@ resistance_predict <- function(x, } df_prediction$value <- ifelse(df_prediction$value > 1, 1, ifelse(df_prediction$value < 0, 0, df_prediction$value)) - df_prediction <- df_prediction[order(df_prediction$year), ] + df_prediction <- df_prediction[order(df_prediction$year), , drop = FALSE] - structure( - .Data = df_prediction, - class = c("resistance_predict", "data.frame"), - I_as_S = I_as_S, - model_title = model, - model = model_lm, - ab = col_ab + out <- as_original_data_class(df_prediction, class(x.bak)) + structure(out, + class = c("resistance_predict", class(out)), + I_as_S = I_as_S, + model_title = model, + model = model_lm, + ab = col_ab ) } diff --git a/R/rsi_calc.R b/R/rsi_calc.R index 9e5120c2..ed80c9c5 100755 --- a/R/rsi_calc.R +++ b/R/rsi_calc.R @@ -89,7 +89,7 @@ rsi_calc <- function(..., x <- NULL try(x <- as.data.frame(dots, stringsAsFactors = FALSE), silent = TRUE) if (is.null(x)) { - # support for example_isolates %pm>% group_by(hospital_id) %pm>% summarise(amox = susceptibility(GEN, AMX)) + # support for example_isolates %pm>% group_by(ward) %pm>% summarise(amox = susceptibility(GEN, AMX)) x <- as.data.frame(list(...), stringsAsFactors = FALSE) } } @@ -226,6 +226,7 @@ rsi_calc_df <- function(type, # "proportion", "count" or "both" translate_ab <- get_translate_ab(translate_ab) + data.bak <- data # select only groups and antibiotics if (is_null_or_grouped_tbl(data)) { data_has_groups <- TRUE @@ -331,9 +332,9 @@ rsi_calc_df <- function(type, # "proportion", "count" or "both" if (data_has_groups) { # ordering by the groups and two more: "antibiotic" and "interpretation" - out <- pm_ungroup(out[do.call("order", out[, seq_len(length(groups) + 2)]), ]) + out <- pm_ungroup(out[do.call("order", out[, seq_len(length(groups) + 2), drop = FALSE]), , drop = FALSE]) } else { - out <- out[order(out$antibiotic, out$interpretation), ] + out <- out[order(out$antibiotic, out$interpretation), , drop = FALSE] } if (type == "proportion") { @@ -344,8 +345,8 @@ rsi_calc_df <- function(type, # "proportion", "count" or "both" } rownames(out) <- NULL - class(out) <- c("rsi_df", class(out)) - out + out <- as_original_data_class(out, class(data.bak)) + structure(out, class = c("rsi_df", class(out))) } get_translate_ab <- function(translate_ab) { diff --git a/R/sysdata.rda b/R/sysdata.rda index b698b578..08ad91a4 100644 Binary files a/R/sysdata.rda and b/R/sysdata.rda differ diff --git a/R/translate.R b/R/translate.R index 8926b58a..9b8bbeff 100755 --- a/R/translate.R +++ b/R/translate.R @@ -27,13 +27,13 @@ #' #' For language-dependent output of AMR functions, like [mo_name()], [mo_gramstain()], [mo_type()] and [ab_name()]. #' @param x text to translate -#' @param lang language to choose. Use one of these supported language names or ISO-639-1 codes: `r paste0('"', sapply(LANGUAGES_SUPPORTED_NAMES, function(x) x[[1]]), '" ("' , LANGUAGES_SUPPORTED, '")', collapse = ", ")`. +#' @param language language to choose. Use one of these supported language names or ISO-639-1 codes: `r paste0('"', sapply(LANGUAGES_SUPPORTED_NAMES, function(x) x[[1]]), '" ("' , LANGUAGES_SUPPORTED, '")', collapse = ", ")`. #' @details The currently `r length(LANGUAGES_SUPPORTED)` supported languages are `r vector_and(sapply(LANGUAGES_SUPPORTED_NAMES, function(x) x[[1]]), quotes = FALSE, sort = FALSE)`. All these languages have translations available for all antimicrobial agents and colloquial microorganism names. #' #' Please read about adding or updating a language in [our Wiki](https://github.com/msberends/AMR/wiki/). #' #' ## Changing the Default Language -#' The system language will be used at default (as returned by `Sys.getenv("LANG")` or, if `LANG` is not set, [Sys.getlocale("LC_COLLATE")]), if that language is supported. But the language to be used can be overwritten in two ways and will be checked in this order: +#' The system language will be used at default (as returned by `Sys.getenv("LANG")` or, if `LANG` is not set, [`Sys.getlocale("LC_COLLATE")`][Sys.getlocale()]), if that language is supported. But the language to be used can be overwritten in two ways and will be checked in this order: #' #' 1. Setting the R option `AMR_locale`, either by using `set_AMR_locale()` or by running e.g. `options(AMR_locale = "de")`. #' @@ -93,10 +93,10 @@ get_AMR_locale <- function() { #' @rdname translate #' @export -set_AMR_locale <- function(lang) { - lang <- validate_language(lang) - options(AMR_locale = lang) - message_("Using the ", LANGUAGES_SUPPORTED_NAMES[[lang]]$exonym, " language (", LANGUAGES_SUPPORTED_NAMES[[lang]]$endonym, ") for the AMR package for this session.") +set_AMR_locale <- function(language) { + language <- validate_language(language) + options(AMR_locale = language) + message_("Using the ", LANGUAGES_SUPPORTED_NAMES[[language]]$exonym, " language (", LANGUAGES_SUPPORTED_NAMES[[language]]$endonym, ") for the AMR package for this session.") } #' @rdname translate @@ -111,6 +111,7 @@ translate_AMR <- function(x, language = get_AMR_locale()) { translate_into_language(x, language = language) } + validate_language <- function(language, extra_txt = character(0)) { if (trimws(tolower(language)) %in% c("en", "english", "", "false", NA)) { return("en") @@ -124,8 +125,8 @@ validate_language <- function(language, extra_txt = character(0)) { lang } -find_language <- function(lang, fallback = TRUE) { - lang <- Map(function(l, n, check = lang) { +find_language <- function(language, fallback = TRUE) { + language <- Map(function(l, n, check = language) { grepl(paste0("^(", l[1], "|", l[2], "|", n, "(_|$)|", toupper(n), "(_|$))"), check, @@ -136,12 +137,12 @@ find_language <- function(lang, fallback = TRUE) { LANGUAGES_SUPPORTED_NAMES, LANGUAGES_SUPPORTED, USE.NAMES = TRUE) - lang <- names(which(lang == TRUE)) - if (isTRUE(fallback) && length(lang) == 0) { + language <- names(which(language == TRUE)) + if (isTRUE(fallback) && length(language) == 0) { # other language -> set to English - lang <- "en" + language <- "en" } - lang + language } # translate strings based on inst/translations.tsv diff --git a/R/zzz.R b/R/zzz.R index 5d47e1bc..d3951597 100755 --- a/R/zzz.R +++ b/R/zzz.R @@ -101,9 +101,13 @@ if (utf8_supported && !is_latex) { } }, silent = TRUE) + # be sure to print tibbles as tibbles + if (pkg_is_available("tibble", also_load = FALSE)) { + loadNamespace("tibble") + } # reference data - they have additional columns compared to `antibiotics` and `microorganisms` to improve speed - # they cannott be part of R/sysdata.rda since CRAN thinks it would make the package too large (+3 MB) + # they cannot be part of R/sysdata.rda since CRAN thinks it would make the package too large (+3 MB) assign(x = "AB_lookup", value = create_AB_lookup(), envir = asNamespace("AMR")) assign(x = "MO_lookup", value = create_MO_lookup(), envir = asNamespace("AMR")) assign(x = "MO.old_lookup", value = create_MO.old_lookup(), envir = asNamespace("AMR")) @@ -140,7 +144,7 @@ create_MO_lookup <- function() { MO_lookup$g_species <- gsub("^([a-z])[a-z]+ ([a-z]+) ?.*", "\\1 \\2", MO_lookup$fullname_lower, perl = TRUE) # so arrange data on prevalence first, then kingdom, then full name - MO_lookup[order(MO_lookup$prevalence, MO_lookup$kingdom_index, MO_lookup$fullname_lower), ] + MO_lookup[order(MO_lookup$prevalence, MO_lookup$kingdom_index, MO_lookup$fullname_lower), , drop = FALSE] } create_MO.old_lookup <- function() { @@ -151,7 +155,7 @@ create_MO.old_lookup <- function() { MO.old_lookup$g_species <- trimws(gsub("^([a-z])[a-z]+ ([a-z]+) ?.*", "\\1 \\2", MO.old_lookup$fullname_lower)) # so arrange data on prevalence first, then full name - MO.old_lookup[order(MO.old_lookup$prevalence, MO.old_lookup$fullname_lower), ] + MO.old_lookup[order(MO.old_lookup$prevalence, MO.old_lookup$fullname_lower), , drop = FALSE] } create_intr_resistance <- function() { diff --git a/data-raw/AMR_latest.tar.gz b/data-raw/AMR_latest.tar.gz deleted file mode 100644 index 99e90ba0..00000000 Binary files a/data-raw/AMR_latest.tar.gz and /dev/null differ diff --git a/data-raw/Loinc.csv b/data-raw/Loinc.csv index 52958a4a..34621ce6 100644 --- a/data-raw/Loinc.csv +++ b/data-raw/Loinc.csv @@ -71673,7 +71673,7 @@ "7553-1","Origanum vulgare Ab.IgG","ACnc","Pt","Ser","Qn","","ALLERGY","2.27","MIN","","ACTIVE","","1","","","","","","Y","","ABS; Aby; Allergen; Allergens; ALLERGY TESTING; Antby; Anti; Antibodies; Antibody; Arbitrary concentration; Autoantibodies; Autoantibody; f283; Immune globulin G; Immunoglobulin G; Oregano; Oreganum; Point in time; QNT; Quan; Quant; Quantitative; Random; Rf283; Serum; SR","Oregano IgG Qn","Both","","","","","Oregano IgG Ab [Units/volume] in Serum","","","","","","","0","0","0","","","","","","1.0h(2)","","Oregano IgG Qn (S)" "75531-4","Enrollment basis","Type","Pt","^Patient","Nom","","SURVEY.PCORNET","2.50","MIN","","ACTIVE","","4","","","","","","","","Nominal; Point in time; Random; Survey; SURVEY.PCORNET; Typ","Enrollment basis","Observation","","","","","Enrollment basis","","","","","","","0","0","0","","","","","","2.50","","" "75532-2","Applicable accrediting agency for unit","Type","Pt","{Nursing unit}","Nom","NMMDS","SURVEY.NMMDS","2.50","MIN","Types of accreditation that are appropriate or applicable to a unit or service. The unit may or may not have received the accreditation.","ACTIVE","","4","","","","","","","","Nominal; Nursing Management Minimum Data Set; Point in time; Random; Survey; SURVEY.NMMDS; Typ","","Observation","","","Copyright © 2006 Diane Huber and Connie Delaney. Used with permission.","","Applicable accrediting agency for unit [NMMDS]","","","","","","","0","0","0","","NMMDS","","","","2.50","","" -"75533-0","Accreditation, certification, & licensure panel","-","Pt","{Nursing unit}","-","NMMDS","PANEL.SURVEY.NMMDS","2.52","MIN","The set of terms in this panel are used to indicate quality assurance organizations of the nursing delivery unit/service by 3 different quality measure categories: accreditation, certification and licensure. Accreditation is a seal of approval given by private, nationally recognized groups that check on the quality of care at health care facilities and organizations. Health care organizations must meet certain quality standards in order to be accredited. Certification is the formal recognition of the knowledge, skills, and experience demonstrated by the achievement of standards that are identified by the profession² (ANA, 2009). Licensure is the granting of authority to practice² (ANA, 2009). State agencies determine the requirements for licensure and examine the competency necessary to meet quality standards.","ACTIVE","","4","","","","","","","","Nursing Management Minimum Data Set; Pan; PANEL.SURVEY.NMMDS; Panl; Pnl; Point in time; Random; Survey; SURVEY.NMMDS","","","","","Copyright © 2006 Diane Huber and Connie Delaney. Used with permission.","","NMMDS accreditation, certification, and licensure panel [NMMDS]","","","","","","","0","0","0","","NMMDS","Panel","","","2.50","","" +"75533-0","Accreditation, certification, & licensure panel","-","Pt","{Nursing unit}","-","NMMDS","PANEL.SURVEY.NMMDS","2.52","MIN","The set of terms in this panel are used to indicate quality assurance organizations of the nursing delivery unit/service by 3 different quality measure categories: accreditation, certification and licensure. Accreditation is a seal of approval given by private, nationally recognized groups that check on the quality of care at health care facilities and organizations. Health care organizations must meet certain quality standards in order to be accredited. Certification is the formal recognition of the knowledge, skills, and experience demonstrated by the achievement of standards that are identified by the profession² (ANA, 2009). Licensure is the granting of authority to practise² (ANA, 2009). State agencies determine the requirements for licensure and examine the competency necessary to meet quality standards.","ACTIVE","","4","","","","","","","","Nursing Management Minimum Data Set; Pan; PANEL.SURVEY.NMMDS; Panl; Pnl; Point in time; Random; Survey; SURVEY.NMMDS","","","","","Copyright © 2006 Diane Huber and Connie Delaney. Used with permission.","","NMMDS accreditation, certification, and licensure panel [NMMDS]","","","","","","","0","0","0","","NMMDS","Panel","","","2.50","","" "75534-8","Accreditation received","Type","Pt","{Nursing unit}","Nom","NMMDS","SURVEY.NMMDS","2.50","MIN","Types of accreditation obtained by a unit to demonstrate quality of care. Accreditation is a seal of approval given by private, nationally recognized groups that check on the quality of care at health care facilities and organizations. Health care organizations must meet certain quality standards in order to be accredited.","ACTIVE","","4","","","","","","","","Nominal; Nursing Management Minimum Data Set; Point in time; Random; Survey; SURVEY.NMMDS; Typ","","Observation","","","Copyright © 2006 Diane Huber and Connie Delaney. Used with permission.","","Accreditation received [NMMDS]","","","","","","","0","0","0","","NMMDS","","","","2.50","","" "75535-5","Certification received","Type","Pt","{Nursing unit}","Nom","NMMDS","SURVEY.NMMDS","2.50","MIN","Types of certification obtained by a unit to demonstrate quality care.","ACTIVE","","4","","","","","","","","Nominal; Nursing Management Minimum Data Set; Point in time; Random; Survey; SURVEY.NMMDS; Typ","","Observation","","","Copyright © 2006 Diane Huber and Connie Delaney. Used with permission.","","Certification received [NMMDS]","","","","","","","0","0","0","","NMMDS","","","","2.50","","" "75536-3","Applicable certification agency for unit","Type","Pt","{Nursing unit}","Nom","NMMDS","SURVEY.NMMDS","2.50","MIN","Types of certification that are appropriate or applicable to a unit or service. The unit may or may not have received the certification.","ACTIVE","","4","","","","","","","","Nominal; Nursing Management Minimum Data Set; Point in time; Random; Survey; SURVEY.NMMDS; Typ","","Observation","","","Copyright © 2006 Diane Huber and Connie Delaney. Used with permission.","","Applicable certification agency for unit [NMMDS]","","","","","","","0","0","0","","NMMDS","","","","2.50","","" diff --git a/data-raw/_language_update.R b/data-raw/_language_update.R index 819eae7d..b5340724 100644 --- a/data-raw/_language_update.R +++ b/data-raw/_language_update.R @@ -55,7 +55,7 @@ lang_env$TRANSLATIONS <- utils::read.delim(file = "data-raw/translations.tsv", quote = "") lang_env$LANGUAGES_SUPPORTED_NAMES <- c(list(en = list(exonym = "English", endonym = "English")), - lapply(lang_env$TRANSLATIONS[, which(nchar(colnames(lang_env$TRANSLATIONS)) == 2)], + lapply(lang_env$TRANSLATIONS[, which(nchar(colnames(lang_env$TRANSLATIONS)) == 2), drop = FALSE], function(x) list(exonym = x[1], endonym = x[2]))) lang_env$LANGUAGES_SUPPORTED <- names(lang_env$LANGUAGES_SUPPORTED_NAMES) diff --git a/data-raw/data_dsmz.rds b/data-raw/data_dsmz.rds deleted file mode 100644 index 27e0f4b8..00000000 Binary files a/data-raw/data_dsmz.rds and /dev/null differ diff --git a/data-raw/download_buttons.psd b/data-raw/download_buttons.psd deleted file mode 100644 index 7d814086..00000000 Binary files a/data-raw/download_buttons.psd and /dev/null differ diff --git a/data-raw/download_dta.png b/data-raw/download_dta.png deleted file mode 100644 index 23a2d5d8..00000000 Binary files a/data-raw/download_dta.png and /dev/null differ diff --git a/data-raw/download_r.png b/data-raw/download_r.png deleted file mode 100644 index a97dab3a..00000000 Binary files a/data-raw/download_r.png and /dev/null differ diff --git a/data-raw/download_sas.png b/data-raw/download_sas.png deleted file mode 100644 index b4882c8a..00000000 Binary files a/data-raw/download_sas.png and /dev/null differ diff --git a/data-raw/download_sav.png b/data-raw/download_sav.png deleted file mode 100644 index 8ef2dffb..00000000 Binary files a/data-raw/download_sav.png and /dev/null differ diff --git a/data-raw/download_txt.png b/data-raw/download_txt.png deleted file mode 100644 index d5a7bbb3..00000000 Binary files a/data-raw/download_txt.png and /dev/null differ diff --git a/data-raw/download_xlsx.png b/data-raw/download_xlsx.png deleted file mode 100644 index 05f78fd2..00000000 Binary files a/data-raw/download_xlsx.png and /dev/null differ diff --git a/data-raw/ex2.R b/data-raw/ex2.R deleted file mode 100644 index a1d0e69b..00000000 --- a/data-raw/ex2.R +++ /dev/null @@ -1,13 +0,0 @@ -ex2 <- example_isolates -for (extra_id in seq_len(50)) { - ex2 <- ex2 %>% - bind_rows(example_isolates %>% mutate(patient_id = paste0(patient_id, extra_id))) -} -# randomly clear antibibiograms of 2% -clr <- sort(sample(x = seq_len(nrow(ex2)), - size = nrow(ex2) * 0.02)) -for (row in which(is.rsi(ex2))) { - ex2[clr, row] <- NA_rsi_ -} - - diff --git a/data-raw/ex2.rds b/data-raw/ex2.rds deleted file mode 100644 index 39f81fc2..00000000 Binary files a/data-raw/ex2.rds and /dev/null differ diff --git a/data-raw/example_isolates.rds b/data-raw/example_isolates.rds deleted file mode 100644 index 46eac9ba..00000000 Binary files a/data-raw/example_isolates.rds and /dev/null differ diff --git a/data-raw/exploratory_data_analysis_test.R b/data-raw/exploratory_data_analysis_test.R deleted file mode 100644 index f833fc67..00000000 --- a/data-raw/exploratory_data_analysis_test.R +++ /dev/null @@ -1,28 +0,0 @@ -library(dplyr) -example_isolates %>% - select(mo, where(is.rsi)) %>% - tidyr::pivot_longer(cols = where(is.rsi)) %>% - # remove intrisic R - filter(!paste(mo, name) %in% AMR:::INTRINSIC_R) %>% - mutate(name = as.ab(name), - value = ifelse(value == "R", 1, 0), - class = ab_group(name)) %>% - group_by(mo, class) %>% - summarise(n = n(), - res = mean(value, na.rm = TRUE)) %>% - filter(n > 30, !is.na(res)) - - - -df <- example_isolates -search_mo <- "B_ESCHR_COLI" -intrinsic_res <- INTRINSIC_R[INTRINSIC_R %like% search_mo] -intrinsic_res <- gsub(".* (.*)", "\\1", intrinsic_res) - -x <- df %>% - select(mo, where(is.rsi)) %>% - filter(mo == search_mo) %>% - # at least 30 results available - select(function(x) sum(!is.na(x)) >= 30) %>% - # remove intrisic R - select(!matches(paste(intrinsic_res, collapse = "|"))) diff --git a/data-raw/github_preview.png b/data-raw/github_preview.png deleted file mode 100644 index 0feb5c9d..00000000 Binary files a/data-raw/github_preview.png and /dev/null differ diff --git a/data-raw/github_preview.psd b/data-raw/github_preview.psd deleted file mode 100644 index 985ac64e..00000000 Binary files a/data-raw/github_preview.psd and /dev/null differ diff --git a/data-raw/pre-commit-hook.R b/data-raw/pre-commit-hook.R index e7d17686..1c3d8bf0 100644 --- a/data-raw/pre-commit-hook.R +++ b/data-raw/pre-commit-hook.R @@ -29,7 +29,7 @@ library(dplyr, warn.conflicts = FALSE) devtools::load_all(quiet = TRUE) -set_AMR_locale("en") +suppressMessages(set_AMR_locale("en")) old_globalenv <- ls(envir = globalenv()) @@ -69,7 +69,7 @@ TRANSLATIONS <- utils::read.delim(file = "data-raw/translations.tsv", quote = "") LANGUAGES_SUPPORTED_NAMES <- c(list(en = list(exonym = "English", endonym = "English")), - lapply(TRANSLATIONS[, which(nchar(colnames(TRANSLATIONS)) == 2)], + lapply(TRANSLATIONS[, which(nchar(colnames(TRANSLATIONS)) == 2), drop = FALSE], function(x) list(exonym = x[1], endonym = x[2]))) LANGUAGES_SUPPORTED <- names(LANGUAGES_SUPPORTED_NAMES) @@ -122,7 +122,7 @@ create_MO_fullname_lower <- function() { MO_lookup$species, MO_lookup$subspecies))) ind <- MO_lookup$genus == "" | grepl("^[(]unknown ", MO_lookup$fullname, perl = TRUE) - MO_lookup[ind, "fullname_lower"] <- tolower(MO_lookup[ind, "fullname"]) + MO_lookup[ind, "fullname_lower"] <- tolower(MO_lookup[ind, "fullname", drop = TRUE]) MO_lookup$fullname_lower <- trimws(gsub("[^.a-z0-9/ \\-]+", "", MO_lookup$fullname_lower, perl = TRUE)) MO_lookup$fullname_lower } @@ -208,49 +208,50 @@ create_AB_lookup <- function() { AB_LOOKUP <- create_AB_lookup() # Export to package as internal data ---- -usethis::use_data(EUCAST_RULES_DF, - TRANSLATIONS, - LANGUAGES_SUPPORTED_NAMES, - LANGUAGES_SUPPORTED, - MO_CONS, - MO_COPS, - MO_STREP_ABCG, - MO_FULLNAME_LOWER, - MO_PREVALENT_GENERA, - AB_LOOKUP, - AB_AMINOGLYCOSIDES, - AB_AMINOPENICILLINS, - AB_ANTIFUNGALS, - AB_ANTIMYCOBACTERIALS, - AB_CARBAPENEMS, - AB_CEPHALOSPORINS, - AB_CEPHALOSPORINS_1ST, - AB_CEPHALOSPORINS_2ND, - AB_CEPHALOSPORINS_3RD, - AB_CEPHALOSPORINS_4TH, - AB_CEPHALOSPORINS_5TH, - AB_CEPHALOSPORINS_EXCEPT_CAZ, - AB_FLUOROQUINOLONES, - AB_LIPOGLYCOPEPTIDES, - AB_GLYCOPEPTIDES, - AB_GLYCOPEPTIDES_EXCEPT_LIPO, - AB_LINCOSAMIDES, - AB_MACROLIDES, - AB_OXAZOLIDINONES, - AB_PENICILLINS, - AB_POLYMYXINS, - AB_QUINOLONES, - AB_STREPTOGRAMINS, - AB_TETRACYCLINES, - AB_TETRACYCLINES_EXCEPT_TGC, - AB_TRIMETHOPRIMS, - AB_UREIDOPENICILLINS, - AB_BETALACTAMS, - DEFINED_AB_GROUPS, - internal = TRUE, - overwrite = TRUE, - version = 2, - compress = "xz") +usethis::ui_info(paste0("Saving {usethis::ui_value('sysdata.rda')} to {usethis::ui_value('R/')}")) +suppressMessages(usethis::use_data(EUCAST_RULES_DF, + TRANSLATIONS, + LANGUAGES_SUPPORTED_NAMES, + LANGUAGES_SUPPORTED, + MO_CONS, + MO_COPS, + MO_STREP_ABCG, + MO_FULLNAME_LOWER, + MO_PREVALENT_GENERA, + AB_LOOKUP, + AB_AMINOGLYCOSIDES, + AB_AMINOPENICILLINS, + AB_ANTIFUNGALS, + AB_ANTIMYCOBACTERIALS, + AB_CARBAPENEMS, + AB_CEPHALOSPORINS, + AB_CEPHALOSPORINS_1ST, + AB_CEPHALOSPORINS_2ND, + AB_CEPHALOSPORINS_3RD, + AB_CEPHALOSPORINS_4TH, + AB_CEPHALOSPORINS_5TH, + AB_CEPHALOSPORINS_EXCEPT_CAZ, + AB_FLUOROQUINOLONES, + AB_LIPOGLYCOPEPTIDES, + AB_GLYCOPEPTIDES, + AB_GLYCOPEPTIDES_EXCEPT_LIPO, + AB_LINCOSAMIDES, + AB_MACROLIDES, + AB_OXAZOLIDINONES, + AB_PENICILLINS, + AB_POLYMYXINS, + AB_QUINOLONES, + AB_STREPTOGRAMINS, + AB_TETRACYCLINES, + AB_TETRACYCLINES_EXCEPT_TGC, + AB_TRIMETHOPRIMS, + AB_UREIDOPENICILLINS, + AB_BETALACTAMS, + DEFINED_AB_GROUPS, + internal = TRUE, + overwrite = TRUE, + version = 2, + compress = "xz")) # Export data sets to the repository in different formats ----------------- @@ -285,7 +286,7 @@ rsi <- rsi_translation %>% mutate(mo_name = mo_name(mo, language = NULL), .after = mo) %>% mutate(ab_name = ab_name(ab, language = NULL), .after = ab) if (changed_md5(rsi)) { - usethis::ui_info(paste0("Saving {usethis::ui_value('rsi_translation')} to {usethis::ui_value('/data-raw/')}")) + usethis::ui_info(paste0("Saving {usethis::ui_value('rsi_translation')} to {usethis::ui_value('data-raw/')}")) write_md5(rsi) try(saveRDS(rsi, "data-raw/rsi_translation.rds", version = 2, compress = "xz"), silent = TRUE) try(write.table(rsi, "data-raw/rsi_translation.txt", sep = "\t", na = "", row.names = FALSE), silent = TRUE) @@ -298,7 +299,7 @@ if (changed_md5(rsi)) { } if (changed_md5(microorganisms)) { - usethis::ui_info(paste0("Saving {usethis::ui_value('microorganisms')} to {usethis::ui_value('/data-raw/')}")) + usethis::ui_info(paste0("Saving {usethis::ui_value('microorganisms')} to {usethis::ui_value('data-raw/')}")) write_md5(microorganisms) try(saveRDS(microorganisms, "data-raw/microorganisms.rds", version = 2, compress = "xz"), silent = TRUE) try(write.table(mo, "data-raw/microorganisms.txt", sep = "\t", na = "", row.names = FALSE), silent = TRUE) @@ -315,7 +316,7 @@ if (changed_md5(microorganisms)) { } if (changed_md5(microorganisms.old)) { - usethis::ui_info(paste0("Saving {usethis::ui_value('microorganisms.old')} to {usethis::ui_value('/data-raw/')}")) + usethis::ui_info(paste0("Saving {usethis::ui_value('microorganisms.old')} to {usethis::ui_value('data-raw/')}")) write_md5(microorganisms.old) try(saveRDS(microorganisms.old, "data-raw/microorganisms.old.rds", version = 2, compress = "xz"), silent = TRUE) try(write.table(microorganisms.old, "data-raw/microorganisms.old.txt", sep = "\t", na = "", row.names = FALSE), silent = TRUE) @@ -329,7 +330,7 @@ if (changed_md5(microorganisms.old)) { ab <- dplyr::mutate_if(antibiotics, ~!is.numeric(.), as.character) if (changed_md5(ab)) { - usethis::ui_info(paste0("Saving {usethis::ui_value('antibiotics')} to {usethis::ui_value('/data-raw/')}")) + usethis::ui_info(paste0("Saving {usethis::ui_value('antibiotics')} to {usethis::ui_value('data-raw/')}")) write_md5(ab) try(saveRDS(antibiotics, "data-raw/antibiotics.rds", version = 2, compress = "xz"), silent = TRUE) try(write.table(antibiotics, "data-raw/antibiotics.txt", sep = "\t", na = "", row.names = FALSE), silent = TRUE) @@ -343,7 +344,7 @@ if (changed_md5(ab)) { av <- dplyr::mutate_if(antivirals, ~!is.numeric(.), as.character) if (changed_md5(av)) { - usethis::ui_info(paste0("Saving {usethis::ui_value('antivirals')} to {usethis::ui_value('/data-raw/')}")) + usethis::ui_info(paste0("Saving {usethis::ui_value('antivirals')} to {usethis::ui_value('data-raw/')}")) write_md5(av) try(saveRDS(antivirals, "data-raw/antivirals.rds", version = 2, compress = "xz"), silent = TRUE) try(write.table(av, "data-raw/antivirals.txt", sep = "\t", na = "", row.names = FALSE), silent = TRUE) @@ -360,7 +361,7 @@ intrinsicR <- data.frame(microorganism = mo_name(intrinsic_resistant$mo, languag antibiotic = ab_name(intrinsic_resistant$ab, language = NULL), stringsAsFactors = FALSE) if (changed_md5(intrinsicR)) { - usethis::ui_info(paste0("Saving {usethis::ui_value('intrinsic_resistant')} to {usethis::ui_value('/data-raw/')}")) + usethis::ui_info(paste0("Saving {usethis::ui_value('intrinsic_resistant')} to {usethis::ui_value('data-raw/')}")) write_md5(intrinsicR) try(saveRDS(intrinsicR, "data-raw/intrinsic_resistant.rds", version = 2, compress = "xz"), silent = TRUE) try(write.table(intrinsicR, "data-raw/intrinsic_resistant.txt", sep = "\t", na = "", row.names = FALSE), silent = TRUE) @@ -373,7 +374,7 @@ if (changed_md5(intrinsicR)) { } if (changed_md5(dosage)) { - usethis::ui_info(paste0("Saving {usethis::ui_value('dosage')} to {usethis::ui_value('/data-raw/')}")) + usethis::ui_info(paste0("Saving {usethis::ui_value('dosage')} to {usethis::ui_value('data-raw/')}")) write_md5(dosage) try(saveRDS(dosage, "data-raw/dosage.rds", version = 2, compress = "xz"), silent = TRUE) try(write.table(dosage, "data-raw/dosage.txt", sep = "\t", na = "", row.names = FALSE), silent = TRUE) @@ -385,7 +386,7 @@ if (changed_md5(dosage)) { try(arrow::write_parquet(dosage, "data-raw/dosage.parquet"), silent = TRUE) } -reset_AMR_locale() +suppressMessages(reset_AMR_locale()) # remove leftovers from global env current_globalenv <- ls(envir = globalenv()) diff --git a/data-raw/reproduction_of_antibiotics.R b/data-raw/reproduction_of_antibiotics.R index 50fd8505..3d711a8a 100644 --- a/data-raw/reproduction_of_antibiotics.R +++ b/data-raw/reproduction_of_antibiotics.R @@ -138,7 +138,7 @@ abx2 <- abx2 %>% arrange(ab) seqnr <- 0 # add follow up nrs for (i in 2:nrow(abx2)) { - if (abx2[i, "ab"] == abx2[i - 1, "ab"]) { + if (abx2[i, "ab", drop = TRUE] == abx2[i - 1, "ab", drop = TRUE]) { seqnr <- seqnr + 1 abx2[i, "seqnr"] <- seqnr } else { @@ -147,7 +147,7 @@ for (i in 2:nrow(abx2)) { } for (i in 2:nrow(abx2)) { if (!is.na(abx2[i, "seqnr"])) { - abx2[i, "ab"] <- paste0(abx2[i, "ab"], abx2[i, "seqnr"]) + abx2[i, "ab"] <- paste0(abx2[i, "ab", drop = TRUE], abx2[i, "seqnr", drop = TRUE]) } } abx2 <- abx2 %>% select(-seqnr) %>% arrange(name) diff --git a/data-raw/reproduction_of_example_isolates_unclean.R b/data-raw/reproduction_of_example_isolates_unclean.R index df746f90..fa24456c 100644 --- a/data-raw/reproduction_of_example_isolates_unclean.R +++ b/data-raw/reproduction_of_example_isolates_unclean.R @@ -92,7 +92,7 @@ example_isolates_unclean <- data_a %>% example_isolates_unclean$patient_id <- sample(patients, size = nrow(example_isolates_unclean), replace = TRUE) example_isolates_unclean <- example_isolates_unclean %>% - select(patient_id, hospital, date, bacteria, everything()) - -usethis::use_data(example_isolates_unclean, overwrite = TRUE) + select(patient_id, hospital, date, bacteria, everything()) %>% + dataset_UTF8_to_ASCII() +usethis::use_data(example_isolates_unclean, overwrite = TRUE, internal = FALSE, version = 2, compress = "xz") diff --git a/data-raw/reproduction_of_intrinsic_resistant.R b/data-raw/reproduction_of_intrinsic_resistant.R index a6767afc..5f964a85 100644 --- a/data-raw/reproduction_of_intrinsic_resistant.R +++ b/data-raw/reproduction_of_intrinsic_resistant.R @@ -36,7 +36,7 @@ int_resis <- eucast_rules(int_resis, is.na(have_these_values) & reference.version == 3.3), info = FALSE) -int_resis2 <- int_resis[, sapply(int_resis, function(x) any(!is.rsi(x) | x == "R"))] %>% +int_resis2 <- int_resis[, sapply(int_resis, function(x) any(!is.rsi(x) | x == "R")), drop = FALSE] %>% tidyr::pivot_longer(-mo) %>% filter(value == "R") %>% select(mo, ab = name) diff --git a/data-raw/tinytest_1.2.4.10.tar.gz b/data-raw/tinytest_1.2.4.10.tar.gz deleted file mode 100644 index bc71fd65..00000000 Binary files a/data-raw/tinytest_1.2.4.10.tar.gz and /dev/null differ diff --git a/data/WHONET.rda b/data/WHONET.rda index 358d0b2d..b4b8159f 100644 Binary files a/data/WHONET.rda and b/data/WHONET.rda differ diff --git a/data/antibiotics.rda b/data/antibiotics.rda index 51336713..c7bc3ab8 100755 Binary files a/data/antibiotics.rda and b/data/antibiotics.rda differ diff --git a/data/antivirals.rda b/data/antivirals.rda index a041649c..675f5c2d 100644 Binary files a/data/antivirals.rda and b/data/antivirals.rda differ diff --git a/data/dosage.rda b/data/dosage.rda index 9ca04232..2a914f77 100644 Binary files a/data/dosage.rda and b/data/dosage.rda differ diff --git a/data/example_isolates.rda b/data/example_isolates.rda index 38f9c2c8..86956a8e 100644 Binary files a/data/example_isolates.rda and b/data/example_isolates.rda differ diff --git a/data/example_isolates_unclean.rda b/data/example_isolates_unclean.rda index d15c0393..04166b01 100644 Binary files a/data/example_isolates_unclean.rda and b/data/example_isolates_unclean.rda differ diff --git a/data/intrinsic_resistant.rda b/data/intrinsic_resistant.rda index b5fd5d31..0257313f 100644 Binary files a/data/intrinsic_resistant.rda and b/data/intrinsic_resistant.rda differ diff --git a/data/microorganisms.codes.rda b/data/microorganisms.codes.rda index 5d0223a3..a970048f 100644 Binary files a/data/microorganisms.codes.rda and b/data/microorganisms.codes.rda differ diff --git a/data/microorganisms.rda b/data/microorganisms.rda index 8859ffd6..d84b6f72 100644 Binary files a/data/microorganisms.rda and b/data/microorganisms.rda differ diff --git a/data/rsi_translation.rda b/data/rsi_translation.rda index acbbf9a4..1001176c 100644 Binary files a/data/rsi_translation.rda and b/data/rsi_translation.rda differ diff --git a/index.md b/index.md index a37b0dd4..a1f140a6 100644 --- a/index.md +++ b/index.md @@ -65,8 +65,8 @@ library(AMR) library(dplyr) out <- example_isolates %>% - # group by hospital code: - group_by(hospital_id) %>% + # group by ward: + group_by(ward) %>% # calculate AMR using resistance(), over all aminoglycosides # and polymyxins: summarise(across(c(aminoglycosides(), polymyxins()), @@ -74,36 +74,33 @@ out <- example_isolates %>% out ``` -| hospital_id | GEN | TOB | AMK | KAN | COL | -|:------------|:-----:|:-----:|:-----:|:-----:|:-----:| -| A | 0.203 | 0.328 | 0.657 | 1 | 0.853 | -| B | 0.274 | 0.367 | 0.645 | 1 | 0.845 | -| C | 0.212 | 0.342 | 0.582 | 1 | 0.865 | -| D | 0.251 | 0.332 | 0.642 | 1 | 0.745 | +| ward | GEN | TOB | AMK | KAN | COL | +|:-----------|------:|------:|------:|------:|------:| +| Clinical | 0.229 | 0.315 | 0.626 | 1 | 0.780 | +| ICU | 0.290 | 0.400 | 0.662 | 1 | 0.857 | +| Outpatient | 0.200 | 0.368 | 0.605 | NA | 0.889 | ```r # transform the antibiotic columns to names: out %>% set_ab_names() ``` -| hospital_id | gentamicin | tobramycin | amikacin | kanamycin | colistin | -|:------------|:----------:|:----------:|:---------|:---------:|:---------:| -| A | 0.203 | 0.328 | 0.657 | 1 | 0.853 | -| B | 0.274 | 0.367 | 0.645 | 1 | 0.845 | -| C | 0.212 | 0.342 | 0.582 | 1 | 0.865 | -| D | 0.251 | 0.332 | 0.642 | 1 | 0.745 | +| ward | gentamicin | tobramycin | amikacin | kanamycin | colistin | +|:-----------|-----------:|-----------:|----------|----------:|----------:| +| Clinical | 0.229 | 0.315 | 0.626 | 1 | 0.780 | +| ICU | 0.290 | 0.400 | 0.662 | 1 | 0.857 | +| Outpatient | 0.200 | 0.368 | 0.605 | NA | 0.889 | ```r # transform the antibiotic column to ATC codes: out %>% set_ab_names(property = "atc") ``` -| hospital_id | J01GB03 | J01GB01 | J01GB06 | J01GB04 | J01XB01 | -|:------------|:----------:|:----------:|:---------|:---------:|:---------:| -| A | 0.203 | 0.328 | 0.657 | 1 | 0.853 | -| B | 0.274 | 0.367 | 0.645 | 1 | 0.845 | -| C | 0.212 | 0.342 | 0.582 | 1 | 0.865 | -| D | 0.251 | 0.332 | 0.642 | 1 | 0.745 | +| ward | J01GB03 | J01GB01 | J01GB06 | J01GB04 | J01XB01 | +|:-----------|-----------:|-----------:|----------|----------:|----------:| +| Clinical | 0.229 | 0.315 | 0.626 | 1 | 0.780 | +| ICU | 0.290 | 0.400 | 0.662 | 1 | 0.857 | +| Outpatient | 0.200 | 0.368 | 0.605 | NA | 0.889 | ### What else can you do with this package? diff --git a/inst/tinytest/test-_misc.R b/inst/tinytest/test-_misc.R index 2ffc6904..c08b8ffd 100755 --- a/inst/tinytest/test-_misc.R +++ b/inst/tinytest/test-_misc.R @@ -49,5 +49,5 @@ if (AMR:::pkg_is_available("dplyr", min_version = "1.0.0")) { # we rely on "grouped_tbl" being a class of grouped tibbles, so implement a test that checks for this: if (AMR:::pkg_is_available("dplyr", min_version = "1.0.0")) { - expect_true(AMR:::is_null_or_grouped_tbl(example_isolates %>% group_by(hospital_id))) + expect_true(AMR:::is_null_or_grouped_tbl(example_isolates %>% group_by(ward))) } diff --git a/inst/tinytest/test-bug_drug_combinations.R b/inst/tinytest/test-bug_drug_combinations.R index 5a053986..ed7c7444 100644 --- a/inst/tinytest/test-bug_drug_combinations.R +++ b/inst/tinytest/test-bug_drug_combinations.R @@ -30,7 +30,7 @@ expect_true(is.data.frame(format(b))) expect_true(is.data.frame(format(b, combine_IR = TRUE, add_ab_group = FALSE))) if (AMR:::pkg_is_available("dplyr", min_version = "1.0.0")) { expect_true(example_isolates %>% - group_by(hospital_id) %>% + group_by(ward) %>% bug_drug_combinations(FUN = mo_gramstain) %>% is.data.frame()) } diff --git a/inst/tinytest/test-count.R b/inst/tinytest/test-count.R index e91b61d6..b4db17cb 100644 --- a/inst/tinytest/test-count.R +++ b/inst/tinytest/test-count.R @@ -49,7 +49,7 @@ expect_error(count_susceptible("test", minimum = "test")) expect_error(count_susceptible("test", as_percent = "test")) expect_error(count_df(c("A", "B", "C"))) -expect_error(count_df(example_isolates[, "date"])) +expect_error(count_df(example_isolates[, "date", drop = TRUE])) if (AMR:::pkg_is_available("dplyr", min_version = "1.0.0")) { expect_equal(example_isolates %>% count_susceptible(AMC), 1433) @@ -63,7 +63,7 @@ if (AMR:::pkg_is_available("dplyr", min_version = "1.0.0")) { # count of cases expect_equal(example_isolates %>% - group_by(hospital_id) %>% + group_by(ward) %>% summarise(cipro = count_susceptible(CIP), genta = count_susceptible(GEN), combination = count_susceptible(CIP, GEN)) %>% @@ -89,9 +89,9 @@ if (AMR:::pkg_is_available("dplyr", min_version = "1.0.0")) { ) # grouping in rsi_calc_df() (= backbone of rsi_df()) - expect_true("hospital_id" %in% (example_isolates %>% - group_by(hospital_id) %>% - select(hospital_id, AMX, CIP, gender) %>% + expect_true("ward" %in% (example_isolates %>% + group_by(ward) %>% + select(ward, AMX, CIP, gender) %>% rsi_df() %>% colnames())) } diff --git a/inst/tinytest/test-data.R b/inst/tinytest/test-data.R index d6330c66..aa36aa35 100644 --- a/inst/tinytest/test-data.R +++ b/inst/tinytest/test-data.R @@ -45,16 +45,16 @@ expect_false(any(is.na(microorganisms.codes$mo))) expect_true(all(dosage$ab %in% antibiotics$ab)) expect_true(all(dosage$name %in% antibiotics$name)) # check valid disks/MICs -expect_false(any(is.na(as.mic(rsi_translation[which(rsi_translation$method == "MIC"), "breakpoint_S"])))) -expect_false(any(is.na(as.mic(rsi_translation[which(rsi_translation$method == "MIC"), "breakpoint_R"])))) -expect_false(any(is.na(as.disk(rsi_translation[which(rsi_translation$method == "DISK"), "breakpoint_S"])))) -expect_false(any(is.na(as.disk(rsi_translation[which(rsi_translation$method == "DISK"), "breakpoint_R"])))) +expect_false(any(is.na(as.mic(rsi_translation[which(rsi_translation$method == "MIC"), "breakpoint_S", drop = TRUE])))) +expect_false(any(is.na(as.mic(rsi_translation[which(rsi_translation$method == "MIC"), "breakpoint_R", drop = TRUE])))) +expect_false(any(is.na(as.disk(rsi_translation[which(rsi_translation$method == "DISK"), "breakpoint_S", drop = TRUE])))) +expect_false(any(is.na(as.disk(rsi_translation[which(rsi_translation$method == "DISK"), "breakpoint_R", drop = TRUE])))) # antibiotic names must always be coercible to their original AB code expect_identical(as.ab(antibiotics$name), antibiotics$ab) # there should be no diacritics (i.e. non ASCII) characters in the datasets (CRAN policy) -datasets <- data(package = "AMR", envir = asNamespace("AMR"))$results[, "Item"] +datasets <- data(package = "AMR", envir = asNamespace("AMR"))$results[, "Item", drop = TRUE] for (i in seq_len(length(datasets))) { dataset <- get(datasets[i], envir = asNamespace("AMR")) expect_identical(class(dataset), "data.frame") @@ -62,8 +62,8 @@ for (i in seq_len(length(datasets))) { } df <- AMR:::MO_lookup -expect_true(nrow(df[which(df$prevalence == 1), ]) < nrow(df[which(df$prevalence == 2), ])) -expect_true(nrow(df[which(df$prevalence == 2), ]) < nrow(df[which(df$prevalence == 3), ])) +expect_true(nrow(df[which(df$prevalence == 1), , drop = FALSE]) < nrow(df[which(df$prevalence == 2), , drop = FALSE])) +expect_true(nrow(df[which(df$prevalence == 2), , drop = FALSE]) < nrow(df[which(df$prevalence == 3), , drop = FALSE])) expect_true(all(c("mo", "fullname", "kingdom", "phylum", "class", "order", "family", "genus", "species", "subspecies", "rank", "ref", "species_id", "source", "prevalence", "snomed", diff --git a/inst/tinytest/test-episode.R b/inst/tinytest/test-episode.R index c1f8cb0c..330ab0e4 100644 --- a/inst/tinytest/test-episode.R +++ b/inst/tinytest/test-episode.R @@ -49,7 +49,7 @@ if (AMR:::pkg_is_available("dplyr", min_version = "1.0.0")) { mutate(out = first_isolate(., include_unknown = TRUE, method = "episode-based", info = FALSE)) ) y <- example_isolates %>% - group_by(patient_id, mo) %>% + group_by(patient, mo) %>% mutate(out = is_new_episode(date, 365)) expect_identical(which(x$out), which(y$out)) diff --git a/inst/tinytest/test-first_isolate.R b/inst/tinytest/test-first_isolate.R index 21ba5ad3..3dadd7f8 100755 --- a/inst/tinytest/test-first_isolate.R +++ b/inst/tinytest/test-first_isolate.R @@ -52,8 +52,8 @@ expect_equal( first_isolate(example_isolates, col_mo = "mo", col_date = "date", - col_patient_id = "patient_id", - col_icu = "ward_icu", + col_patient_id = "patient", + col_icu = example_isolates$ward == "ICU", info = TRUE, icu_exclude = TRUE), na.rm = TRUE), @@ -67,7 +67,7 @@ x[random_rows, "specimen"] <- "Urine" expect_true( sum(first_isolate(x = x, col_date = "date", - col_patient_id = "patient_id", + col_patient_id = "patient", col_mo = "mo", col_specimen = "specimen", filter_specimen = "Urine", @@ -76,11 +76,11 @@ expect_true( expect_true( sum(first_isolate(x = x, col_date = "date", - col_patient_id = "patient_id", + col_patient_id = "patient", col_mo = "mo", col_specimen = "specimen", filter_specimen = "Urine", - col_icu = "ward_icu", + col_icu = x$ward == "ICU", icu_exclude = TRUE, info = TRUE), na.rm = TRUE) < 1501) @@ -89,7 +89,7 @@ test_iso <- example_isolates test_iso$specimen <- "test" expect_message(first_isolate(test_iso, "date", - "patient_id", + "patient", col_mo = "mo", col_specimen = "specimen", filter_specimen = "something_unexisting", @@ -99,13 +99,13 @@ expect_message(first_isolate(test_iso, expect_message(first_isolate(example_isolates, col_date = "date", col_mo = "mo", - col_patient_id = "patient_id", + col_patient_id = "patient", col_testcode = "gender", testcodes_exclude = "M", info = TRUE)) # errors -expect_error(first_isolate("date", "patient_id", col_mo = "mo")) +expect_error(first_isolate("date", "patient", col_mo = "mo")) expect_error(first_isolate(example_isolates, col_date = "non-existing col", col_mo = "mo")) @@ -116,12 +116,12 @@ if (AMR:::pkg_is_available("dplyr", min_version = "1.0.0")) { mutate(mo = as.character(mo)) %>% first_isolate(col_date = "date", col_mo = "mo", - col_patient_id = "patient_id", + col_patient_id = "patient", info = FALSE), example_isolates %>% first_isolate(col_date = "date", col_mo = "mo", - col_patient_id = "patient_id", + col_patient_id = "patient", info = FALSE)) # support for WHONET @@ -133,8 +133,8 @@ if (AMR:::pkg_is_available("dplyr", min_version = "1.0.0")) { first_isolate(info = TRUE)) # groups - x <- example_isolates %>% group_by(ward_icu) %>% mutate(first = first_isolate()) - y <- example_isolates %>% group_by(ward_icu) %>% mutate(first = first_isolate(.)) + x <- example_isolates %>% group_by(ward) %>% mutate(first = first_isolate()) + y <- example_isolates %>% group_by(ward) %>% mutate(first = first_isolate(.)) expect_identical(x, y) } @@ -146,7 +146,7 @@ expect_equal( sum( first_isolate(x = df, col_date = "date", - col_patient_id = "patient_id", + col_patient_id = "patient", col_mo = "mo", info = TRUE), na.rm = TRUE), diff --git a/inst/tinytest/test-key_antimicrobials.R b/inst/tinytest/test-key_antimicrobials.R index a31c80d6..811e6b43 100644 --- a/inst/tinytest/test-key_antimicrobials.R +++ b/inst/tinytest/test-key_antimicrobials.R @@ -33,4 +33,4 @@ expect_false(antimicrobials_equal("SSS", "SIS", ignore_I = FALSE, type = "keyant expect_true(antimicrobials_equal(".SS", "SI.", ignore_I = TRUE, type = "keyantimicrobials")) expect_false(antimicrobials_equal(".SS", "SI.", ignore_I = FALSE, type = "keyantimicrobials")) -expect_warning(key_antimicrobials(example_isolates[rep(1, 10), ])) +expect_warning(key_antimicrobials(example_isolates[rep(1, 10), , drop = FALSE])) diff --git a/inst/tinytest/test-mdro.R b/inst/tinytest/test-mdro.R index 24c88c54..1a67009c 100755 --- a/inst/tinytest/test-mdro.R +++ b/inst/tinytest/test-mdro.R @@ -235,6 +235,6 @@ expect_warning(mdro(example_isolates, # print groups if (AMR:::pkg_is_available("dplyr", min_version = "1.0.0")) { - expect_stdout(x <- mdro(example_isolates %>% group_by(hospital_id), info = TRUE)) - expect_stdout(x <- mdro(example_isolates %>% group_by(hospital_id), guideline = custom, info = TRUE)) + expect_stdout(x <- mdro(example_isolates %>% group_by(ward), info = TRUE)) + expect_stdout(x <- mdro(example_isolates %>% group_by(ward), guideline = custom, info = TRUE)) } diff --git a/inst/tinytest/test-mo.R b/inst/tinytest/test-mo.R index e23f88a7..8083cf2a 100644 --- a/inst/tinytest/test-mo.R +++ b/inst/tinytest/test-mo.R @@ -147,7 +147,8 @@ expect_identical(as.character(as.mo("S. salivarius", Lancefield = TRUE)), "B_S if (AMR:::pkg_is_available("dplyr", min_version = "1.0.0")) { # select with one column expect_identical( - example_isolates[1:10, ] %>% + example_isolates %>% + slice(1:10) %>% left_join_microorganisms() %>% select(genus) %>% as.mo() %>% @@ -157,9 +158,11 @@ if (AMR:::pkg_is_available("dplyr", min_version = "1.0.0")) { # select with two columns expect_identical( - example_isolates[1:10, ] %>% + example_isolates %>% + slice(1:10) %>% pull(mo), - example_isolates[1:10, ] %>% + example_isolates %>% + slice(1:10) %>% left_join_microorganisms() %>% select(genus, species) %>% as.mo()) diff --git a/inst/tinytest/test-proportion.R b/inst/tinytest/test-proportion.R index a613fc18..bfc0e758 100755 --- a/inst/tinytest/test-proportion.R +++ b/inst/tinytest/test-proportion.R @@ -48,7 +48,7 @@ if (AMR:::pkg_is_available("dplyr", min_version = "1.0.0")) { # percentages expect_equal(example_isolates %>% - group_by(hospital_id) %>% + group_by(ward) %>% summarise(R = proportion_R(CIP, as_percent = TRUE), I = proportion_I(CIP, as_percent = TRUE), S = proportion_S(CIP, as_percent = TRUE), @@ -60,7 +60,7 @@ if (AMR:::pkg_is_available("dplyr", min_version = "1.0.0")) { # count of cases expect_equal(example_isolates %>% - group_by(hospital_id) %>% + group_by(ward) %>% summarise(cipro_p = proportion_SI(CIP, as_percent = TRUE), cipro_n = n_rsi(CIP), genta_p = proportion_SI(GEN, as_percent = TRUE), @@ -122,4 +122,4 @@ expect_warning(proportion_R(as.character(example_isolates$GEN))) expect_warning(proportion_I(as.character(example_isolates$GEN))) expect_warning(proportion_S(example_isolates$AMC, as.character(example_isolates$GEN))) expect_error(proportion_df(c("A", "B", "C"))) -expect_error(proportion_df(example_isolates[, "date"])) +expect_error(proportion_df(example_isolates[, "date", drop = TRUE])) diff --git a/man/AMR.Rd b/man/AMR.Rd index 3b4eb29b..8ff052d3 100644 --- a/man/AMR.Rd +++ b/man/AMR.Rd @@ -35,19 +35,14 @@ This package can be used for: } \section{Reference Data Publicly Available}{ -All reference data sets (about microorganisms, antibiotics, R/SI interpretation, EUCAST rules, etc.) in this \code{AMR} package are publicly and freely available. We continually export our data sets to formats for use in R, SPSS, SAS, Stata and Excel. We also supply flat files that are machine-readable and suitable for input in any software program, such as laboratory information systems. Please find \href{https://msberends.github.io/AMR/articles/datasets.html}{all download links on our website}, which is automatically updated with every code change. -} - -\section{Read more on Our Website!}{ - -On our website \url{https://msberends.github.io/AMR/} you can find \href{https://msberends.github.io/AMR/articles/AMR.html}{a comprehensive tutorial} about how to conduct AMR data analysis, the \href{https://msberends.github.io/AMR/reference/}{complete documentation of all functions} and \href{https://msberends.github.io/AMR/articles/WHONET.html}{an example analysis using WHONET data}. +All data sets in this \code{AMR} package (about microorganisms, antibiotics, R/SI interpretation, EUCAST rules, etc.) are publicly and freely available for download in the following formats: R, MS Excel, Apache Feather, Apache Parquet, SPSS, SAS, and Stata. We also provide tab-separated plain text files that are machine-readable and suitable for input in any software program, such as laboratory information systems. Please visit \href{https://msberends.github.io/AMR/articles/datasets.html}{our website for the download links}. The actual files are of course available on \href{https://github.com/msberends/AMR/tree/main/data-raw}{our GitHub repository}. } \section{Contact Us}{ -For suggestions, comments or questions, please contact us at: +For suggestions, comments or questions, please contact us via: -Matthijs S. Berends \cr +Dr. Matthijs S. Berends \cr m.s.berends [at] umcg [dot] nl \cr University of Groningen Department of Medical Microbiology and Infection Prevention \cr diff --git a/man/WHONET.Rd b/man/WHONET.Rd index 87152716..ddebd0b1 100644 --- a/man/WHONET.Rd +++ b/man/WHONET.Rd @@ -5,7 +5,7 @@ \alias{WHONET} \title{Data Set with 500 Isolates - WHONET Example} \format{ -A \link{data.frame} with 500 observations and 53 variables: +A [tibble\link[tibble:tibble]{tibble::tibble} with 500 observations and 53 variables: \itemize{ \item \verb{Identification number}\cr ID of the sample \item \verb{Specimen number}\cr ID of the specimen @@ -41,12 +41,10 @@ WHONET \description{ This example data set has the exact same structure as an export file from WHONET. Such files can be used with this package, as this example data set shows. The antibiotic results are from our \link{example_isolates} data set. All patient names are created using online surname generators and are only in place for practice purposes. } -\section{Reference Data Publicly Available}{ - -All reference data sets (about microorganisms, antibiotics, R/SI interpretation, EUCAST rules, etc.) in this \code{AMR} package are publicly and freely available. We continually export our data sets to formats for use in R, SPSS, SAS, Stata and Excel. We also supply flat files that are machine-readable and suitable for input in any software program, such as laboratory information systems. Please find \href{https://msberends.github.io/AMR/articles/datasets.html}{all download links on our website}, which is automatically updated with every code change. +\details{ +Like all data sets in this package, this data set is publicly available for download in the following formats: R, MS Excel, Apache Feather, Apache Parquet, SPSS, SAS, and Stata. Please visit \href{https://msberends.github.io/AMR/articles/datasets.html}{our website for the download links}. The actual files are of course available on \href{https://github.com/msberends/AMR/tree/main/data-raw}{our GitHub repository}. } - \examples{ -head(WHONET) +WHONET } \keyword{datasets} diff --git a/man/ab_property.Rd b/man/ab_property.Rd index 36af484c..96c611de 100644 --- a/man/ab_property.Rd +++ b/man/ab_property.Rd @@ -104,7 +104,7 @@ European Commission Public Health PHARMACEUTICALS - COMMUNITY REGISTER: \url{htt \section{Reference Data Publicly Available}{ -All reference data sets (about microorganisms, antibiotics, R/SI interpretation, EUCAST rules, etc.) in this \code{AMR} package are publicly and freely available. We continually export our data sets to formats for use in R, SPSS, SAS, Stata and Excel. We also supply flat files that are machine-readable and suitable for input in any software program, such as laboratory information systems. Please find \href{https://msberends.github.io/AMR/articles/datasets.html}{all download links on our website}, which is automatically updated with every code change. +All data sets in this \code{AMR} package (about microorganisms, antibiotics, R/SI interpretation, EUCAST rules, etc.) are publicly and freely available for download in the following formats: R, MS Excel, Apache Feather, Apache Parquet, SPSS, SAS, and Stata. We also provide tab-separated plain text files that are machine-readable and suitable for input in any software program, such as laboratory information systems. Please visit \href{https://msberends.github.io/AMR/articles/datasets.html}{our website for the download links}. The actual files are of course available on \href{https://github.com/msberends/AMR/tree/main/data-raw}{our GitHub repository}. } \examples{ diff --git a/man/age_groups.Rd b/man/age_groups.Rd index 047cb9f4..0260aa19 100644 --- a/man/age_groups.Rd +++ b/man/age_groups.Rd @@ -59,7 +59,7 @@ age_groups(ages, "children") if (require("dplyr")) { example_isolates \%>\% filter_first_isolate() \%>\% - filter(mo == as.mo("E. coli")) \%>\% + filter(mo == as.mo("Escherichia coli")) \%>\% group_by(age_group = age_groups(age)) \%>\% select(age_group, CIP) \%>\% ggplot_rsi(x = "age_group", diff --git a/man/antibiotic_class_selectors.Rd b/man/antibiotic_class_selectors.Rd index 707fba72..deb9a945 100644 --- a/man/antibiotic_class_selectors.Rd +++ b/man/antibiotic_class_selectors.Rd @@ -166,121 +166,153 @@ The \code{\link[=not_intrinsic_resistant]{not_intrinsic_resistant()}} function c \section{Reference Data Publicly Available}{ -All reference data sets (about microorganisms, antibiotics, R/SI interpretation, EUCAST rules, etc.) in this \code{AMR} package are publicly and freely available. We continually export our data sets to formats for use in R, SPSS, SAS, Stata and Excel. We also supply flat files that are machine-readable and suitable for input in any software program, such as laboratory information systems. Please find \href{https://msberends.github.io/AMR/articles/datasets.html}{all download links on our website}, which is automatically updated with every code change. +All data sets in this \code{AMR} package (about microorganisms, antibiotics, R/SI interpretation, EUCAST rules, etc.) are publicly and freely available for download in the following formats: R, MS Excel, Apache Feather, Apache Parquet, SPSS, SAS, and Stata. We also provide tab-separated plain text files that are machine-readable and suitable for input in any software program, such as laboratory information systems. Please visit \href{https://msberends.github.io/AMR/articles/datasets.html}{our website for the download links}. The actual files are of course available on \href{https://github.com/msberends/AMR/tree/main/data-raw}{our GitHub repository}. } \examples{ # `example_isolates` is a data set available in the AMR package. # See ?example_isolates. -df <- example_isolates[ , c("hospital_id", "mo", - "AMP", "AMC", "TZP", "CXM", "CRO", "GEN", - "TOB", "COL", "IPM", "MEM", "TEC", "VAN")] +example_isolates # base R ------------------------------------------------------------------ # select columns 'IPM' (imipenem) and 'MEM' (meropenem) -df[, carbapenems()] +example_isolates[, carbapenems()] # select columns 'mo', 'AMK', 'GEN', 'KAN' and 'TOB' -df[, c("mo", aminoglycosides())] +example_isolates[, c("mo", aminoglycosides())] # select only antibiotic columns with DDDs for oral treatment -df[, administrable_per_os()] +example_isolates[, administrable_per_os()] # filter using any() or all() -df[any(carbapenems() == "R"), ] -subset(df, any(carbapenems() == "R")) +example_isolates[any(carbapenems() == "R"), ] +subset(example_isolates, any(carbapenems() == "R")) # filter on any or all results in the carbapenem columns (i.e., IPM, MEM): -df[any(carbapenems()), ] -df[all(carbapenems()), ] +example_isolates[any(carbapenems()), ] +example_isolates[all(carbapenems()), ] # filter with multiple antibiotic selectors using c() -df[all(c(carbapenems(), aminoglycosides()) == "R"), ] +example_isolates[all(c(carbapenems(), aminoglycosides()) == "R"), ] # filter + select in one go: get penicillins in carbapenems-resistant strains -df[any(carbapenems() == "R"), penicillins()] +example_isolates[any(carbapenems() == "R"), penicillins()] # You can combine selectors with '&' to be more specific. For example, # penicillins() would select benzylpenicillin ('peni G') and # administrable_per_os() would select erythromycin. Yet, when combined these # drugs are both omitted since benzylpenicillin is not administrable per os # and erythromycin is not a penicillin: -df[, penicillins() & administrable_per_os()] +example_isolates[, penicillins() & administrable_per_os()] # ab_selector() applies a filter in the `antibiotics` data set and is thus very # flexible. For instance, to select antibiotic columns with an oral DDD of at # least 1 gram: -df[, ab_selector(oral_ddd > 1 & oral_units == "g")] +example_isolates[, ab_selector(oral_ddd > 1 & oral_units == "g")] # dplyr ------------------------------------------------------------------- \donttest{ if (require("dplyr")) { - # get AMR for all aminoglycosides e.g., per hospital: - df \%>\% - group_by(hospital_id) \%>\% + # get AMR for all aminoglycosides e.g., per ward: + example_isolates \%>\% + group_by(ward) \%>\% summarise(across(aminoglycosides(), resistance)) +} +if (require("dplyr")) { + # You can combine selectors with '&' to be more specific: - df \%>\% + example_isolates \%>\% select(penicillins() & administrable_per_os()) + +} +if (require("dplyr")) { # get AMR for only drugs that matter - no intrinsic resistance: - df \%>\% + example_isolates \%>\% filter(mo_genus() \%in\% c("Escherichia", "Klebsiella")) \%>\% - group_by(hospital_id) \%>\% + group_by(ward) \%>\% summarise(across(not_intrinsic_resistant(), resistance)) +} +if (require("dplyr")) { + # get susceptibility for antibiotics whose name contains "trim": - df \%>\% + example_isolates \%>\% filter(first_isolate()) \%>\% - group_by(hospital_id) \%>\% + group_by(ward) \%>\% summarise(across(ab_selector(name \%like\% "trim"), susceptibility)) + +} +if (require("dplyr")) { # this will select columns 'IPM' (imipenem) and 'MEM' (meropenem): - df \%>\% + example_isolates \%>\% select(carbapenems()) +} +if (require("dplyr")) { + # this will select columns 'mo', 'AMK', 'GEN', 'KAN' and 'TOB': - df \%>\% + example_isolates \%>\% select(mo, aminoglycosides()) +} +if (require("dplyr")) { + # any() and all() work in dplyr's filter() too: - df \%>\% + example_isolates \%>\% filter(any(aminoglycosides() == "R"), all(cephalosporins_2nd() == "R")) +} +if (require("dplyr")) { + # also works with c(): - df \%>\% + example_isolates \%>\% filter(any(c(carbapenems(), aminoglycosides()) == "R")) +} +if (require("dplyr")) { + # not setting any/all will automatically apply all(): - df \%>\% + example_isolates \%>\% filter(aminoglycosides() == "R") +} +if (require("dplyr")) { + # this will select columns 'mo' and all antimycobacterial drugs ('RIF'): - df \%>\% + example_isolates \%>\% select(mo, ab_class("mycobact")) +} +if (require("dplyr")) { + # get bug/drug combinations for only glycopeptides in Gram-positives: - df \%>\% + example_isolates \%>\% filter(mo_is_gram_positive()) \%>\% select(mo, glycopeptides()) \%>\% bug_drug_combinations() \%>\% format() +} +if (require("dplyr")) { + data.frame(some_column = "some_value", J01CA01 = "S") \%>\% # ATC code of ampicillin select(penicillins()) # only the 'J01CA01' column will be selected - +} +if (require("dplyr")) { + # with recent versions of dplyr this is all equal: - x <- df[carbapenems() == "R", ] - y <- df \%>\% filter(carbapenems() == "R") - z <- df \%>\% filter(if_all(carbapenems(), ~.x == "R")) - identical(x, y) - identical(y, z) + x <- example_isolates[carbapenems() == "R", ] + y <- example_isolates \%>\% filter(carbapenems() == "R") + z <- example_isolates \%>\% filter(if_all(carbapenems(), ~.x == "R")) + identical(x, y) && identical(y, z) } } } diff --git a/man/antibiotics.Rd b/man/antibiotics.Rd index 5fc10a20..3e4313ee 100644 --- a/man/antibiotics.Rd +++ b/man/antibiotics.Rd @@ -6,7 +6,7 @@ \alias{antivirals} \title{Data Sets with 566 Antimicrobial Drugs} \format{ -\subsection{For the \link{antibiotics} data set: a \link{data.frame} with 464 observations and 14 variables:}{ +\subsection{For the \link{antibiotics} data set: a [tibble\link[tibble:tibble]{tibble::tibble} with 464 observations and 14 variables:}{ \itemize{ \item \code{ab}\cr Antibiotic ID as used in this package (such as \code{AMC}), using the official EARS-Net (European Antimicrobial Resistance Surveillance Network) codes where available \item \code{cid}\cr Compound ID as found in PubChem @@ -25,7 +25,7 @@ } } -\subsection{For the \link{antivirals} data set: a \link{data.frame} with 102 observations and 9 variables:}{ +\subsection{For the \link{antivirals} data set: a [tibble\link[tibble:tibble]{tibble::tibble} with 102 observations and 9 variables:}{ \itemize{ \item \code{atc}\cr ATC codes (Anatomical Therapeutic Chemical) as defined by the WHOCC \item \code{cid}\cr Compound ID as found in PubChem @@ -39,7 +39,7 @@ } } -An object of class \code{data.frame} with 102 rows and 9 columns. +An object of class \code{tbl_df} (inherits from \code{tbl}, \code{data.frame}) with 102 rows and 9 columns. } \source{ World Health Organization (WHO) Collaborating Centre for Drug Statistics Methodology (WHOCC): \url{https://www.whocc.no/atc_ddd_index/} @@ -60,24 +60,9 @@ Properties that are based on an ATC code are only available when an ATC is avail Synonyms (i.e. trade names) were derived from the Compound ID (\code{cid}) and consequently only available where a CID is available. \subsection{Direct download}{ -These data sets are available as 'flat files' for use even without \R - you can find the files here: -\itemize{ -\item \url{https://github.com/msberends/AMR/raw/main/data-raw/antibiotics.txt} -\item \url{https://github.com/msberends/AMR/raw/main/data-raw/antivirals.txt} -} - -Files in \R format (with preserved data structure) can be found here: -\itemize{ -\item \url{https://github.com/msberends/AMR/raw/main/data/antibiotics.rda} -\item \url{https://github.com/msberends/AMR/raw/main/data/antivirals.rda} +Like all data sets in this package, these data sets are publicly available for download in the following formats: R, MS Excel, Apache Feather, Apache Parquet, SPSS, SAS, and Stata. Please visit \href{https://msberends.github.io/AMR/articles/datasets.html}{our website for the download links}. The actual files are of course available on \href{https://github.com/msberends/AMR/tree/main/data-raw}{our GitHub repository}. } } -} -\section{Reference Data Publicly Available}{ - -All reference data sets (about microorganisms, antibiotics, R/SI interpretation, EUCAST rules, etc.) in this \code{AMR} package are publicly and freely available. We continually export our data sets to formats for use in R, SPSS, SAS, Stata and Excel. We also supply flat files that are machine-readable and suitable for input in any software program, such as laboratory information systems. Please find \href{https://msberends.github.io/AMR/articles/datasets.html}{all download links on our website}, which is automatically updated with every code change. -} - \section{WHOCC}{ \if{html}{\figure{logo_who.png}{options: height="60" style=margin-bottom:"5"} \cr} @@ -91,8 +76,8 @@ The WHOCC is located in Oslo at the Norwegian Institute of Public Health and fun } \examples{ -head(antibiotics) -head(antivirals) +antibiotics +antivirals } \seealso{ \link{microorganisms}, \link{intrinsic_resistant} diff --git a/man/as.ab.Rd b/man/as.ab.Rd index 0eb5c663..8380feed 100644 --- a/man/as.ab.Rd +++ b/man/as.ab.Rd @@ -61,7 +61,7 @@ The WHOCC is located in Oslo at the Norwegian Institute of Public Health and fun \section{Reference Data Publicly Available}{ -All reference data sets (about microorganisms, antibiotics, R/SI interpretation, EUCAST rules, etc.) in this \code{AMR} package are publicly and freely available. We continually export our data sets to formats for use in R, SPSS, SAS, Stata and Excel. We also supply flat files that are machine-readable and suitable for input in any software program, such as laboratory information systems. Please find \href{https://msberends.github.io/AMR/articles/datasets.html}{all download links on our website}, which is automatically updated with every code change. +All data sets in this \code{AMR} package (about microorganisms, antibiotics, R/SI interpretation, EUCAST rules, etc.) are publicly and freely available for download in the following formats: R, MS Excel, Apache Feather, Apache Parquet, SPSS, SAS, and Stata. We also provide tab-separated plain text files that are machine-readable and suitable for input in any software program, such as laboratory information systems. Please visit \href{https://msberends.github.io/AMR/articles/datasets.html}{our website for the download links}. The actual files are of course available on \href{https://github.com/msberends/AMR/tree/main/data-raw}{our GitHub repository}. } \examples{ diff --git a/man/as.mic.Rd b/man/as.mic.Rd index 09c9fc53..2e3597d2 100755 --- a/man/as.mic.Rd +++ b/man/as.mic.Rd @@ -103,9 +103,16 @@ as.rsi(x = as.mic(c(0.01, 2, 4, 8)), # plot MIC values, see ?plot plot(mic_data) plot(mic_data, mo = "E. coli", ab = "cipro") -autoplot(mic_data, mo = "E. coli", ab = "cipro") -autoplot(mic_data, mo = "E. coli", ab = "cipro", language = "nl") # Dutch -autoplot(mic_data, mo = "E. coli", ab = "cipro", language = "uk") # Ukrainian + +if (require("ggplot2")) { + autoplot(mic_data, mo = "E. coli", ab = "cipro") +} +if (require("ggplot2")) { + autoplot(mic_data, mo = "E. coli", ab = "cipro", language = "nl") # Dutch +} +if (require("ggplot2")) { + autoplot(mic_data, mo = "E. coli", ab = "cipro", language = "uk") # Ukrainian +} } \seealso{ \code{\link[=as.rsi]{as.rsi()}} diff --git a/man/as.mo.Rd b/man/as.mo.Rd index 512a6864..a0b0ce2e 100644 --- a/man/as.mo.Rd +++ b/man/as.mo.Rd @@ -173,7 +173,7 @@ This package contains the complete taxonomic tree of almost all microorganisms ( \section{Reference Data Publicly Available}{ -All reference data sets (about microorganisms, antibiotics, R/SI interpretation, EUCAST rules, etc.) in this \code{AMR} package are publicly and freely available. We continually export our data sets to formats for use in R, SPSS, SAS, Stata and Excel. We also supply flat files that are machine-readable and suitable for input in any software program, such as laboratory information systems. Please find \href{https://msberends.github.io/AMR/articles/datasets.html}{all download links on our website}, which is automatically updated with every code change. +All data sets in this \code{AMR} package (about microorganisms, antibiotics, R/SI interpretation, EUCAST rules, etc.) are publicly and freely available for download in the following formats: R, MS Excel, Apache Feather, Apache Parquet, SPSS, SAS, and Stata. We also provide tab-separated plain text files that are machine-readable and suitable for input in any software program, such as laboratory information systems. Please visit \href{https://msberends.github.io/AMR/articles/datasets.html}{our website for the download links}. The actual files are of course available on \href{https://github.com/msberends/AMR/tree/main/data-raw}{our GitHub repository}. } \examples{ diff --git a/man/as.rsi.Rd b/man/as.rsi.Rd index ef2ad806..ceeaafb4 100755 --- a/man/as.rsi.Rd +++ b/man/as.rsi.Rd @@ -156,7 +156,7 @@ This AMR package honours this (new) insight. Use \code{\link[=susceptibility]{su \section{Reference Data Publicly Available}{ -All reference data sets (about microorganisms, antibiotics, R/SI interpretation, EUCAST rules, etc.) in this \code{AMR} package are publicly and freely available. We continually export our data sets to formats for use in R, SPSS, SAS, Stata and Excel. We also supply flat files that are machine-readable and suitable for input in any software program, such as laboratory information systems. Please find \href{https://msberends.github.io/AMR/articles/datasets.html}{all download links on our website}, which is automatically updated with every code change. +All data sets in this \code{AMR} package (about microorganisms, antibiotics, R/SI interpretation, EUCAST rules, etc.) are publicly and freely available for download in the following formats: R, MS Excel, Apache Feather, Apache Parquet, SPSS, SAS, and Stata. We also provide tab-separated plain text files that are machine-readable and suitable for input in any software program, such as laboratory information systems. Please visit \href{https://msberends.github.io/AMR/articles/datasets.html}{our website for the download links}. The actual files are of course available on \href{https://github.com/msberends/AMR/tree/main/data-raw}{our GitHub repository}. } \examples{ diff --git a/man/availability.Rd b/man/availability.Rd index 8338b8c4..1da7df38 100644 --- a/man/availability.Rd +++ b/man/availability.Rd @@ -25,7 +25,7 @@ availability(example_isolates) \donttest{ if (require("dplyr")) { example_isolates \%>\% - filter(mo == as.mo("E. coli")) \%>\% + filter(mo == as.mo("Escherichia coli")) \%>\% select_if(is.rsi) \%>\% availability() } diff --git a/man/bug_drug_combinations.Rd b/man/bug_drug_combinations.Rd index 44605858..d5904224 100644 --- a/man/bug_drug_combinations.Rd +++ b/man/bug_drug_combinations.Rd @@ -25,7 +25,7 @@ bug_drug_combinations(x, col_mo = NULL, FUN = mo_shortname, ...) ) } \arguments{ -\item{x}{data with antibiotic columns, such as \code{amox}, \code{AMX} and \code{AMC}} +\item{x}{a data set with antibiotic columns, such as \code{amox}, \code{AMX} and \code{AMC}} \item{col_mo}{column name of the IDs of the microorganisms (see \code{\link[=as.mo]{as.mo()}}), defaults to the first column of class \code{\link{mo}}. Values will be coerced using \code{\link[=as.mo]{as.mo()}}.} @@ -74,7 +74,7 @@ bug_drug_combinations(example_isolates, FUN = mo_gramstain) bug_drug_combinations(example_isolates, - FUN = function(x) ifelse(x == as.mo("E. coli"), + FUN = function(x) ifelse(x == as.mo("Escherichia coli"), "E. coli", "Others")) } diff --git a/man/catalogue_of_life.Rd b/man/catalogue_of_life.Rd index 65da2bc1..2c87b3ae 100644 --- a/man/catalogue_of_life.Rd +++ b/man/catalogue_of_life.Rd @@ -40,13 +40,13 @@ catalogue_of_life_version() mo_shortname("Chlamydophila psittaci") # Get any property from the entire taxonomic tree for all included species -mo_class("E. coli") +mo_class("Escherichia coli") -mo_family("E. coli") +mo_family("Escherichia coli") -mo_gramstain("E. coli") # based on kingdom and phylum, see ?mo_gramstain +mo_gramstain("Escherichia coli") # based on kingdom and phylum, see ?mo_gramstain -mo_ref("E. coli") +mo_ref("Escherichia coli") # Do not get mistaken - this package is about microorganisms mo_kingdom("C. elegans") diff --git a/man/count.Rd b/man/count.Rd index e68a6371..ac308825 100644 --- a/man/count.Rd +++ b/man/count.Rd @@ -155,7 +155,7 @@ susceptibility(example_isolates$AMX) * n_rsi(example_isolates$AMX) \donttest{ if (require("dplyr")) { example_isolates \%>\% - group_by(hospital_id) \%>\% + group_by(ward) \%>\% summarise(R = count_R(CIP), I = count_I(CIP), S = count_S(CIP), @@ -166,7 +166,7 @@ if (require("dplyr")) { # Number of available isolates for a whole antibiotic class # (i.e., in this data set columns GEN, TOB, AMK, KAN) example_isolates \%>\% - group_by(hospital_id) \%>\% + group_by(ward) \%>\% summarise(across(aminoglycosides(), n_rsi)) # Count co-resistance between amoxicillin/clav acid and gentamicin, @@ -188,8 +188,8 @@ if (require("dplyr")) { # It also supports grouping variables example_isolates \%>\% - select(hospital_id, AMX, CIP) \%>\% - group_by(hospital_id) \%>\% + select(ward, AMX, CIP) \%>\% + group_by(ward) \%>\% count_df(translate = FALSE) } } diff --git a/man/custom_eucast_rules.Rd b/man/custom_eucast_rules.Rd index 294d4fe3..5fb7b114 100644 --- a/man/custom_eucast_rules.Rd +++ b/man/custom_eucast_rules.Rd @@ -7,7 +7,7 @@ custom_eucast_rules(...) } \arguments{ -\item{...}{rules in \link[=`~`]{formula} notation, see \emph{Examples}} +\item{...}{rules in \link[base:tilde]{formula} notation, see \emph{Examples}} } \value{ A \link{list} containing the custom rules diff --git a/man/dosage.Rd b/man/dosage.Rd index 27954555..cfe0a79e 100644 --- a/man/dosage.Rd +++ b/man/dosage.Rd @@ -5,7 +5,7 @@ \alias{dosage} \title{Data Set with Treatment Dosages as Defined by EUCAST} \format{ -A \link{data.frame} with 169 observations and 9 variables: +A [tibble\link[tibble:tibble]{tibble::tibble} with 169 observations and 9 variables: \itemize{ \item \code{ab}\cr Antibiotic ID as used in this package (such as \code{AMC}), using the official EARS-Net (European Antimicrobial Resistance Surveillance Network) codes where available \item \code{name}\cr Official name of the antimicrobial agent as used by WHONET/EARS-Net or the WHO @@ -25,14 +25,13 @@ dosage EUCAST breakpoints used in this package are based on the dosages in this data set. They can be retrieved with \code{\link[=eucast_dosage]{eucast_dosage()}}. } \details{ -\href{https://www.eucast.org/clinical_breakpoints/}{'EUCAST Clinical Breakpoint Tables' v11.0} (2021) are based on the dosages in this data set. -} -\section{Reference Data Publicly Available}{ +This data set is based on \href{https://www.eucast.org/clinical_breakpoints/}{'EUCAST Clinical Breakpoint Tables' v11.0} (2021). +\subsection{Direct download}{ -All reference data sets (about microorganisms, antibiotics, R/SI interpretation, EUCAST rules, etc.) in this \code{AMR} package are publicly and freely available. We continually export our data sets to formats for use in R, SPSS, SAS, Stata and Excel. We also supply flat files that are machine-readable and suitable for input in any software program, such as laboratory information systems. Please find \href{https://msberends.github.io/AMR/articles/datasets.html}{all download links on our website}, which is automatically updated with every code change. +Like all data sets in this package, this data set is publicly available for download in the following formats: R, MS Excel, Apache Feather, Apache Parquet, SPSS, SAS, and Stata. Please visit \href{https://msberends.github.io/AMR/articles/datasets.html}{our website for the download links}. The actual files are of course available on \href{https://github.com/msberends/AMR/tree/main/data-raw}{our GitHub repository}. +} } - \examples{ -head(dosage) +dosage } \keyword{datasets} diff --git a/man/eucast_rules.Rd b/man/eucast_rules.Rd index 7e177347..de880742 100644 --- a/man/eucast_rules.Rd +++ b/man/eucast_rules.Rd @@ -36,7 +36,7 @@ eucast_rules( eucast_dosage(ab, administration = "iv", version_breakpoints = 11) } \arguments{ -\item{x}{data with antibiotic columns, such as \code{amox}, \code{AMX} and \code{AMC}} +\item{x}{a data set with antibiotic columns, such as \code{amox}, \code{AMX} and \code{AMC}} \item{col_mo}{column name of the IDs of the microorganisms (see \code{\link[=as.mo]{as.mo()}}), defaults to the first column of class \code{\link{mo}}. Values will be coerced using \code{\link[=as.mo]{as.mo()}}.} @@ -83,26 +83,26 @@ Custom rules can be created using \code{\link[=custom_eucast_rules]{custom_eucas AMC == "I" & genus == "Klebsiella" ~ aminopenicillins == "I") eucast_rules(example_isolates, rules = "custom", custom_rules = x, info = FALSE) -#> # A tibble: 2,000 x 49 -#> date hospit~1 ward_~2 ward_~3 ward_~4 age gender patie~5 mo -#> * -#> 1 2002-01-02 D FALSE TRUE FALSE 65 F A77334 B_ESCHR_COLI -#> 2 2002-01-03 D FALSE TRUE FALSE 65 F A77334 B_ESCHR_COLI -#> 3 2002-01-07 B TRUE FALSE FALSE 45 F 067927 B_STPHY_EPDR -#> 4 2002-01-07 B TRUE FALSE FALSE 45 F 067927 B_STPHY_EPDR -#> 5 2002-01-13 B TRUE FALSE FALSE 45 F 067927 B_STPHY_EPDR -#> 6 2002-01-13 B TRUE FALSE FALSE 45 F 067927 B_STPHY_EPDR -#> 7 2002-01-14 D FALSE TRUE FALSE 78 M 462729 B_STPHY_AURS -#> 8 2002-01-14 D FALSE TRUE FALSE 78 M 462729 B_STPHY_AURS -#> 9 2002-01-16 B TRUE FALSE FALSE 45 F 067927 B_STPHY_EPDR -#> 10 2002-01-17 B TRUE FALSE FALSE 79 F 858515 B_STPHY_EPDR -#> # ... with 1,990 more rows, 40 more variables: PEN , OXA , FLC , -#> # AMX , AMC , AMP , TZP , CZO , FEP , -#> # CXM , FOX , CTX , CAZ , CRO , GEN , -#> # TOB , AMK , KAN , TMP , SXT , NIT , -#> # FOS , LNZ , CIP , MFX , VAN , TEC , -#> # TCY , TGC , DOX , ERY , CLI , AZM , -#> # IPM , MEM , MTR , CHL , COL , MUP , ... +#> # A tibble: 2,000 x 46 +#> date patient age gender ward mo PEN OXA FLC AMX +#> * +#> 1 2002-01-02 A77334 65 F Clinical B_ESCHR_COLI R NA NA NA +#> 2 2002-01-03 A77334 65 F Clinical B_ESCHR_COLI R NA NA NA +#> 3 2002-01-07 067927 45 F ICU B_STPHY_EPDR R NA R NA +#> 4 2002-01-07 067927 45 F ICU B_STPHY_EPDR R NA R NA +#> 5 2002-01-13 067927 45 F ICU B_STPHY_EPDR R NA R NA +#> 6 2002-01-13 067927 45 F ICU B_STPHY_EPDR R NA R NA +#> 7 2002-01-14 462729 78 M Clinical B_STPHY_AURS R NA S R +#> 8 2002-01-14 462729 78 M Clinical B_STPHY_AURS R NA S R +#> 9 2002-01-16 067927 45 F ICU B_STPHY_EPDR R NA R NA +#> 10 2002-01-17 858515 79 F ICU B_STPHY_EPDR R NA S NA +#> # ... with 1,990 more rows, and 36 more variables: AMC , AMP , +#> # TZP , CZO , FEP , CXM , FOX , CTX , +#> # CAZ , CRO , GEN , TOB , AMK , KAN , +#> # TMP , SXT , NIT , FOS , LNZ , CIP , +#> # MFX , VAN , TEC , TCY , TGC , DOX , +#> # ERY , CLI , AZM , IPM , MEM , MTR , +#> # CHL , COL , MUP , RIF #> # i Use `print(n = ...)` to see more rows, and `colnames()` to see all variable names }\if{html}{\out{}} } @@ -131,7 +131,7 @@ Amikacin (\code{AMK}, \href{https://www.whocc.no/atc_ddd_index/?code=J01GB06&sho \section{Reference Data Publicly Available}{ -All reference data sets (about microorganisms, antibiotics, R/SI interpretation, EUCAST rules, etc.) in this \code{AMR} package are publicly and freely available. We continually export our data sets to formats for use in R, SPSS, SAS, Stata and Excel. We also supply flat files that are machine-readable and suitable for input in any software program, such as laboratory information systems. Please find \href{https://msberends.github.io/AMR/articles/datasets.html}{all download links on our website}, which is automatically updated with every code change. +All data sets in this \code{AMR} package (about microorganisms, antibiotics, R/SI interpretation, EUCAST rules, etc.) are publicly and freely available for download in the following formats: R, MS Excel, Apache Feather, Apache Parquet, SPSS, SAS, and Stata. We also provide tab-separated plain text files that are machine-readable and suitable for input in any software program, such as laboratory information systems. Please visit \href{https://msberends.github.io/AMR/articles/datasets.html}{our website for the download links}. The actual files are of course available on \href{https://github.com/msberends/AMR/tree/main/data-raw}{our GitHub repository}. } \examples{ diff --git a/man/example_isolates.Rd b/man/example_isolates.Rd index 21e1ca37..fcb21288 100644 --- a/man/example_isolates.Rd +++ b/man/example_isolates.Rd @@ -5,32 +5,27 @@ \alias{example_isolates} \title{Data Set with 2,000 Example Isolates} \format{ -A \link{data.frame} with 2,000 observations and 49 variables: +A [tibble\link[tibble:tibble]{tibble::tibble} with 2,000 observations and 46 variables: \itemize{ -\item \code{date}\cr date of receipt at the laboratory -\item \code{hospital_id}\cr ID of the hospital, from A to D -\item \code{ward_icu}\cr \link{logical} to determine if ward is an intensive care unit -\item \code{ward_clinical}\cr \link{logical} to determine if ward is a regular clinical ward -\item \code{ward_outpatient}\cr \link{logical} to determine if ward is an outpatient clinic -\item \code{age}\cr age of the patient -\item \code{gender}\cr gender of the patient -\item \code{patient_id}\cr ID of the patient -\item \code{mo}\cr ID of microorganism created with \code{\link[=as.mo]{as.mo()}}, see also \link{microorganisms} -\item \code{PEN:RIF}\cr 40 different antibiotics with class \code{\link{rsi}} (see \code{\link[=as.rsi]{as.rsi()}}); these column names occur in the \link{antibiotics} data set and can be translated with \code{\link[=ab_name]{ab_name()}} +\item \code{date}\cr Date of receipt at the laboratory +\item \code{patient}\cr ID of the patient +\item \code{age}\cr Age of the patient +\item \code{gender}\cr Gender of the patient, either "F" or "M" +\item \code{ward}\cr Ward type where the patient was admitted, either "Clinical", "ICU" or "Outpatient" +\item \code{mo}\cr ID of microorganism created with \code{\link[=as.mo]{as.mo()}}, see also the \link{microorganisms} data set +\item \code{PEN:RIF}\cr 40 different antibiotics with class \code{\link{rsi}} (see \code{\link[=as.rsi]{as.rsi()}}); these column names occur in the \link{antibiotics} data set and can be translated with \code{\link[=set_ab_names]{set_ab_names()}} or \code{\link[=ab_name]{ab_name()}} } } \usage{ example_isolates } \description{ -A data set containing 2,000 microbial isolates with their full antibiograms. The data set reflects reality and can be used to practice AMR data analysis. For examples, please read \href{https://msberends.github.io/AMR/articles/AMR.html}{the tutorial on our website}. +A data set containing 2,000 microbial isolates with their full antibiograms. This data set contains randomised fictitious data, but reflects reality and can be used to practise AMR data analysis. For examples, please read \href{https://msberends.github.io/AMR/articles/AMR.html}{the tutorial on our website}. } -\section{Reference Data Publicly Available}{ - -All reference data sets (about microorganisms, antibiotics, R/SI interpretation, EUCAST rules, etc.) in this \code{AMR} package are publicly and freely available. We continually export our data sets to formats for use in R, SPSS, SAS, Stata and Excel. We also supply flat files that are machine-readable and suitable for input in any software program, such as laboratory information systems. Please find \href{https://msberends.github.io/AMR/articles/datasets.html}{all download links on our website}, which is automatically updated with every code change. +\details{ +Like all data sets in this package, this data set is publicly available for download in the following formats: R, MS Excel, Apache Feather, Apache Parquet, SPSS, SAS, and Stata. Please visit \href{https://msberends.github.io/AMR/articles/datasets.html}{our website for the download links}. The actual files are of course available on \href{https://github.com/msberends/AMR/tree/main/data-raw}{our GitHub repository}. } - \examples{ -head(example_isolates) +example_isolates } \keyword{datasets} diff --git a/man/example_isolates_unclean.Rd b/man/example_isolates_unclean.Rd index 81071f58..8fbe04fa 100644 --- a/man/example_isolates_unclean.Rd +++ b/man/example_isolates_unclean.Rd @@ -5,7 +5,7 @@ \alias{example_isolates_unclean} \title{Data Set with Unclean Data} \format{ -A \link{data.frame} with 3,000 observations and 8 variables: +A [tibble\link[tibble:tibble]{tibble::tibble} with 3,000 observations and 8 variables: \itemize{ \item \code{patient_id}\cr ID of the patient \item \code{date}\cr date of receipt at the laboratory @@ -20,12 +20,10 @@ example_isolates_unclean \description{ A data set containing 3,000 microbial isolates that are not cleaned up and consequently not ready for AMR data analysis. This data set can be used for practice. } -\section{Reference Data Publicly Available}{ - -All reference data sets (about microorganisms, antibiotics, R/SI interpretation, EUCAST rules, etc.) in this \code{AMR} package are publicly and freely available. We continually export our data sets to formats for use in R, SPSS, SAS, Stata and Excel. We also supply flat files that are machine-readable and suitable for input in any software program, such as laboratory information systems. Please find \href{https://msberends.github.io/AMR/articles/datasets.html}{all download links on our website}, which is automatically updated with every code change. +\details{ +Like all data sets in this package, this data set is publicly available for download in the following formats: R, MS Excel, Apache Feather, Apache Parquet, SPSS, SAS, and Stata. Please visit \href{https://msberends.github.io/AMR/articles/datasets.html}{our website for the download links}. The actual files are of course available on \href{https://github.com/msberends/AMR/tree/main/data-raw}{our GitHub repository}. } - \examples{ -head(example_isolates_unclean) +example_isolates_unclean } \keyword{datasets} diff --git a/man/first_isolate.Rd b/man/first_isolate.Rd index e6db158c..58ae08a2 100755 --- a/man/first_isolate.Rd +++ b/man/first_isolate.Rd @@ -58,7 +58,7 @@ filter_first_isolate( \item{col_specimen}{column name of the specimen type or group} -\item{col_icu}{column name of the logicals (\code{TRUE}/\code{FALSE}) whether a ward or department is an Intensive Care Unit (ICU)} +\item{col_icu}{column name of the logicals (\code{TRUE}/\code{FALSE}) whether a ward or department is an Intensive Care Unit (ICU). This can also be a \link{logical} vector with the same length as rows in \code{x}.} \item{col_keyantimicrobials}{(only useful when \code{method = "phenotype-based"}) column name of the key antimicrobials to determine first isolates, see \code{\link[=key_antimicrobials]{key_antimicrobials()}}. Defaults to the first column that starts with 'key' followed by 'ab' or 'antibiotics' or 'antimicrobials' (case insensitive). Use \code{col_keyantimicrobials = FALSE} to prevent this. Can also be the output of \code{\link[=key_antimicrobials]{key_antimicrobials()}}.} @@ -87,7 +87,7 @@ filter_first_isolate( \item{...}{arguments passed on to \code{\link[=first_isolate]{first_isolate()}} when using \code{\link[=filter_first_isolate]{filter_first_isolate()}}, otherwise arguments passed on to \code{\link[=key_antimicrobials]{key_antimicrobials()}} (such as \code{universal}, \code{gram_negative}, \code{gram_positive})} } \value{ -A \code{\link{logical}} vector +A \link{logical} vector } \description{ Determine first isolates of all microorganisms of every patient per episode and (if needed) per specimen type. These functions support all four methods as summarised by Hindler \emph{et al.} in 2007 (\doi{10.1086/511864}). To determine patient episodes not necessarily based on microorganisms, use \code{\link[=is_new_episode]{is_new_episode()}} that also supports grouping with the \code{dplyr} package. @@ -177,36 +177,22 @@ if (require("dplyr")) { # filter on first isolates using dplyr: example_isolates \%>\% filter(first_isolate()) + +} +if (require("dplyr")) { # short-hand version: example_isolates \%>\% filter_first_isolate(info = FALSE) +} +if (require("dplyr")) { + # flag the first isolates per group: example_isolates \%>\% - group_by(hospital_id) \%>\% + group_by(ward) \%>\% mutate(first = first_isolate()) \%>\% - select(hospital_id, date, patient_id, mo, first) - - # now let's see if first isolates matter: - A <- example_isolates \%>\% - group_by(hospital_id) \%>\% - summarise(count = n_rsi(GEN), # gentamicin availability - resistance = resistance(GEN)) # gentamicin resistance - - B <- example_isolates \%>\% - filter_first_isolate() \%>\% # the 1st isolate filter - group_by(hospital_id) \%>\% - summarise(count = n_rsi(GEN), # gentamicin availability - resistance = resistance(GEN)) # gentamicin resistance - - # Have a look at A and B. - A - B - - # B is more reliable because every isolate is counted only once. - # Gentamicin resistance in hospital D appears to be 4.2\% higher than - # when you (erroneously) would have used all isolates for analysis. + select(ward, date, patient, mo, first) } } } diff --git a/man/get_episode.Rd b/man/get_episode.Rd index 3bd0e4e4..563ae93f 100644 --- a/man/get_episode.Rd +++ b/man/get_episode.Rd @@ -54,26 +54,34 @@ if (require("dplyr")) { # episodes based on any (combination of) grouping variables: df \%>\% mutate(condition = sample(x = c("A", "B", "C"), - size = 2000, + size = 200, replace = TRUE)) \%>\% group_by(condition) \%>\% mutate(new_episode = is_new_episode(date, 365)) \%>\% - select(patient_id, date, condition, new_episode) + select(patient, date, condition, new_episode) + +} +if (require("dplyr")) { df \%>\% - group_by(hospital_id, patient_id) \%>\% + group_by(ward, patient) \%>\% transmute(date, - patient_id, + patient, new_index = get_episode(date, 60), new_logical = is_new_episode(date, 60)) + +} +if (require("dplyr")) { df \%>\% - group_by(hospital_id) \%>\% - summarise(n_patients = n_distinct(patient_id), + group_by(ward) \%>\% + summarise(n_patients = n_distinct(patient), n_episodes_365 = sum(is_new_episode(date, episode_days = 365)), n_episodes_60 = sum(is_new_episode(date, episode_days = 60)), n_episodes_30 = sum(is_new_episode(date, episode_days = 30))) +} +if (require("dplyr")) { # grouping on patients and microorganisms leads to the same # results as first_isolate() when using 'episode-based': @@ -82,16 +90,19 @@ if (require("dplyr")) { method = "episode-based") y <- df \%>\% - group_by(patient_id, mo) \%>\% + group_by(patient, mo) \%>\% filter(is_new_episode(date, 365)) \%>\% ungroup() identical(x, y) + +} +if (require("dplyr")) { # but is_new_episode() has a lot more flexibility than first_isolate(), # since you can now group on anything that seems relevant: df \%>\% - group_by(patient_id, mo, hospital_id, ward_icu) \%>\% + group_by(patient, mo, ward) \%>\% mutate(flag_episode = is_new_episode(date, 365)) \%>\% select(group_vars(.), flag_episode) } diff --git a/man/ggplot_rsi.Rd b/man/ggplot_rsi.Rd index f29e35d1..269afbee 100644 --- a/man/ggplot_rsi.Rd +++ b/man/ggplot_rsi.Rd @@ -142,11 +142,14 @@ At default, the names of antibiotics will be shown on the plots using \code{\lin } \examples{ \donttest{ -if (require("ggplot2") & require("dplyr")) { +if (require("ggplot2") && require("dplyr")) { # get antimicrobial results for drugs against a UTI: ggplot(example_isolates \%>\% select(AMX, NIT, FOS, TMP, CIP)) + geom_rsi() + +} +if (require("ggplot2") && require("dplyr")) { # prettify the plot using some additional functions: df <- example_isolates \%>\% select(AMX, NIT, FOS, TMP, CIP) @@ -156,16 +159,25 @@ if (require("ggplot2") & require("dplyr")) { scale_rsi_colours() + labels_rsi_count() + theme_rsi() + +} +if (require("ggplot2") && require("dplyr")) { # or better yet, simplify this using the wrapper function - a single command: example_isolates \%>\% select(AMX, NIT, FOS, TMP, CIP) \%>\% ggplot_rsi() + +} +if (require("ggplot2") && require("dplyr")) { # get only proportions and no counts: example_isolates \%>\% select(AMX, NIT, FOS, TMP, CIP) \%>\% ggplot_rsi(datalabels = FALSE) + +} +if (require("ggplot2") && require("dplyr")) { # add other ggplot2 arguments as you like: example_isolates \%>\% @@ -175,11 +187,17 @@ if (require("ggplot2") & require("dplyr")) { size = 1, linetype = 2, alpha = 0.25) + +} +if (require("ggplot2") && require("dplyr")) { # you can alter the colours with colour names: example_isolates \%>\% select(AMX) \%>\% ggplot_rsi(colours = c(SI = "yellow")) + +} +if (require("ggplot2") && require("dplyr")) { # but you can also use the built-in colour-blind friendly colours for # your plots, where "S" is green, "I" is yellow and "R" is red: @@ -189,34 +207,42 @@ if (require("ggplot2") & require("dplyr")) { ggplot() + geom_col(aes(x = x, y = y, fill = z)) + scale_rsi_colours(Value4 = "S", Value5 = "I", Value6 = "R") + +} +if (require("ggplot2") && require("dplyr")) { # resistance of ciprofloxacine per age group example_isolates \%>\% mutate(first_isolate = first_isolate()) \%>\% filter(first_isolate == TRUE, - mo == as.mo("E. coli")) \%>\% + mo == as.mo("Escherichia coli")) \%>\% # age_groups() is also a function in this AMR package: group_by(age_group = age_groups(age)) \%>\% select(age_group, CIP) \%>\% ggplot_rsi(x = "age_group") + +} +if (require("ggplot2") && require("dplyr")) { # a shorter version which also adjusts data label colours: example_isolates \%>\% select(AMX, NIT, FOS, TMP, CIP) \%>\% ggplot_rsi(colours = FALSE) - + +} +if (require("ggplot2") && require("dplyr")) { # it also supports groups (don't forget to use the group var on `x` or `facet`): example_isolates \%>\% - filter(mo_is_gram_negative()) \%>\% + filter(mo_is_gram_negative(), ward != "Outpatient") \%>\% # select only UTI-specific drugs - select(hospital_id, AMX, NIT, FOS, TMP, CIP) \%>\% - group_by(hospital_id) \%>\% - ggplot_rsi(x = "hospital_id", + select(ward, AMX, NIT, FOS, TMP, CIP) \%>\% + group_by(ward) \%>\% + ggplot_rsi(x = "ward", facet = "antibiotic", nrow = 1, - title = "AMR of Anti-UTI Drugs Per Hospital", - x.title = "Hospital", + title = "AMR of Anti-UTI Drugs Per Ward", + x.title = "Ward", datalabels = FALSE) } } diff --git a/man/intrinsic_resistant.Rd b/man/intrinsic_resistant.Rd index 7dfe72f1..8e1e33b0 100644 --- a/man/intrinsic_resistant.Rd +++ b/man/intrinsic_resistant.Rd @@ -5,7 +5,7 @@ \alias{intrinsic_resistant} \title{Data Set with Bacterial Intrinsic Resistance} \format{ -A \link{data.frame} with 134,956 observations and 2 variables: +A [tibble\link[tibble:tibble]{tibble::tibble} with 134,956 observations and 2 variables: \itemize{ \item \code{mo}\cr Microorganism ID \item \code{ab}\cr Antibiotic ID @@ -18,16 +18,15 @@ intrinsic_resistant Data set containing defined intrinsic resistance by EUCAST of all bug-drug combinations. } \details{ -The repository of this \code{AMR} package contains a file comprising this data set with full taxonomic and antibiotic names: \url{https://github.com/msberends/AMR/blob/main/data-raw/intrinsic_resistant.txt}. This file \strong{allows for machine reading EUCAST guidelines about intrinsic resistance}, which is almost impossible with the Excel and PDF files distributed by EUCAST. The file is updated automatically. - This data set is based on \href{https://www.eucast.org/expert_rules_and_expected_phenotypes/}{'EUCAST Expert Rules' and 'EUCAST Intrinsic Resistance and Unusual Phenotypes' v3.3} (2021). -} -\section{Reference Data Publicly Available}{ +\subsection{Direct download}{ -All reference data sets (about microorganisms, antibiotics, R/SI interpretation, EUCAST rules, etc.) in this \code{AMR} package are publicly and freely available. We continually export our data sets to formats for use in R, SPSS, SAS, Stata and Excel. We also supply flat files that are machine-readable and suitable for input in any software program, such as laboratory information systems. Please find \href{https://msberends.github.io/AMR/articles/datasets.html}{all download links on our website}, which is automatically updated with every code change. -} +Like all data sets in this package, this data set is publicly available for download in the following formats: R, MS Excel, Apache Feather, Apache Parquet, SPSS, SAS, and Stata. Please visit \href{https://msberends.github.io/AMR/articles/datasets.html}{our website for the download links}. The actual files are of course available on \href{https://github.com/msberends/AMR/tree/main/data-raw}{our GitHub repository}. +They \strong{allow for machine reading EUCAST and CLSI guidelines}, which is almost impossible with the MS Excel and PDF files distributed by EUCAST and CLSI. +} +} \examples{ -head(intrinsic_resistant) +intrinsic_resistant } \keyword{datasets} diff --git a/man/join.Rd b/man/join.Rd index 6c488a77..828da147 100755 --- a/man/join.Rd +++ b/man/join.Rd @@ -47,21 +47,22 @@ If the \code{dplyr} package is installed, their join functions will be used. Oth left_join_microorganisms(as.mo("K. pneumoniae")) left_join_microorganisms("B_KLBSL_PNMN") +df <- data.frame(date = seq(from = as.Date("2018-01-01"), + to = as.Date("2018-01-07"), + by = 1), + bacteria = as.mo(c("S. aureus", "MRSA", "MSSA", "STAAUR", + "E. coli", "E. coli", "E. coli")), + stringsAsFactors = FALSE) +colnames(df) + +df_joined <- left_join_microorganisms(df, "bacteria") +colnames(df_joined) + \donttest{ if (require("dplyr")) { example_isolates \%>\% left_join_microorganisms() \%>\% colnames() - - df <- data.frame(date = seq(from = as.Date("2018-01-01"), - to = as.Date("2018-01-07"), - by = 1), - bacteria = as.mo(c("S. aureus", "MRSA", "MSSA", "STAAUR", - "E. coli", "E. coli", "E. coli")), - stringsAsFactors = FALSE) - colnames(df) - df_joined <- left_join_microorganisms(df, "bacteria") - colnames(df_joined) } } } diff --git a/man/like.Rd b/man/like.Rd index 306071a9..cf93675f 100755 --- a/man/like.Rd +++ b/man/like.Rd @@ -60,11 +60,9 @@ a \%unlike\% b a[1] \%like\% b a \%like\% b[1] -# get isolates whose name start with 'Ent' or 'ent' -example_isolates[which(mo_name(example_isolates$mo) \%like\% "^ent"), ] \donttest{ -# faster way, since mo_name() is context-aware: -example_isolates[which(mo_name() \%like\% "^ent"), ] +# get isolates whose name start with 'Entero' (case-insensitive) +example_isolates[which(mo_name() \%like\% "^entero"), ] if (require("dplyr")) { example_isolates \%>\% diff --git a/man/microorganisms.Rd b/man/microorganisms.Rd index e1189be4..778f085c 100755 --- a/man/microorganisms.Rd +++ b/man/microorganisms.Rd @@ -5,7 +5,7 @@ \alias{microorganisms} \title{Data Set with 70,764 Microorganisms} \format{ -A \link{data.frame} with 70,764 observations and 16 variables: +A [tibble\link[tibble:tibble]{tibble::tibble} with 70,764 observations and 16 variables: \itemize{ \item \code{mo}\cr ID of microorganism as used by this package \item \code{fullname}\cr Full name, like \code{"Escherichia coli"} @@ -65,9 +65,7 @@ For convenience, some entries were added manually: \subsection{Direct download}{ -This data set is available as 'flat file' for use even without \R - you can find the file here: \url{https://github.com/msberends/AMR/raw/main/data-raw/microorganisms.txt}. - -The file in \R format (with preserved data structure) can be found here: \url{https://github.com/msberends/AMR/raw/main/data/microorganisms.rda}. +Like all data sets in this package, this data set is publicly available for download in the following formats: R, MS Excel, Apache Feather, Apache Parquet, SPSS, SAS, and Stata. Please visit \href{https://msberends.github.io/AMR/articles/datasets.html}{our website for the download links}. The actual files are of course available on \href{https://github.com/msberends/AMR/tree/main/data-raw}{our GitHub repository}. } } \section{About the Records from LPSN (see \emph{Source})}{ @@ -85,13 +83,8 @@ This package contains the complete taxonomic tree of almost all microorganisms ( \link[=catalogue_of_life]{Click here} for more information about the included taxa. Check which versions of the CoL and LPSN were included in this package with \code{\link[=catalogue_of_life_version]{catalogue_of_life_version()}}. } -\section{Reference Data Publicly Available}{ - -All reference data sets (about microorganisms, antibiotics, R/SI interpretation, EUCAST rules, etc.) in this \code{AMR} package are publicly and freely available. We continually export our data sets to formats for use in R, SPSS, SAS, Stata and Excel. We also supply flat files that are machine-readable and suitable for input in any software program, such as laboratory information systems. Please find \href{https://msberends.github.io/AMR/articles/datasets.html}{all download links on our website}, which is automatically updated with every code change. -} - \examples{ -head(microorganisms) +microorganisms } \seealso{ \code{\link[=as.mo]{as.mo()}}, \code{\link[=mo_property]{mo_property()}}, \link{microorganisms.codes}, \link{intrinsic_resistant} diff --git a/man/microorganisms.codes.Rd b/man/microorganisms.codes.Rd index 08f81729..2df9a756 100644 --- a/man/microorganisms.codes.Rd +++ b/man/microorganisms.codes.Rd @@ -5,7 +5,7 @@ \alias{microorganisms.codes} \title{Data Set with 5,604 Common Microorganism Codes} \format{ -A \link{data.frame} with 5,604 observations and 2 variables: +A [tibble\link[tibble:tibble]{tibble::tibble} with 5,604 observations and 2 variables: \itemize{ \item \code{code}\cr Commonly used code of a microorganism \item \code{mo}\cr ID of the microorganism in the \link{microorganisms} data set @@ -17,11 +17,9 @@ microorganisms.codes \description{ A data set containing commonly used codes for microorganisms, from laboratory systems and WHONET. Define your own with \code{\link[=set_mo_source]{set_mo_source()}}. They will all be searched when using \code{\link[=as.mo]{as.mo()}} and consequently all the \code{\link[=mo_property]{mo_*}} functions. } -\section{Reference Data Publicly Available}{ - -All reference data sets (about microorganisms, antibiotics, R/SI interpretation, EUCAST rules, etc.) in this \code{AMR} package are publicly and freely available. We continually export our data sets to formats for use in R, SPSS, SAS, Stata and Excel. We also supply flat files that are machine-readable and suitable for input in any software program, such as laboratory information systems. Please find \href{https://msberends.github.io/AMR/articles/datasets.html}{all download links on our website}, which is automatically updated with every code change. +\details{ +Like all data sets in this package, this data set is publicly available for download in the following formats: R, MS Excel, Apache Feather, Apache Parquet, SPSS, SAS, and Stata. Please visit \href{https://msberends.github.io/AMR/articles/datasets.html}{our website for the download links}. The actual files are of course available on \href{https://github.com/msberends/AMR/tree/main/data-raw}{our GitHub repository}. } - \section{Catalogue of Life}{ \if{html}{\figure{logo_col.png}{options: height="40" style=margin-bottom:"5"} \cr} @@ -31,7 +29,7 @@ This package contains the complete taxonomic tree of almost all microorganisms ( } \examples{ -head(microorganisms.codes) +microorganisms.codes } \seealso{ \code{\link[=as.mo]{as.mo()}} \link{microorganisms} diff --git a/man/microorganisms.old.Rd b/man/microorganisms.old.Rd index c3d4f900..4e3b214e 100644 --- a/man/microorganisms.old.Rd +++ b/man/microorganisms.old.Rd @@ -5,7 +5,7 @@ \alias{microorganisms.old} \title{Data Set with Previously Accepted Taxonomic Names} \format{ -A \link{data.frame} with 14,338 observations and 4 variables: +A [tibble\link[tibble:tibble]{tibble::tibble} with 14,338 observations and 4 variables: \itemize{ \item \code{fullname}\cr Old full taxonomic name of the microorganism \item \code{fullname_new}\cr New full taxonomic name of the microorganism @@ -24,6 +24,9 @@ microorganisms.old \description{ A data set containing old (previously valid or accepted) taxonomic names according to the Catalogue of Life. This data set is used internally by \code{\link[=as.mo]{as.mo()}}. } +\details{ +Like all data sets in this package, this data set is publicly available for download in the following formats: R, MS Excel, Apache Feather, Apache Parquet, SPSS, SAS, and Stata. Please visit \href{https://msberends.github.io/AMR/articles/datasets.html}{our website for the download links}. The actual files are of course available on \href{https://github.com/msberends/AMR/tree/main/data-raw}{our GitHub repository}. +} \section{Catalogue of Life}{ \if{html}{\figure{logo_col.png}{options: height="40" style=margin-bottom:"5"} \cr} @@ -32,13 +35,8 @@ This package contains the complete taxonomic tree of almost all microorganisms ( \link[=catalogue_of_life]{Click here} for more information about the included taxa. Check which versions of the CoL and LPSN were included in this package with \code{\link[=catalogue_of_life_version]{catalogue_of_life_version()}}. } -\section{Reference Data Publicly Available}{ - -All reference data sets (about microorganisms, antibiotics, R/SI interpretation, EUCAST rules, etc.) in this \code{AMR} package are publicly and freely available. We continually export our data sets to formats for use in R, SPSS, SAS, Stata and Excel. We also supply flat files that are machine-readable and suitable for input in any software program, such as laboratory information systems. Please find \href{https://msberends.github.io/AMR/articles/datasets.html}{all download links on our website}, which is automatically updated with every code change. -} - \examples{ -head(microorganisms.old) +microorganisms.old } \seealso{ \code{\link[=as.mo]{as.mo()}} \code{\link[=mo_property]{mo_property()}} \link{microorganisms} diff --git a/man/mo_matching_score.Rd b/man/mo_matching_score.Rd index edc4cb94..922f3ded 100644 --- a/man/mo_matching_score.Rd +++ b/man/mo_matching_score.Rd @@ -41,7 +41,7 @@ Since \code{AMR} version 1.8.1, common microorganism abbreviations are ignored i \section{Reference Data Publicly Available}{ -All reference data sets (about microorganisms, antibiotics, R/SI interpretation, EUCAST rules, etc.) in this \code{AMR} package are publicly and freely available. We continually export our data sets to formats for use in R, SPSS, SAS, Stata and Excel. We also supply flat files that are machine-readable and suitable for input in any software program, such as laboratory information systems. Please find \href{https://msberends.github.io/AMR/articles/datasets.html}{all download links on our website}, which is automatically updated with every code change. +All data sets in this \code{AMR} package (about microorganisms, antibiotics, R/SI interpretation, EUCAST rules, etc.) are publicly and freely available for download in the following formats: R, MS Excel, Apache Feather, Apache Parquet, SPSS, SAS, and Stata. We also provide tab-separated plain text files that are machine-readable and suitable for input in any software program, such as laboratory information systems. Please visit \href{https://msberends.github.io/AMR/articles/datasets.html}{our website for the download links}. The actual files are of course available on \href{https://github.com/msberends/AMR/tree/main/data-raw}{our GitHub repository}. } \examples{ diff --git a/man/mo_property.Rd b/man/mo_property.Rd index 31b5f29a..8f9432e5 100644 --- a/man/mo_property.Rd +++ b/man/mo_property.Rd @@ -187,7 +187,7 @@ This package contains the complete taxonomic tree of almost all microorganisms ( \section{Reference Data Publicly Available}{ -All reference data sets (about microorganisms, antibiotics, R/SI interpretation, EUCAST rules, etc.) in this \code{AMR} package are publicly and freely available. We continually export our data sets to formats for use in R, SPSS, SAS, Stata and Excel. We also supply flat files that are machine-readable and suitable for input in any software program, such as laboratory information systems. Please find \href{https://msberends.github.io/AMR/articles/datasets.html}{all download links on our website}, which is automatically updated with every code change. +All data sets in this \code{AMR} package (about microorganisms, antibiotics, R/SI interpretation, EUCAST rules, etc.) are publicly and freely available for download in the following formats: R, MS Excel, Apache Feather, Apache Parquet, SPSS, SAS, and Stata. We also provide tab-separated plain text files that are machine-readable and suitable for input in any software program, such as laboratory information systems. Please visit \href{https://msberends.github.io/AMR/articles/datasets.html}{our website for the download links}. The actual files are of course available on \href{https://github.com/msberends/AMR/tree/main/data-raw}{our GitHub repository}. } \examples{ diff --git a/man/proportion.Rd b/man/proportion.Rd index f844b5ac..3637dcba 100644 --- a/man/proportion.Rd +++ b/man/proportion.Rd @@ -158,12 +158,12 @@ proportion_R(example_isolates$AMX) \donttest{ if (require("dplyr")) { example_isolates \%>\% - group_by(hospital_id) \%>\% + group_by(ward) \%>\% summarise(r = resistance(CIP), n = n_rsi(CIP)) # n_rsi works like n_distinct in dplyr, see ?n_rsi example_isolates \%>\% - group_by(hospital_id) \%>\% + group_by(ward) \%>\% summarise(R = resistance(CIP, as_percent = TRUE), SI = susceptibility(CIP, as_percent = TRUE), n1 = count_all(CIP), # the actual total; sum of all three @@ -195,7 +195,7 @@ if (require("dplyr")) { example_isolates \%>\% - group_by(hospital_id) \%>\% + group_by(ward) \%>\% summarise(cipro_p = susceptibility(CIP, as_percent = TRUE), cipro_n = count_all(CIP), genta_p = susceptibility(GEN, as_percent = TRUE), @@ -211,8 +211,8 @@ if (require("dplyr")) { # It also supports grouping variables # (use rsi_df to also include the count) example_isolates \%>\% - select(hospital_id, AMX, CIP) \%>\% - group_by(hospital_id) \%>\% + select(ward, AMX, CIP) \%>\% + group_by(ward) \%>\% rsi_df(translate = FALSE) } } diff --git a/man/resistance_predict.Rd b/man/resistance_predict.Rd index 1951f59c..fe83f7e9 100644 --- a/man/resistance_predict.Rd +++ b/man/resistance_predict.Rd @@ -142,7 +142,7 @@ if (require("dplyr")) { filter_first_isolate() \%>\% filter(mo_genus(mo) == "Staphylococcus") \%>\% resistance_predict("PEN", model = "binomial") - plot(x) + print(plot(x)) # get the model from the object mymodel <- attributes(x)$model @@ -150,7 +150,7 @@ if (require("dplyr")) { } # create nice plots with ggplot2 yourself -if (require("dplyr") & require("ggplot2")) { +if (require("dplyr") && require("ggplot2")) { data <- example_isolates \%>\% filter(mo == as.mo("E. coli")) \%>\% diff --git a/man/rsi_translation.Rd b/man/rsi_translation.Rd index d67a54a9..8b2776a5 100644 --- a/man/rsi_translation.Rd +++ b/man/rsi_translation.Rd @@ -5,7 +5,7 @@ \alias{rsi_translation} \title{Data Set for R/SI Interpretation} \format{ -A \link{data.frame} with 20,369 observations and 11 variables: +A [tibble\link[tibble:tibble]{tibble::tibble} with 20,369 observations and 11 variables: \itemize{ \item \code{guideline}\cr Name of the guideline \item \code{method}\cr Either "DISK" or "MIC" @@ -27,15 +27,12 @@ rsi_translation Data set containing reference data to interpret MIC and disk diffusion to R/SI values, according to international guidelines. Currently implemented guidelines are EUCAST (2011-2022) and CLSI (2011-2022). Use \code{\link[=as.rsi]{as.rsi()}} to transform MICs or disks measurements to R/SI values. } \details{ -The repository of this \code{AMR} package contains a file comprising this exact data set: \url{https://github.com/msberends/AMR/blob/main/data-raw/rsi_translation.txt}. This file \strong{allows for machine reading EUCAST and CLSI guidelines}, which is almost impossible with the Excel and PDF files distributed by EUCAST and CLSI. The file is updated automatically and the \code{mo} and \code{ab} columns have been transformed to contain the full official names instead of codes. -} -\section{Reference Data Publicly Available}{ +Like all data sets in this package, this data set is publicly available for download in the following formats: R, MS Excel, Apache Feather, Apache Parquet, SPSS, SAS, and Stata. Please visit \href{https://msberends.github.io/AMR/articles/datasets.html}{our website for the download links}. The actual files are of course available on \href{https://github.com/msberends/AMR/tree/main/data-raw}{our GitHub repository}. -All reference data sets (about microorganisms, antibiotics, R/SI interpretation, EUCAST rules, etc.) in this \code{AMR} package are publicly and freely available. We continually export our data sets to formats for use in R, SPSS, SAS, Stata and Excel. We also supply flat files that are machine-readable and suitable for input in any software program, such as laboratory information systems. Please find \href{https://msberends.github.io/AMR/articles/datasets.html}{all download links on our website}, which is automatically updated with every code change. +They \strong{allow for machine reading EUCAST and CLSI guidelines}, which is almost impossible with the MS Excel and PDF files distributed by EUCAST and CLSI. } - \examples{ -head(rsi_translation) +rsi_translation } \seealso{ \link{intrinsic_resistant} diff --git a/man/translate.Rd b/man/translate.Rd index e3e470b7..09890d5a 100644 --- a/man/translate.Rd +++ b/man/translate.Rd @@ -10,14 +10,14 @@ \usage{ get_AMR_locale() -set_AMR_locale(lang) +set_AMR_locale(language) reset_AMR_locale() translate_AMR(x, language = get_AMR_locale()) } \arguments{ -\item{lang}{language to choose. Use one of these supported language names or ISO-639-1 codes: "English" ("en"), "Chinese" ("zh"), "Danish" ("da"), "Dutch" ("nl"), "French" ("fr"), "German" ("de"), "Greek" ("el"), "Italian" ("it"), "Japanese" ("ja"), "Polish" ("pl"), "Portuguese" ("pt"), "Russian" ("ru"), "Spanish" ("es"), "Swedish" ("sv"), "Turkish" ("tr"), "Ukrainian" ("uk").} +\item{language}{language to choose. Use one of these supported language names or ISO-639-1 codes: "English" ("en"), "Chinese" ("zh"), "Danish" ("da"), "Dutch" ("nl"), "French" ("fr"), "German" ("de"), "Greek" ("el"), "Italian" ("it"), "Japanese" ("ja"), "Polish" ("pl"), "Portuguese" ("pt"), "Russian" ("ru"), "Spanish" ("es"), "Swedish" ("sv"), "Turkish" ("tr"), "Ukrainian" ("uk").} \item{x}{text to translate} } @@ -30,7 +30,7 @@ The currently 16 supported languages are English, Chinese, Danish, Dutch, French Please read about adding or updating a language in \href{https://github.com/msberends/AMR/wiki/}{our Wiki}. \subsection{Changing the Default Language}{ -The system language will be used at default (as returned by \code{Sys.getenv("LANG")} or, if \code{LANG} is not set, \link{Sys.getlocale("LC_COLLATE")}), if that language is supported. But the language to be used can be overwritten in two ways and will be checked in this order: +The system language will be used at default (as returned by \code{Sys.getenv("LANG")} or, if \code{LANG} is not set, \code{\link[=Sys.getlocale]{Sys.getlocale("LC_COLLATE")}}), if that language is supported. But the language to be used can be overwritten in two ways and will be checked in this order: \enumerate{ \item Setting the R option \code{AMR_locale}, either by using \code{set_AMR_locale()} or by running e.g. \code{options(AMR_locale = "de")}. diff --git a/pkgdown/extra.css b/pkgdown/extra.css index a812063f..4eca8890 100644 --- a/pkgdown/extra.css +++ b/pkgdown/extra.css @@ -25,9 +25,6 @@ # ==================================================================== # */ -h1, h2 { - text-align: left !important; -} /* replace 'Developers' with 'Maintainers' */ .developers h2 { display: none; @@ -74,11 +71,15 @@ a pre[href] { color: var(--bs-success); text-decoration: none; } +a[href] { + text-decoration: none; +} code a[href]:hover, code a[href]:focus, a[href] code:hover, a[href] code:focus, pre a[href]:hover, pre a[href]:focus, a code[href]:hover, a code[href]:focus, -a pre[href]:hover, a pre[href]:focus { +a pre[href]:hover, a pre[href]:focus, +a[href]:hover, a[href]:focus { /* adjusted colour for all real links; having href attribute */ text-decoration: underline; } diff --git a/pkgdown/extra.js b/pkgdown/extra.js index 9b247271..2ed5ada4 100644 --- a/pkgdown/extra.js +++ b/pkgdown/extra.js @@ -27,6 +27,9 @@ $(document).ready(function() { + // Replace 'Developers' with 'Maintainers' on the main page + $(".developers h2").text("Maintainers"); + // replace \donttest and \dontrun texts in Examples if ($("#ref-examples ~ div pre").length > 0) { $("#ref-examples ~ div pre").html($("#ref-examples ~ div pre").html().replaceAll("# \\donttest{", "")); diff --git a/vignettes/AMR.Rmd b/vignettes/AMR.Rmd index 03cd1d9b..787bc5eb 100755 --- a/vignettes/AMR.Rmd +++ b/vignettes/AMR.Rmd @@ -534,7 +534,7 @@ autoplot(disk_values, ## Independence test -The next example uses the `example_isolates` data set. This is a data set included with this package and contains 2,000 microbial isolates with their full antibiograms. It reflects reality and can be used to practice AMR data analysis. +The next example uses the `example_isolates` data set. This is a data set included with this package and contains 2,000 microbial isolates with their full antibiograms. It reflects reality and can be used to practise AMR data analysis. We will compare the resistance to fosfomycin (column `FOS`) in hospital A and D. The input for the `fisher.test()` can be retrieved with a transformation like this: @@ -543,11 +543,11 @@ We will compare the resistance to fosfomycin (column `FOS`) in hospital A and D. library(tidyr) check_FOS <- example_isolates %>% - filter(hospital_id %in% c("A", "D")) %>% # filter on only hospitals A and D - select(hospital_id, FOS) %>% # select the hospitals and fosfomycin - group_by(hospital_id) %>% # group on the hospitals - count_df(combine_SI = TRUE) %>% # count all isolates per group (hospital_id) - pivot_wider(names_from = hospital_id, # transform output so A and D are columns + filter(ward %in% c("A", "D")) %>% # filter on only hospitals A and D + select(ward, FOS) %>% # select the hospitals and fosfomycin + group_by(ward) %>% # group on the hospitals + count_df(combine_SI = TRUE) %>% # count all isolates per group (ward) + pivot_wider(names_from = ward, # transform output so A and D are columns values_from = value) %>% select(A, D) %>% # and only select these columns as.matrix() # transform to a good old matrix for fisher.test() diff --git a/vignettes/datasets.Rmd b/vignettes/datasets.Rmd index 72ff12c4..f91408e5 100644 --- a/vignettes/datasets.Rmd +++ b/vignettes/datasets.Rmd @@ -80,6 +80,7 @@ download_txt <- function(filename) { print_df <- function(x, rows = 6) { x %>% + as.data.frame(stringsAsFactors = FALSE) %>% head(n = rows) %>% mutate_all(function(x) { if (is.list(x)) { @@ -285,3 +286,42 @@ Currently included dosages in the data set are meant for: `r AMR:::format_eucast dosage %>% print_df() ``` + + +## `example_isolates`: Example Data for Practice + +`r structure_txt(example_isolates)` + +This data set is in R available as `example_isolates`, after you load the `AMR` package. + +`r download_txt("example_isolates")` + +### Source + +This data set contains randomised fictitious data, but reflects reality and can be used to practise AMR data analysis. + +### Example content + +```{r, echo = FALSE} +example_isolates %>% + print_df() +``` + +## `example_isolates_unclean`: Example Data for Practice + +`r structure_txt(example_isolates_unclean)` + +This data set is in R available as `example_isolates_unclean`, after you load the `AMR` package. + +`r download_txt("example_isolates_unclean")` + +### Source + +This data set contains randomised fictitious data, but reflects reality and can be used to practise AMR data analysis. + +### Example content + +```{r, echo = FALSE} +example_isolates_unclean %>% + print_df() +```