diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index 1b735ad5..9935dcf8 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -107,9 +107,9 @@ jobs: sudo apt install -y libssl-dev pandoc pandoc-citeproc libxml2-dev libicu-dev libcurl4-openssl-dev libpng-dev - name: Query dependencies - # this will change once a week, so it will cache dependency updates + # this will change every day (i.e. at scheduled night run of GitHub Action), so it will cache dependency updates run: | - writeLines(paste(format(Sys.Date(), "week %V %Y"), sprintf("R-%i.%i", getRversion()$major, getRversion()$minor)), ".github/week-R-version") + writeLines(paste0(format(Sys.Date(), "%Y%m%d"), sprintf("-R-%i.%i", getRversion()$major, getRversion()$minor)), ".github/daily-R-bundle") shell: Rscript {0} - name: Restore cached R packages @@ -117,7 +117,7 @@ jobs: uses: actions/cache@v2 with: path: ${{ env.R_LIBS_USER }} - key: ${{ matrix.config.os }}-${{ hashFiles('.github/week-R-version') }}-v4 + key: ${{ matrix.config.os }}-${{ hashFiles('.github/daily-R-bundle') }}-v4 - name: Unpack AMR and install R dependencies if: always() diff --git a/DESCRIPTION b/DESCRIPTION index 2f273501..4bed10aa 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: AMR -Version: 1.7.1.9004 -Date: 2021-06-15 +Version: 1.7.1.9005 +Date: 2021-06-22 Title: Antimicrobial Resistance Data Analysis Authors@R: c( person(role = c("aut", "cre"), diff --git a/NEWS.md b/NEWS.md index 68e8bca0..84bd537a 100755 --- a/NEWS.md +++ b/NEWS.md @@ -1,12 +1,17 @@ -# `AMR` 1.7.1.9004 -## Last updated: 15 June 2021 +# `AMR` 1.7.1.9005 +## Last updated: 22 June 2021 ### Changed -* Added more antibiotic class selectors: `aminopenicillins()`, `lincosamides()`, `lipoglycopeptides()`, `polymyxins()`, `quinolones()`, `streptogramins()` and `ureidopenicillins()` +* Antibiotic class selectors (see `ab_class()`) + * They now finally also work in R-3.0 and R-3.1, supporting every version of R since 2013 + * Added more selectors: `aminopenicillins()`, `lincosamides()`, `lipoglycopeptides()`, `polymyxins()`, `quinolones()`, `streptogramins()` and `ureidopenicillins()` + * Fix for using selectors multiple times in one call (e.g., using them in `dplyr::filter()` and immediately after in `dplyr::select()`) * Added `ggplot2::autoplot()` generic for classes ``, ``, `` and `` * Fix to prevent introducing `NA`s for old MO codes when running `as.mo()` on them * Added more informative error messages when any of the `proportion_*()` and `count_*()` functions fail -* Fix for using antibiotic selectors multiple times in one call (e.g., using in `dplyr::filter()` and immediately after in `dplyr::select()`) +* When printing a tibble with any old MO code, a warning will be thrown that old codes should be updated using `as.mo()` +* Improved automatic column selector when `col_*` arguments are left blank, e.g. in `first_isolate()` +* The right input types for `random_mic()`, `random_disk()` and `random_rsi()` are now enforced # `AMR` 1.7.1 diff --git a/R/aa_helper_functions.R b/R/aa_helper_functions.R index 20929691..b6f2436d 100755 --- a/R/aa_helper_functions.R +++ b/R/aa_helper_functions.R @@ -170,65 +170,73 @@ search_type_in_df <- function(x, type, info = TRUE) { # remove attributes from other packages x <- as.data.frame(x, stringsAsFactors = FALSE) - colnames(x) <- trimws(colnames(x)) + colnames_formatted <- tolower(generalise_antibiotic_name(colnames(x))) # -- mo if (type == "mo") { if (any(vapply(FUN.VALUE = logical(1), x, is.mo))) { - found <- sort(colnames(x)[vapply(FUN.VALUE = logical(1), x, is.mo)])[1] - } else if ("mo" %in% colnames(x) & - suppressWarnings( - all(x$mo %in% c(NA, microorganisms$mo)))) { + # take first column + found <- colnames(x)[vapply(FUN.VALUE = logical(1), x, is.mo)] + } else if ("mo" %in% colnames_formatted & + suppressWarnings(all(x$mo %in% c(NA, microorganisms$mo)))) { found <- "mo" - } else if (any(colnames(x) %like% "^(mo|microorganism|organism|bacteria|ba[ck]terie)s?$")) { - found <- sort(colnames(x)[colnames(x) %like% "^(mo|microorganism|organism|bacteria|ba[ck]terie)s?$"])[1] - } else if (any(colnames(x) %like% "^(microorganism|organism|bacteria|ba[ck]terie)")) { - found <- sort(colnames(x)[colnames(x) %like% "^(microorganism|organism|bacteria|ba[ck]terie)"])[1] - } else if (any(colnames(x) %like% "species")) { - found <- sort(colnames(x)[colnames(x) %like% "species"])[1] + } else if (any(colnames_formatted %like_case% "^(mo|microorganism|organism|bacteria|ba[ck]terie)s?$")) { + found <- sort(colnames(x)[colnames_formatted %like_case% "^(mo|microorganism|organism|bacteria|ba[ck]terie)s?$"]) + } else if (any(colnames_formatted %like_case% "^(microorganism|organism|bacteria|ba[ck]terie)")) { + found <- sort(colnames(x)[colnames_formatted %like_case% "^(microorganism|organism|bacteria|ba[ck]terie)"]) + } else if (any(colnames_formatted %like_case% "species")) { + found <- sort(colnames(x)[colnames_formatted %like_case% "species"]) } } # -- key antibiotics if (type %in% c("keyantibiotics", "keyantimicrobials")) { - if (any(colnames(x) %like% "^key.*(ab|antibiotics|antimicrobials)")) { - found <- sort(colnames(x)[colnames(x) %like% "^key.*(ab|antibiotics|antimicrobials)"])[1] + if (any(colnames_formatted %like_case% "^key.*(ab|antibiotics|antimicrobials)")) { + found <- sort(colnames(x)[colnames_formatted %like_case% "^key.*(ab|antibiotics|antimicrobials)"]) } } # -- date if (type == "date") { - if (any(colnames(x) %like% "^(specimen date|specimen_date|spec_date)")) { + if (any(colnames_formatted %like_case% "^(specimen date|specimen_date|spec_date)")) { # WHONET support - found <- sort(colnames(x)[colnames(x) %like% "^(specimen date|specimen_date|spec_date)"])[1] + found <- sort(colnames(x)[colnames_formatted %like_case% "^(specimen date|specimen_date|spec_date)"]) if (!any(class(pm_pull(x, found)) %in% c("Date", "POSIXct"))) { stop(font_red(paste0("Found column '", font_bold(found), "' to be used as input for `col_", type, "`, but this column contains no valid dates. Transform its values to valid dates first.")), call. = FALSE) } + } else if (any(vapply(FUN.VALUE = logical(1), x, function(x) inherits(x, c("Date", "POSIXct"))))) { - found <- sort(colnames(x)[vapply(FUN.VALUE = logical(1), x, function(x) inherits(x, c("Date", "POSIXct")))])[1] + # take first column + found <- colnames(x)[vapply(FUN.VALUE = logical(1), x, function(x) inherits(x, c("Date", "POSIXct")))] } } # -- patient id if (type == "patient_id") { - if (any(colnames(x) %like% "^(identification |patient|patid)")) { - found <- sort(colnames(x)[colnames(x) %like% "^(identification |patient|patid)"])[1] + crit1 <- colnames_formatted %like_case% "^(patient|patid)" + if (any(crit1)) { + found <- colnames(x)[crit1] + } else { + crit2 <- colnames_formatted %like_case% "(identification |patient|pat.*id)" + if (any(crit2)) { + found <- colnames(x)[crit2] + } } } # -- specimen if (type == "specimen") { - if (any(colnames(x) %like% "(specimen type|spec_type)")) { - found <- sort(colnames(x)[colnames(x) %like% "(specimen type|spec_type)"])[1] - } else if (any(colnames(x) %like% "^(specimen)")) { - found <- sort(colnames(x)[colnames(x) %like% "^(specimen)"])[1] + if (any(colnames_formatted %like_case% "(specimen type|spec_type)")) { + found <- sort(colnames(x)[colnames_formatted %like_case% "(specimen type|spec_type)"]) + } else if (any(colnames_formatted %like_case% "^(specimen)")) { + found <- sort(colnames(x)[colnames_formatted %like_case% "^(specimen)"]) } } # -- UTI (urinary tract infection) if (type == "uti") { - if (any(colnames(x) == "uti")) { - found <- colnames(x)[colnames(x) == "uti"][1] - } else if (any(colnames(x) %like% "(urine|urinary)")) { - found <- sort(colnames(x)[colnames(x) %like% "(urine|urinary)"])[1] + if (any(colnames_formatted == "uti")) { + found <- colnames(x)[colnames_formatted == "uti"] + } else if (any(colnames_formatted %like_case% "(urine|urinary)")) { + found <- sort(colnames(x)[colnames_formatted %like_case% "(urine|urinary)"]) } if (!is.null(found)) { # this column should contain logicals @@ -241,10 +249,12 @@ search_type_in_df <- function(x, type, info = TRUE) { } } + found <- found[1] + if (!is.null(found) & info == TRUE) { if (message_not_thrown_before(fn = paste0("search_", type))) { msg <- paste0("Using column '", font_bold(found), "' as input for `col_", type, "`.") - if (type %in% c("keyantibiotics", "specimen")) { + if (type %in% c("keyantibiotics", "keyantimicrobials", "specimen")) { msg <- paste(msg, "Use", font_bold(paste0("col_", type), "= FALSE"), "to prevent this.") } message_(msg) @@ -696,7 +706,7 @@ meet_criteria <- function(object, ifelse(!is.null(has_length) && length(has_length) == 1 && has_length == 1, "be a finite number", "all be finite numbers"), - " (i.e., not be infinite)", + " (i.e. not be infinite)", call = call_depth) } if (!is.null(contains_column_class)) { @@ -713,13 +723,7 @@ meet_criteria <- function(object, return(invisible()) } -get_current_data <- function(arg_name, call, reuse_from_1st_call = TRUE) { - # check if retrieved before, then get it from package environment to improve speed - if (reuse_from_1st_call == TRUE && - identical(unique_call_id(entire_session = FALSE), pkg_env$get_current_data.call)) { - return(pkg_env$get_current_data.out) - } - +get_current_data <- function(arg_name, call) { # try dplyr::cur_data_all() first to support dplyr groups # only useful for e.g. dplyr::filter(), dplyr::mutate() and dplyr::summarise() # not useful (throws error) with e.g. dplyr::select() - but that will be caught later in this function @@ -727,73 +731,32 @@ get_current_data <- function(arg_name, call, reuse_from_1st_call = TRUE) { if (!is.null(cur_data_all)) { out <- tryCatch(cur_data_all(), error = function(e) NULL) if (is.data.frame(out)) { - out <- structure(out, type = "dplyr_cur_data_all") - pkg_env$get_current_data.call <- unique_call_id(entire_session = FALSE) - pkg_env$get_current_data.out <- out - return(out) - } - } - - if (getRversion() < "3.2") { - # R-3.0 and R-3.1 do not have an `x` element in the call stack, rendering this function useless - # R-3.2 was released in April 2015 - if (is.na(arg_name)) { - # such as for carbapenems() etc. - warning_("this function requires R version 3.2 or later - you have ", R.version.string, call = call) - return(data.frame()) - } else { - # mimic a default R error, e.g. for example_isolates[which(mo_name() %like% "^ent"), ] - stop_("argument `", arg_name, "` is missing with no default", call = call) + return(structure(out, type = "dplyr_cur_data_all")) } } - # try a (base R) method, by going over the complete system call stack with sys.frames() - not_set <- TRUE - source <- "base_R" - frms <- lapply(sys.frames(), function(el) { - if (not_set == TRUE && ".Generic" %in% names(el)) { - if (tryCatch(".data" %in% names(el) && is.data.frame(el$`.data`), error = function(e) FALSE)) { - # - - - - - # dplyr - # - - - - - # an element `.data` will be in the system call stack when using dplyr::select() - # [but not when using dplyr::filter(), dplyr::mutate() or dplyr::summarise()] - not_set <<- FALSE - source <<- "dplyr_selector" - el$`.data` - } else if (tryCatch(any(c("x", "xx") %in% names(el)), error = function(e) FALSE)) { - # - - - - - # base R - # - - - - - # an element `x` will be in this environment for only cols, e.g. `example_isolates[, carbapenems()]` - # an element `xx` will be in this environment for rows + cols, e.g. `example_isolates[c(1:3), carbapenems()]` - if (tryCatch(is.data.frame(el$xx), error = function(e) FALSE)) { - not_set <<- FALSE - el$xx - } else if (tryCatch(is.data.frame(el$x))) { - not_set <<- FALSE - el$x - } else { - NULL - } - } else { - NULL + # try a manual (base R) method, by going over all underlying environments with sys.frames() + for (el in sys.frames()) { + if (!is.null(el$`.Generic`)) { + # don't check `".Generic" %in% names(el)`, because in R < 3.2, `names(el)` is always NULL + + if (!is.null(el$`.data`) && is.data.frame(el$`.data`)) { + # an element `.data` will be in the environment when using `dplyr::select()` + # (but not when using `dplyr::filter()`, `dplyr::mutate()` or `dplyr::summarise()`) + return(structure(el$`.data`, type = "dplyr_selector")) + + } else if (!is.null(el$xx) && is.data.frame(el$xx)) { + # an element `xx` will be in the environment for rows + cols, e.g. `example_isolates[c(1:3), carbapenems()]` + return(structure(el$xx, type = "base_R")) + + } else if (!is.null(el$x) && is.data.frame(el$x)) { + # an element `x` will be in the environment for only cols, e.g. `example_isolates[, carbapenems()]` + return(structure(el$x, type = "base_R")) } - } else { - NULL } - }) - - # lookup the matched frame and return its value: a data.frame - vars_df <- tryCatch(frms[[which(!vapply(FUN.VALUE = logical(1), frms, is.null))]], error = function(e) NULL) - if (is.data.frame(vars_df)) { - out <- structure(vars_df, type = source) - pkg_env$get_current_data.call <- unique_call_id(entire_session = FALSE) - pkg_env$get_current_data.out <- out - return(out) } - # nothing worked, so: + # no data.frame found, so an error must be returned: if (is.na(arg_name)) { if (isTRUE(is.numeric(call))) { fn <- as.character(sys.call(call + 1)[1]) @@ -982,8 +945,8 @@ font_grey_bg <- function(..., collapse = " ") { # similar to HTML #444444 try_colour(..., before = "\033[48;5;238m", after = "\033[49m", collapse = collapse) } else { - # similar to HTML #eeeeee - try_colour(..., before = "\033[48;5;254m", after = "\033[49m", collapse = collapse) + # similar to HTML #f0f0f0 + try_colour(..., before = "\033[48;5;255m", after = "\033[49m", collapse = collapse) } } font_green_bg <- function(..., collapse = " ") { diff --git a/R/ab_class_selectors.R b/R/ab_class_selectors.R index 18fa70bf..5b40a715 100644 --- a/R/ab_class_selectors.R +++ b/R/ab_class_selectors.R @@ -25,13 +25,12 @@ #' Antibiotic Class Selectors #' -#' These functions allow for filtering rows and selecting columns based on antibiotic test results that are of a specific antibiotic class, without the need to define the columns or antibiotic abbreviations. \strong{\Sexpr{ifelse(getRversion() < "3.2", paste0("NOTE: THESE FUNCTIONS DO NOT WORK ON YOUR CURRENT R VERSION. These functions require R version 3.2 or later - you have ", R.version.string, "."), "")}} +#' These functions allow for filtering rows and selecting columns based on antibiotic test results that are of a specific antibiotic class, without the need to define the columns or antibiotic abbreviations. #' @inheritSection lifecycle Stable Lifecycle #' @param ab_class an antimicrobial class, such as `"carbapenems"`. The columns `group`, `atc_group1` and `atc_group2` of the [antibiotics] data set will be searched (case-insensitive) for this value. #' @param only_rsi_columns a [logical] to indicate whether only columns of class `` must be selected (defaults to `FALSE`), see [as.rsi()] -#' @details \strong{\Sexpr{ifelse(getRversion() < "3.2", paste0("NOTE: THESE FUNCTIONS DO NOT WORK ON YOUR CURRENT R VERSION. These functions require R version 3.2 or later - you have ", R.version.string, "."), "")}} -#' -#' These functions can be used in data set calls for selecting columns and filtering rows, see *Examples*. They support base R, but work more convenient in dplyr functions such as [`select()`][dplyr::select()], [`filter()`][dplyr::filter()] and [`summarise()`][dplyr::summarise()]. +#' @details +#' These functions can be used in data set calls for selecting columns and filtering rows. They are heavily inspired by the [Tidyverse selection helpers](https://tidyselect.r-lib.org/reference/language.html), but also work in base \R and not only in `dplyr` verbs. Nonetheless, they are very convenient to use with `dplyr` functions such as [`select()`][dplyr::select()], [`filter()`][dplyr::filter()] and [`summarise()`][dplyr::summarise()], see *Examples*. #' #' All columns in the data in which these functions are called will be searched for known antibiotic names, abbreviations, brand names, and codes (ATC, EARS-Net, WHO, etc.) in the [antibiotics] data set. This means that a selector such as [aminoglycosides()] will pick up column names like 'gen', 'genta', 'J01GB03', 'tobra', 'Tobracin', etc. Use the [ab_class()] function to filter/select on a manually defined antibiotic class. #' @@ -267,18 +266,9 @@ ab_selector <- function(function_name, meet_criteria(function_name, allow_class = "character", has_length = 1, allow_NULL = TRUE, .call_depth = 1) meet_criteria(only_rsi_columns, allow_class = "logical", has_length = 1, .call_depth = 1) meet_criteria(ab_class, allow_class = "character", has_length = 1, allow_NULL = TRUE, .call_depth = 1) - - if (getRversion() < "3.2") { - # get_current_data() does not work on R 3.0 and R 3.1. - # R 3.2 was released in April 2015. - warning_("antibiotic class selectors such as ", function_name, - "() require R version 3.2 or later - you have ", R.version.string, - call = FALSE) - return(NULL) - } - + # get_current_data() has to run each time, for cases where e.g., filter() and select() are used in same call - vars_df <- get_current_data(arg_name = NA, call = -3, reuse_from_1st_call = FALSE) + vars_df <- get_current_data(arg_name = NA, call = -3) # to improve speed, get_column_abx() will only run once when e.g. in a select or group call ab_in_data <- get_column_abx(vars_df, info = FALSE, only_rsi_columns = only_rsi_columns, sort = FALSE) @@ -315,12 +305,15 @@ ab_selector <- function(function_name, } else { agents_formatted <- paste0("'", font_bold(agents, collapse = NULL), "'") agents_names <- ab_name(names(agents), tolower = TRUE, language = NULL) - need_name <- tolower(gsub("[^a-zA-Z]", "", agents)) != tolower(gsub("[^a-zA-Z]", "", agents_names)) - agents_formatted[need_name] <- paste0(agents_formatted[need_name], - " (", agents_names[need_name], ")") - message_("For `", function_name, "(", ifelse(function_name == "ab_class", paste0("\"", ab_class, "\""), ""), ")` using ", + need_name <- generalise_antibiotic_name(agents) != generalise_antibiotic_name(agents_names) + agents_formatted[need_name] <- paste0(agents_formatted[need_name], " (", agents_names[need_name], ")") + message_("For `", function_name, "(", + ifelse(function_name == "ab_class", + paste0("\"", ab_class, "\""), + ""), + ")` using ", ifelse(length(agents) == 1, "column: ", "columns: "), - vector_and(agents_formatted, quotes = FALSE)) + vector_and(agents_formatted, quotes = FALSE, sort = FALSE)) } remember_thrown_message(function_name) } diff --git a/R/deprecated.R b/R/deprecated.R index 561644ac..210f8bf3 100755 --- a/R/deprecated.R +++ b/R/deprecated.R @@ -62,7 +62,7 @@ filter_first_weighted_isolate <- function(x = NULL, if (is_null_or_grouped_tbl(x)) { # when `x` is left blank, auto determine it (get_current_data() also contains dplyr::cur_data_all()) # is also fix for using a grouped df as input (a dot as first argument) - x <- tryCatch(get_current_data(arg_name = "x", call = -2, reuse_from_1st_call = FALSE), error = function(e) x) + x <- tryCatch(get_current_data(arg_name = "x", call = -2), error = function(e) x) } meet_criteria(x, allow_class = "data.frame") # also checks dimensions to be >0 meet_criteria(col_date, allow_class = "character", has_length = 1, allow_NULL = TRUE, is_in = colnames(x)) @@ -104,7 +104,7 @@ key_antibiotics <- function(x = NULL, if (is_null_or_grouped_tbl(x)) { # when `x` is left blank, auto determine it (get_current_data() also contains dplyr::cur_data_all()) # is also fix for using a grouped df as input (a dot as first argument) - x <- tryCatch(get_current_data(arg_name = "x", call = -2, reuse_from_1st_call = FALSE), error = function(e) x) + x <- tryCatch(get_current_data(arg_name = "x", call = -2), error = function(e) x) } key_antimicrobials(x = x, @@ -170,7 +170,7 @@ filter_ab_class <- function(x, if (missing(x) || is_null_or_grouped_tbl(x)) { # when `x` is left blank, auto determine it (get_current_data() also contains dplyr::cur_data_all()) # is also fix for using a grouped df as input (a dot as first argument) - x <- get_current_data(arg_name = "x", call = -2 - .call_depth, reuse_from_1st_call = FALSE) + x <- get_current_data(arg_name = "x", call = -2 - .call_depth) .x_name <- "your_data" } meet_criteria(x, allow_class = "data.frame", .call_depth = .call_depth) diff --git a/R/first_isolate.R b/R/first_isolate.R index 892bc74a..0243d9c2 100755 --- a/R/first_isolate.R +++ b/R/first_isolate.R @@ -131,11 +131,8 @@ #' # `example_isolates` is a data set available in the AMR package. #' # See ?example_isolates. #' -#' example_isolates[first_isolate(example_isolates), ] -#' \donttest{ -#' # faster way, only works in R 3.2 and later: #' example_isolates[first_isolate(), ] -#' +#' \donttest{ #' # get all first Gram-negatives #' example_isolates[which(first_isolate() & mo_is_gram_negative()), ] #' @@ -207,7 +204,7 @@ first_isolate <- function(x = NULL, if (is_null_or_grouped_tbl(x)) { # when `x` is left blank, auto determine it (get_current_data() also contains dplyr::cur_data_all()) # is also fix for using a grouped df as input (a dot as first argument) - x <- tryCatch(get_current_data(arg_name = "x", call = -2, reuse_from_1st_call = FALSE), error = function(e) x) + x <- tryCatch(get_current_data(arg_name = "x", call = -2), error = function(e) x) } meet_criteria(x, allow_class = "data.frame") # also checks dimensions to be >0 meet_criteria(col_date, allow_class = "character", has_length = 1, allow_NULL = TRUE, is_in = colnames(x)) @@ -618,7 +615,7 @@ filter_first_isolate <- function(x = NULL, if (is_null_or_grouped_tbl(x)) { # when `x` is left blank, auto determine it (get_current_data() also contains dplyr::cur_data_all()) # is also fix for using a grouped df as input (a dot as first argument) - x <- tryCatch(get_current_data(arg_name = "x", call = -2, reuse_from_1st_call = FALSE), error = function(e) x) + x <- tryCatch(get_current_data(arg_name = "x", call = -2), error = function(e) x) } meet_criteria(x, allow_class = "data.frame") # also checks dimensions to be >0 meet_criteria(col_date, allow_class = "character", has_length = 1, allow_NULL = TRUE, is_in = colnames(x)) diff --git a/R/join_microorganisms.R b/R/join_microorganisms.R index 21975d42..88f73f30 100755 --- a/R/join_microorganisms.R +++ b/R/join_microorganisms.R @@ -36,7 +36,7 @@ #' @param ... ignored, only in place to allow future extensions #' @details **Note:** As opposed to the `join()` functions of `dplyr`, [character] vectors are supported and at default existing columns will get a suffix `"2"` and the newly joined columns will not get a suffix. #' -#' If the `dplyr` package is installed, their join functions will be used. Otherwise, the much slower [merge()] and [interaction()] functions from base R will be used. +#' If the `dplyr` package is installed, their join functions will be used. Otherwise, the much slower [merge()] and [interaction()] functions from base \R will be used. #' @inheritSection AMR Read more on Our Website! #' @return a [data.frame] #' @export diff --git a/R/key_antimicrobials.R b/R/key_antimicrobials.R index 7e35c68a..c1c2b1d2 100755 --- a/R/key_antimicrobials.R +++ b/R/key_antimicrobials.R @@ -130,7 +130,7 @@ key_antimicrobials <- function(x = NULL, if (is_null_or_grouped_tbl(x)) { # when `x` is left blank, auto determine it (get_current_data() also contains dplyr::cur_data_all()) # is also fix for using a grouped df as input (a dot as first argument) - x <- tryCatch(get_current_data(arg_name = "x", call = -2, reuse_from_1st_call = FALSE), error = function(e) x) + x <- tryCatch(get_current_data(arg_name = "x", call = -2), error = function(e) x) } meet_criteria(x, allow_class = "data.frame") # also checks dimensions to be >0 meet_criteria(col_mo, allow_class = "character", has_length = 1, allow_NULL = TRUE, allow_NA = TRUE, is_in = colnames(x)) @@ -232,7 +232,7 @@ all_antimicrobials <- function(x = NULL, if (is_null_or_grouped_tbl(x)) { # when `x` is left blank, auto determine it (get_current_data() also contains dplyr::cur_data_all()) # is also fix for using a grouped df as input (a dot as first argument) - x <- tryCatch(get_current_data(arg_name = "x", call = -2, reuse_from_1st_call = FALSE), error = function(e) x) + x <- tryCatch(get_current_data(arg_name = "x", call = -2), error = function(e) x) } meet_criteria(x, allow_class = "data.frame") # also checks dimensions to be >0 meet_criteria(only_rsi_columns, allow_class = "logical", has_length = 1) diff --git a/R/mdro.R b/R/mdro.R index edf4010e..fb3d0a37 100755 --- a/R/mdro.R +++ b/R/mdro.R @@ -170,7 +170,7 @@ mdro <- function(x = NULL, if (is_null_or_grouped_tbl(x)) { # when `x` is left blank, auto determine it (get_current_data() also contains dplyr::cur_data_all()) # is also fix for using a grouped df as input (a dot as first argument) - x <- tryCatch(get_current_data(arg_name = "x", call = -2, reuse_from_1st_call = FALSE), error = function(e) x) + x <- tryCatch(get_current_data(arg_name = "x", call = -2), error = function(e) x) } meet_criteria(x, allow_class = "data.frame") # also checks dimensions to be >0 meet_criteria(guideline, allow_class = c("list", "character"), allow_NULL = TRUE) diff --git a/R/mo.R b/R/mo.R index 9bcdee85..30cae30d 100755 --- a/R/mo.R +++ b/R/mo.R @@ -1664,9 +1664,7 @@ pillar_shaft.mo <- function(x, ...) { out[is.na(x)] <- font_na(" NA") out[x == "UNKNOWN"] <- font_na(" UNKNOWN") - df <- tryCatch(get_current_data(arg_name = "x", - call = 0, - reuse_from_1st_call = FALSE), + df <- tryCatch(get_current_data(arg_name = "x", call = 0), error = function(e) NULL) if (!is.null(df)) { mo_cols <- vapply(FUN.VALUE = logical(1), df, is.mo) diff --git a/R/mo_property.R b/R/mo_property.R index 419dc0f9..02484cf9 100755 --- a/R/mo_property.R +++ b/R/mo_property.R @@ -747,16 +747,14 @@ mo_validate <- function(x, property, language, ...) { find_mo_col <- function(fn) { # this function tries to find an mo column in the data the function was called in, # which is useful when functions are used within dplyr verbs - df <- get_current_data(arg_name = "x", - call = -3, - reuse_from_1st_call = FALSE) # will return an error if not found + df <- get_current_data(arg_name = "x", call = -3) # will return an error if not found mo <- NULL try({ mo <- suppressMessages(search_type_in_df(df, "mo")) }, silent = TRUE) if (!is.null(df) && !is.null(mo) && is.data.frame(df)) { if (message_not_thrown_before(fn = fn)) { - message_("Using column '", font_bold(mo), "' as input for ", fn, "()") + message_("Using column '", font_bold(mo), "' as input for `", fn, "()`") remember_thrown_message(fn = fn) } return(df[, mo, drop = TRUE]) diff --git a/R/plot.R b/R/plot.R index 22892b98..a60a69dc 100644 --- a/R/plot.R +++ b/R/plot.R @@ -25,7 +25,7 @@ #' Plotting for Classes `rsi`, `mic` and `disk` #' -#' Functions to plot classes `rsi`, `mic` and `disk`, with support for base R and `ggplot2`. +#' Functions to plot classes `rsi`, `mic` and `disk`, with support for base \R and `ggplot2`. #' @inheritSection lifecycle Stable Lifecycle #' @inheritSection AMR Read more on Our Website! #' @param x,data MIC values created with [as.mic()] or disk diffusion values created with [as.disk()] diff --git a/R/random.R b/R/random.R index f403901b..8ac2988d 100644 --- a/R/random.R +++ b/R/random.R @@ -32,7 +32,7 @@ #' @param ab any [character] that can be coerced to a valid antimicrobial agent code with [as.ab()] #' @param prob_RSI a vector of length 3: the probabilities for R (1st value), S (2nd value) and I (3rd value) #' @param ... ignored, only in place to allow future extensions -#' @details The base R function [sample()] is used for generating values. +#' @details The base \R function [sample()] is used for generating values. #' #' Generated values are based on the latest EUCAST guideline implemented in the [rsi_translation] data set. To create specific generated values per bug or drug, set the `mo` and/or `ab` argument. #' @return class `` for [random_mic()] (see [as.mic()]) and class `` for [random_disk()] (see [as.disk()]) @@ -56,18 +56,26 @@ #' random_disk(100, "Streptococcus pneumoniae", "ampicillin") # range 12-27 #' } random_mic <- function(size, mo = NULL, ab = NULL, ...) { + meet_criteria(size, allow_class = c("numeric", "integer"), has_length = 1, is_positive = TRUE, is_finite = TRUE) + meet_criteria(mo, allow_class = "character", has_length = 1, allow_NULL = TRUE) + meet_criteria(ab, allow_class = "character", has_length = 1, allow_NULL = TRUE) random_exec("MIC", size = size, mo = mo, ab = ab) } #' @rdname random #' @export random_disk <- function(size, mo = NULL, ab = NULL, ...) { + meet_criteria(size, allow_class = c("numeric", "integer"), has_length = 1, is_positive = TRUE, is_finite = TRUE) + meet_criteria(mo, allow_class = "character", has_length = 1, allow_NULL = TRUE) + meet_criteria(ab, allow_class = "character", has_length = 1, allow_NULL = TRUE) random_exec("DISK", size = size, mo = mo, ab = ab) } #' @rdname random #' @export random_rsi <- function(size, prob_RSI = c(0.33, 0.33, 0.33), ...) { + meet_criteria(size, allow_class = c("numeric", "integer"), has_length = 1, is_positive = TRUE, is_finite = TRUE) + meet_criteria(prob_RSI, allow_class = c("numeric", "integer"), has_length = 3) sample(as.rsi(c("R", "S", "I")), size = size, replace = TRUE, prob = prob_RSI) } diff --git a/R/rsi.R b/R/rsi.R index cc5fa42c..308c4274 100755 --- a/R/rsi.R +++ b/R/rsi.R @@ -349,7 +349,7 @@ as.rsi.mic <- function(x, # for dplyr's across() cur_column_dplyr <- import_fn("cur_column", "dplyr", error_on_fail = FALSE) - if (!is.null(cur_column_dplyr) && tryCatch(is.data.frame(get_current_data("ab", call = 0, reuse_from_1st_call = FALSE)), error = function(e) FALSE)) { + if (!is.null(cur_column_dplyr) && tryCatch(is.data.frame(get_current_data("ab", call = 0)), error = function(e) FALSE)) { # try to get current column, which will only be available when in across() ab <- tryCatch(cur_column_dplyr(), error = function(e) ab) @@ -438,7 +438,7 @@ as.rsi.disk <- function(x, # for dplyr's across() cur_column_dplyr <- import_fn("cur_column", "dplyr", error_on_fail = FALSE) - if (!is.null(cur_column_dplyr) && tryCatch(is.data.frame(get_current_data("ab", call = 0, reuse_from_1st_call = FALSE)), error = function(e) FALSE)) { + if (!is.null(cur_column_dplyr) && tryCatch(is.data.frame(get_current_data("ab", call = 0)), error = function(e) FALSE)) { # try to get current column, which will only be available when in across() ab <- tryCatch(cur_column_dplyr(), error = function(e) ab) @@ -448,7 +448,7 @@ as.rsi.disk <- function(x, mo_var_found <- "" if (is.null(mo)) { tryCatch({ - df <- get_current_data(arg_name = "mo", call = -3, reuse_from_1st_call = FALSE) # will return an error if not found + df <- get_current_data(arg_name = "mo", call = -3) # will return an error if not found mo <- NULL try({ mo <- suppressMessages(search_type_in_df(df, "mo")) diff --git a/data-raw/AMR_latest.tar.gz b/data-raw/AMR_latest.tar.gz index 328e744a..e574aea9 100644 Binary files a/data-raw/AMR_latest.tar.gz and b/data-raw/AMR_latest.tar.gz differ diff --git a/data-raw/_install_deps.R b/data-raw/_install_deps.R index 42aac100..78762f55 100644 --- a/data-raw/_install_deps.R +++ b/data-raw/_install_deps.R @@ -24,8 +24,8 @@ # ==================================================================== # # some old R instances have trouble installing tinytest, so we ship it too -install.packages("data-raw/tinytest_1.2.4.10.tar.gz") -install.packages("data-raw/AMR_latest.tar.gz", dependencies = FALSE) +install.packages("data-raw/tinytest_1.2.4.10.tar.gz", repos = "https://cran.rstudio.com/", type = "source") +install.packages("data-raw/AMR_latest.tar.gz", repos = "https://cran.rstudio.com/", type = "source", dependencies = FALSE) pkg_suggests <- gsub("[^a-zA-Z0-9]+", "", unlist(strsplit(packageDescription("AMR", fields = "Suggests"), ", ?"))) cat("Packages listed in Suggests:", paste(pkg_suggests, collapse = ", "), "\n") diff --git a/docs/404.html b/docs/404.html index a8039e26..889cf6fb 100644 --- a/docs/404.html +++ b/docs/404.html @@ -81,7 +81,7 @@ AMR (for R) - 1.7.1.9004 + 1.7.1.9005 diff --git a/docs/LICENSE-text.html b/docs/LICENSE-text.html index 36fbf52a..e6725334 100644 --- a/docs/LICENSE-text.html +++ b/docs/LICENSE-text.html @@ -81,7 +81,7 @@ AMR (for R) - 1.7.1.9004 + 1.7.1.9005 diff --git a/docs/articles/datasets.html b/docs/articles/datasets.html index 3a85c03f..9cd2cede 100644 --- a/docs/articles/datasets.html +++ b/docs/articles/datasets.html @@ -39,7 +39,7 @@ AMR (for R) - 1.7.1.9004 + 1.7.1.9005 @@ -192,7 +192,7 @@ diff --git a/docs/authors.html b/docs/authors.html index 865cc4a8..e28124f9 100644 --- a/docs/authors.html +++ b/docs/authors.html @@ -81,7 +81,7 @@ AMR (for R) - 1.7.1.9004 + 1.7.1.9005 diff --git a/docs/index.html b/docs/index.html index e45f10f1..994b266e 100644 --- a/docs/index.html +++ b/docs/index.html @@ -42,7 +42,7 @@ AMR (for R) - 1.7.1.9004 + 1.7.1.9005 @@ -217,16 +217,12 @@ library(dplyr) example_isolates %>% - mutate(bacteria = mo_fullname(mo)) %>% - filter(mo_is_gram_negative(), mo_is_intrinsic_resistant(ab = "cefotax")) %>% - select(bacteria, aminoglycosides(), carbapenems()) -#> ℹ Using column 'mo' as input for `mo_is_gram_negative()` -#> ℹ Using column 'mo' as input for `mo_is_intrinsic_resistant()` -#> ℹ Determining intrinsic resistance based on 'EUCAST Expert Rules' and 'EUCAST Intrinsic -#> Resistance and Unusual Phenotypes' v3.2 (2020) -#> ℹ For `aminoglycosides()` using columns: 'AMK' (amikacin), 'GEN' (gentamicin), 'KAN' -#> (kanamycin) and 'TOB' (tobramycin) -#> ℹ For `carbapenems()` using columns: 'IPM' (imipenem) and 'MEM' (meropenem) + mutate(bacteria = mo_fullname()) %>% + filter(mo_is_gram_negative(), + mo_is_intrinsic_resistant(ab = "cefotax")) %>% + select(bacteria, + aminoglycosides(), + carbapenems())

With only having defined a row filter on Gram-negative bacteria with intrinsic resistance to cefotaxime (mo_is_gram_negative() and mo_is_intrinsic_resistant()) and a column selection on two antibiotic groups (aminoglycosides() and carbapenems()), the reference data about all microorganisms and all antibiotics in the AMR package make sure you get what you meant:

@@ -386,7 +382,7 @@

Latest development version

-

R-code-checkCodeFactorCodecov

+

[R-code-check][https://github.com/msberends/AMR/workflows/R-code-check/badge.svg?branch=master](https://github.com/msberends/AMR/actions) [CodeFactor][https://www.codefactor.io/repository/github/msberends/amr/badge](https://www.codefactor.io/repository/github/msberends/amr) [Codecov][https://codecov.io/gh/msberends/AMR/branch/master/graph/badge.svg](https://codecov.io/gh/msberends/AMR?branch=master)

The latest and unpublished development version can be installed from GitHub in two ways:

  1. diff --git a/docs/news/index.html b/docs/news/index.html index a7fb950b..b1403661 100644 --- a/docs/news/index.html +++ b/docs/news/index.html @@ -81,7 +81,7 @@ AMR (for R) - 1.7.1.9004 + 1.7.1.9005
@@ -236,24 +236,34 @@ Source: NEWS.md -
-

- Unreleased AMR 1.7.1.9004

-
+
+

+ Unreleased AMR 1.7.1.9005

+

-Last updated: 15 June 2021 +Last updated: 22 June 2021

Changed

@@ -307,7 +317,7 @@
  • Function betalactams() as additional antbiotic column selector and function filter_betalactams() as additional antbiotic column filter. The group of betalactams consists of all carbapenems, cephalosporins and penicillins.
  • -
  • A ggplot() method for resistance_predict() +
  • A ggplot() method for resistance_predict()
  • @@ -408,7 +418,7 @@ #> Filtering on oxazolidinones: value in column `LNZ` (linezolid) is either "R", "S" or "I"
  • Support for custom MDRO guidelines, using the new custom_mdro_guideline() function, please see mdro() for additional info

  • -
  • ggplot() generics for classes <mic> and <disk>

  • +
  • ggplot() generics for classes <mic> and <disk>

  • Function mo_is_yeast(), which determines whether a microorganism is a member of the taxonomic class Saccharomycetes or the taxonomic order Saccharomycetales:

    @@ -465,7 +475,7 @@
     
  • Plotting of MIC and disk diffusion values now support interpretation colouring if you supply the microorganism and antimicrobial agent
  • All colours were updated to colour-blind friendly versions for values R, S and I for all plot methods (also applies to tibble printing)
  • Interpretation of MIC and disk diffusion values to R/SI will now be translated if the system language is German, Dutch or Spanish (see translate)
  • -
  • Plotting is now possible with base R using plot() and with ggplot2 using ggplot() on any vector of MIC and disk diffusion values
  • +
  • Plotting is now possible with base R using plot() and with ggplot2 using ggplot() on any vector of MIC and disk diffusion values
  • Updated SNOMED codes to US Edition of SNOMED CT from 1 September 2020 and added the source to the help page of the microorganisms data set
  • diff --git a/docs/pkgdown.yml b/docs/pkgdown.yml index e4790d59..613ff48d 100644 --- a/docs/pkgdown.yml +++ b/docs/pkgdown.yml @@ -12,7 +12,7 @@ articles: datasets: datasets.html resistance_predict: resistance_predict.html welcome_to_AMR: welcome_to_AMR.html -last_built: 2021-06-15T08:50Z +last_built: 2021-06-22T10:02Z urls: reference: https://msberends.github.io/AMR//reference article: https://msberends.github.io/AMR//articles diff --git a/docs/reference/antibiotic_class_selectors.html b/docs/reference/antibiotic_class_selectors.html index 8264b3ac..437e3b71 100644 --- a/docs/reference/antibiotic_class_selectors.html +++ b/docs/reference/antibiotic_class_selectors.html @@ -49,8 +49,7 @@ - + @@ -83,7 +82,7 @@ AMR (for R) - 1.7.1.9001 + 1.7.1.9005
    @@ -240,8 +239,7 @@
    -

    These functions allow for filtering rows and selecting columns based on antibiotic test results that are of a specific antibiotic class, without the need to define the columns or antibiotic abbreviations. -

    +

    These functions allow for filtering rows and selecting columns based on antibiotic test results that are of a specific antibiotic class, without the need to define the columns or antibiotic abbreviations.

    ab_class(ab_class, only_rsi_columns = FALSE)
    @@ -305,9 +303,7 @@
     
         

    Details

    -

    -

    -

    These functions can be used in data set calls for selecting columns and filtering rows, see Examples. They support base R, but work more convenient in dplyr functions such as select(), filter() and summarise().

    +

    These functions can be used in data set calls for selecting columns and filtering rows. They are heavily inspired by the Tidyverse selection helpers, but also work in base R and not only in dplyr verbs. Nonetheless, they are very convenient to use with dplyr functions such as select(), filter() and summarise(), see Examples.

    All columns in the data in which these functions are called will be searched for known antibiotic names, abbreviations, brand names, and codes (ATC, EARS-Net, WHO, etc.) in the antibiotics data set. This means that a selector such as aminoglycosides() will pick up column names like 'gen', 'genta', 'J01GB03', 'tobra', 'Tobracin', etc. Use the ab_class() function to filter/select on a manually defined antibiotic class.

    Full list of supported agents

    diff --git a/docs/reference/count.html b/docs/reference/count.html index 13e9e41a..a0caa797 100644 --- a/docs/reference/count.html +++ b/docs/reference/count.html @@ -83,7 +83,7 @@ count_resistant() should be used to count resistant isolates, count_susceptible( AMR (for R) - 1.7.1.9002 + 1.7.1.9005 diff --git a/docs/reference/first_isolate.html b/docs/reference/first_isolate.html index fd9006aa..d2bbe59d 100644 --- a/docs/reference/first_isolate.html +++ b/docs/reference/first_isolate.html @@ -83,7 +83,7 @@ AMR (for R) - 1.7.1 + 1.7.1.9005 @@ -457,11 +457,8 @@ The lifecycle of this function is stable# `example_isolates` is a data set available in the AMR package. # See ?example_isolates. -example_isolates[first_isolate(example_isolates), ] -# \donttest{ -# faster way, only works in R 3.2 and later: example_isolates[first_isolate(), ] - +# \donttest{ # get all first Gram-negatives example_isolates[which(first_isolate() & mo_is_gram_negative()), ] diff --git a/docs/reference/index.html b/docs/reference/index.html index ffbd55b0..63525396 100644 --- a/docs/reference/index.html +++ b/docs/reference/index.html @@ -81,7 +81,7 @@ AMR (for R) - 1.7.1.9004 + 1.7.1.9005 diff --git a/docs/reference/join.html b/docs/reference/join.html index 96f89dee..583bbac7 100644 --- a/docs/reference/join.html +++ b/docs/reference/join.html @@ -82,7 +82,7 @@ AMR (for R) - 1.7.1 + 1.7.1.9005 @@ -281,7 +281,7 @@

    Details

    Note: As opposed to the join() functions of dplyr, character vectors are supported and at default existing columns will get a suffix "2" and the newly joined columns will not get a suffix.

    -

    If the dplyr package is installed, their join functions will be used. Otherwise, the much slower merge() and interaction() functions from base R will be used.

    +

    If the dplyr package is installed, their join functions will be used. Otherwise, the much slower merge() and interaction() functions from base R will be used.

    Stable Lifecycle

    diff --git a/docs/reference/plot.html b/docs/reference/plot.html index 9455c025..246a176e 100644 --- a/docs/reference/plot.html +++ b/docs/reference/plot.html @@ -82,7 +82,7 @@ AMR (for R) - 1.7.1.9002 + 1.7.1.9005 @@ -239,7 +239,7 @@
    -

    Functions to plot classes rsi, mic and disk, with support for base R and ggplot2.

    +

    Functions to plot classes rsi, mic and disk, with support for base R and ggplot2.

    # S3 method for mic
    diff --git a/docs/reference/random.html b/docs/reference/random.html
    index 2651574c..b199b497 100644
    --- a/docs/reference/random.html
    +++ b/docs/reference/random.html
    @@ -82,7 +82,7 @@
           
           
             AMR (for R)
    -        1.7.1
    +        1.7.1.9005
           
         
     
    @@ -278,7 +278,7 @@
         

    class <mic> for random_mic() (see as.mic()) and class <disk> for random_disk() (see as.disk())

    Details

    -

    The base R function sample() is used for generating values.

    +

    The base R function sample() is used for generating values.

    Generated values are based on the latest EUCAST guideline implemented in the rsi_translation data set. To create specific generated values per bug or drug, set the mo and/or ab argument.

    Stable Lifecycle

    diff --git a/docs/reference/resistance_predict.html b/docs/reference/resistance_predict.html index b27144fc..f0e2e8f4 100644 --- a/docs/reference/resistance_predict.html +++ b/docs/reference/resistance_predict.html @@ -82,7 +82,7 @@ AMR (for R) - 1.7.1.9002 + 1.7.1.9005 diff --git a/docs/survey.html b/docs/survey.html index 10de0e0c..ebb71e85 100644 --- a/docs/survey.html +++ b/docs/survey.html @@ -81,7 +81,7 @@ AMR (for R) - 1.7.1.9004 + 1.7.1.9005 diff --git a/index.md b/index.md index 5b39b463..10708d87 100644 --- a/index.md +++ b/index.md @@ -25,16 +25,12 @@ library(AMR) library(dplyr) example_isolates %>% - mutate(bacteria = mo_fullname(mo)) %>% - filter(mo_is_gram_negative(), mo_is_intrinsic_resistant(ab = "cefotax")) %>% - select(bacteria, aminoglycosides(), carbapenems()) -#> ℹ Using column 'mo' as input for `mo_is_gram_negative()` -#> ℹ Using column 'mo' as input for `mo_is_intrinsic_resistant()` -#> ℹ Determining intrinsic resistance based on 'EUCAST Expert Rules' and 'EUCAST Intrinsic -#> Resistance and Unusual Phenotypes' v3.2 (2020) -#> ℹ For `aminoglycosides()` using columns: 'AMK' (amikacin), 'GEN' (gentamicin), 'KAN' -#> (kanamycin) and 'TOB' (tobramycin) -#> ℹ For `carbapenems()` using columns: 'IPM' (imipenem) and 'MEM' (meropenem) + mutate(bacteria = mo_fullname()) %>% + filter(mo_is_gram_negative(), + mo_is_intrinsic_resistant(ab = "cefotax")) %>% + select(bacteria, + aminoglycosides(), + carbapenems()) ``` With only having defined a row filter on Gram-negative bacteria with intrinsic resistance to cefotaxime (`mo_is_gram_negative()` and `mo_is_intrinsic_resistant()`) and a column selection on two antibiotic groups (`aminoglycosides()` and `carbapenems()`), the reference data about [all microorganisms](./reference/microorganisms.html) and [all antibiotics](./reference/antibiotics.html) in the `AMR` package make sure you get what you meant: @@ -114,9 +110,9 @@ It will be downloaded and installed automatically. For RStudio, click on the men #### Latest development version -![R-code-check](https://github.com/msberends/AMR/workflows/R-code-check/badge.svg?branch=master) -![[CodeFactor](https://www.codefactor.io/repository/github/msberends/amr/badge)](https://www.codefactor.io/repository/github/msberends/amr) -![[Codecov](https://codecov.io/gh/msberends/AMR/branch/master/graph/badge.svg)](https://codecov.io/gh/msberends/AMR?branch=master) +![R-code-check][https://github.com/msberends/AMR/workflows/R-code-check/badge.svg?branch=master](https://github.com/msberends/AMR/actions) +![CodeFactor][https://www.codefactor.io/repository/github/msberends/amr/badge](https://www.codefactor.io/repository/github/msberends/amr) +![Codecov][https://codecov.io/gh/msberends/AMR/branch/master/graph/badge.svg](https://codecov.io/gh/msberends/AMR?branch=master) The latest and unpublished development version can be installed from GitHub in two ways: diff --git a/inst/tinytest/test-ab_class_selectors.R b/inst/tinytest/test-ab_class_selectors.R index e4167be2..d59216a0 100644 --- a/inst/tinytest/test-ab_class_selectors.R +++ b/inst/tinytest/test-ab_class_selectors.R @@ -23,52 +23,48 @@ # how to conduct AMR data analysis: https://msberends.github.io/AMR/ # # ==================================================================== # -if (getRversion() < "3.2") { - expect_warning(example_isolates[, aminoglycosides(), drop = FALSE]) -} -if (getRversion() >= "3.2") { - # antibiotic class selectors require at least R-3.2 - expect_true(ncol(example_isolates[, ab_class("antimyco"), drop = FALSE]) < ncol(example_isolates)) - expect_true(ncol(example_isolates[, aminoglycosides(), drop = FALSE]) < ncol(example_isolates)) - expect_true(ncol(example_isolates[, aminopenicillins(), drop = FALSE]) < ncol(example_isolates)) - expect_true(ncol(example_isolates[, betalactams(), drop = FALSE]) < ncol(example_isolates)) - expect_true(ncol(example_isolates[, carbapenems(), drop = FALSE]) < ncol(example_isolates)) - expect_true(ncol(example_isolates[, cephalosporins(), drop = FALSE]) < ncol(example_isolates)) - expect_true(ncol(example_isolates[, cephalosporins_1st(), drop = FALSE]) < ncol(example_isolates)) - expect_true(ncol(example_isolates[, cephalosporins_2nd(), drop = FALSE]) < ncol(example_isolates)) - expect_true(ncol(example_isolates[, cephalosporins_3rd(), drop = FALSE]) < ncol(example_isolates)) - expect_true(ncol(example_isolates[, cephalosporins_4th(), drop = FALSE]) < ncol(example_isolates)) - expect_true(ncol(example_isolates[, cephalosporins_5th(), drop = FALSE]) < ncol(example_isolates)) - expect_true(ncol(example_isolates[, fluoroquinolones(), drop = FALSE]) < ncol(example_isolates)) - expect_true(ncol(example_isolates[, glycopeptides(), drop = FALSE]) < ncol(example_isolates)) - expect_true(ncol(example_isolates[, lincosamides(), drop = FALSE]) < ncol(example_isolates)) - expect_true(ncol(example_isolates[, lipoglycopeptides(), drop = FALSE]) < ncol(example_isolates)) - expect_true(ncol(example_isolates[, macrolides(), drop = FALSE]) < ncol(example_isolates)) - expect_true(ncol(example_isolates[, oxazolidinones(), drop = FALSE]) < ncol(example_isolates)) - expect_true(ncol(example_isolates[, penicillins(), drop = FALSE]) < ncol(example_isolates)) - expect_true(ncol(example_isolates[, polymyxins(), drop = FALSE]) < ncol(example_isolates)) - expect_true(ncol(example_isolates[, streptogramins(), drop = FALSE]) < ncol(example_isolates)) - expect_true(ncol(example_isolates[, quinolones(), drop = FALSE]) < ncol(example_isolates)) - expect_true(ncol(example_isolates[, tetracyclines(), drop = FALSE]) < ncol(example_isolates)) - expect_true(ncol(example_isolates[, ureidopenicillins(), drop = FALSE]) < ncol(example_isolates)) - - # Examples: - - # select columns 'mo', 'AMK', 'GEN', 'KAN' and 'TOB' - expect_equal(ncol(example_isolates[, c("mo", aminoglycosides())]), 5, tolerance = 0.5) - - # filter using any() or all() - expect_equal(nrow(example_isolates[any(carbapenems() == "R"), ]), 55, tolerance = 0.5) - expect_equal(nrow(subset(example_isolates, any(carbapenems() == "R"))), 55, tolerance = 0.5) - - # filter on any or all results in the carbapenem columns (i.e., IPM, MEM): - expect_equal(nrow(example_isolates[any(carbapenems()), ]), 962, tolerance = 0.5) - expect_equal(nrow(example_isolates[all(carbapenems()), ]), 756, tolerance = 0.5) - - # filter with multiple antibiotic selectors using c() - expect_equal(nrow(example_isolates[all(c(carbapenems(), aminoglycosides()) == "R"), ]), 26, tolerance = 0.5) - - # filter + select in one go: get penicillins in carbapenems-resistant strains - expect_equal(nrow(example_isolates[any(carbapenems() == "R"), penicillins()]), 55, tolerance = 0.5) - expect_equal(ncol(example_isolates[any(carbapenems() == "R"), penicillins()]), 7, tolerance = 0.5) -} +# antibiotic class selectors +expect_true(ncol(example_isolates[, ab_class("antimyco"), drop = FALSE]) < ncol(example_isolates)) +expect_true(ncol(example_isolates[, aminoglycosides(), drop = FALSE]) < ncol(example_isolates)) +expect_true(ncol(example_isolates[, aminopenicillins(), drop = FALSE]) < ncol(example_isolates)) +expect_true(ncol(example_isolates[, betalactams(), drop = FALSE]) < ncol(example_isolates)) +expect_true(ncol(example_isolates[, carbapenems(), drop = FALSE]) < ncol(example_isolates)) +expect_true(ncol(example_isolates[, cephalosporins(), drop = FALSE]) < ncol(example_isolates)) +expect_true(ncol(example_isolates[, cephalosporins_1st(), drop = FALSE]) < ncol(example_isolates)) +expect_true(ncol(example_isolates[, cephalosporins_2nd(), drop = FALSE]) < ncol(example_isolates)) +expect_true(ncol(example_isolates[, cephalosporins_3rd(), drop = FALSE]) < ncol(example_isolates)) +expect_true(ncol(example_isolates[, cephalosporins_4th(), drop = FALSE]) < ncol(example_isolates)) +expect_true(ncol(example_isolates[, cephalosporins_5th(), drop = FALSE]) < ncol(example_isolates)) +expect_true(ncol(example_isolates[, fluoroquinolones(), drop = FALSE]) < ncol(example_isolates)) +expect_true(ncol(example_isolates[, glycopeptides(), drop = FALSE]) < ncol(example_isolates)) +expect_true(ncol(example_isolates[, lincosamides(), drop = FALSE]) < ncol(example_isolates)) +expect_true(ncol(example_isolates[, lipoglycopeptides(), drop = FALSE]) < ncol(example_isolates)) +expect_true(ncol(example_isolates[, macrolides(), drop = FALSE]) < ncol(example_isolates)) +expect_true(ncol(example_isolates[, oxazolidinones(), drop = FALSE]) < ncol(example_isolates)) +expect_true(ncol(example_isolates[, penicillins(), drop = FALSE]) < ncol(example_isolates)) +expect_true(ncol(example_isolates[, polymyxins(), drop = FALSE]) < ncol(example_isolates)) +expect_true(ncol(example_isolates[, streptogramins(), drop = FALSE]) < ncol(example_isolates)) +expect_true(ncol(example_isolates[, quinolones(), drop = FALSE]) < ncol(example_isolates)) +expect_true(ncol(example_isolates[, tetracyclines(), drop = FALSE]) < ncol(example_isolates)) +expect_true(ncol(example_isolates[, ureidopenicillins(), drop = FALSE]) < ncol(example_isolates)) + +# Examples: + +# select columns 'mo', 'AMK', 'GEN', 'KAN' and 'TOB' +expect_equal(ncol(example_isolates[, c("mo", aminoglycosides())]), 5, tolerance = 0.5) + +# filter using any() or all() +expect_equal(nrow(example_isolates[any(carbapenems() == "R"), ]), 55, tolerance = 0.5) +expect_equal(nrow(subset(example_isolates, any(carbapenems() == "R"))), 55, tolerance = 0.5) + +# filter on any or all results in the carbapenem columns (i.e., IPM, MEM): +expect_equal(nrow(example_isolates[any(carbapenems()), ]), 962, tolerance = 0.5) +expect_equal(nrow(example_isolates[all(carbapenems()), ]), 756, tolerance = 0.5) + +# filter with multiple antibiotic selectors using c() +expect_equal(nrow(example_isolates[all(c(carbapenems(), aminoglycosides()) == "R"), ]), 26, tolerance = 0.5) + +# filter + select in one go: get penicillins in carbapenems-resistant strains +expect_equal(nrow(example_isolates[any(carbapenems() == "R"), penicillins()]), 55, tolerance = 0.5) +expect_equal(ncol(example_isolates[any(carbapenems() == "R"), penicillins()]), 7, tolerance = 0.5) + diff --git a/man/antibiotic_class_selectors.Rd b/man/antibiotic_class_selectors.Rd index 1d5c4408..ca0307b2 100644 --- a/man/antibiotic_class_selectors.Rd +++ b/man/antibiotic_class_selectors.Rd @@ -79,12 +79,10 @@ ureidopenicillins(only_rsi_columns = FALSE) \item{only_rsi_columns}{a \link{logical} to indicate whether only columns of class \verb{} must be selected (defaults to \code{FALSE}), see \code{\link[=as.rsi]{as.rsi()}}} } \description{ -These functions allow for filtering rows and selecting columns based on antibiotic test results that are of a specific antibiotic class, without the need to define the columns or antibiotic abbreviations. \strong{\Sexpr{ifelse(getRversion() < "3.2", paste0("NOTE: THESE FUNCTIONS DO NOT WORK ON YOUR CURRENT R VERSION. These functions require R version 3.2 or later - you have ", R.version.string, "."), "")}} +These functions allow for filtering rows and selecting columns based on antibiotic test results that are of a specific antibiotic class, without the need to define the columns or antibiotic abbreviations. } \details{ -\strong{\Sexpr{ifelse(getRversion() < "3.2", paste0("NOTE: THESE FUNCTIONS DO NOT WORK ON YOUR CURRENT R VERSION. These functions require R version 3.2 or later - you have ", R.version.string, "."), "")}} - -These functions can be used in data set calls for selecting columns and filtering rows, see \emph{Examples}. They support base R, but work more convenient in dplyr functions such as \code{\link[dplyr:select]{select()}}, \code{\link[dplyr:filter]{filter()}} and \code{\link[dplyr:summarise]{summarise()}}. +These functions can be used in data set calls for selecting columns and filtering rows. They are heavily inspired by the \href{https://tidyselect.r-lib.org/reference/language.html}{Tidyverse selection helpers}, but also work in base \R and not only in \code{dplyr} verbs. Nonetheless, they are very convenient to use with \code{dplyr} functions such as \code{\link[dplyr:select]{select()}}, \code{\link[dplyr:filter]{filter()}} and \code{\link[dplyr:summarise]{summarise()}}, see \emph{Examples}. All columns in the data in which these functions are called will be searched for known antibiotic names, abbreviations, brand names, and codes (ATC, EARS-Net, WHO, etc.) in the \link{antibiotics} data set. This means that a selector such as \code{\link[=aminoglycosides]{aminoglycosides()}} will pick up column names like 'gen', 'genta', 'J01GB03', 'tobra', 'Tobracin', etc. Use the \code{\link[=ab_class]{ab_class()}} function to filter/select on a manually defined antibiotic class. } diff --git a/man/first_isolate.Rd b/man/first_isolate.Rd index e494fb65..d37370b1 100755 --- a/man/first_isolate.Rd +++ b/man/first_isolate.Rd @@ -181,11 +181,8 @@ On our website \url{https://msberends.github.io/AMR/} you can find \href{https:/ # `example_isolates` is a data set available in the AMR package. # See ?example_isolates. -example_isolates[first_isolate(example_isolates), ] -\donttest{ -# faster way, only works in R 3.2 and later: example_isolates[first_isolate(), ] - +\donttest{ # get all first Gram-negatives example_isolates[which(first_isolate() & mo_is_gram_negative()), ] diff --git a/man/join.Rd b/man/join.Rd index 25ca528f..7abfe7e3 100755 --- a/man/join.Rd +++ b/man/join.Rd @@ -41,7 +41,7 @@ Join the data set \link{microorganisms} easily to an existing data set or to a \ \details{ \strong{Note:} As opposed to the \code{join()} functions of \code{dplyr}, \link{character} vectors are supported and at default existing columns will get a suffix \code{"2"} and the newly joined columns will not get a suffix. -If the \code{dplyr} package is installed, their join functions will be used. Otherwise, the much slower \code{\link[=merge]{merge()}} and \code{\link[=interaction]{interaction()}} functions from base R will be used. +If the \code{dplyr} package is installed, their join functions will be used. Otherwise, the much slower \code{\link[=merge]{merge()}} and \code{\link[=interaction]{interaction()}} functions from base \R will be used. } \section{Stable Lifecycle}{ diff --git a/man/plot.Rd b/man/plot.Rd index ed164a78..716338a5 100644 --- a/man/plot.Rd +++ b/man/plot.Rd @@ -158,7 +158,7 @@ The \code{ggplot} functions return a \code{\link[ggplot2:ggplot]{ggplot}} model that is extendible with any \code{ggplot2} function. } \description{ -Functions to plot classes \code{rsi}, \code{mic} and \code{disk}, with support for base R and \code{ggplot2}. +Functions to plot classes \code{rsi}, \code{mic} and \code{disk}, with support for base \R and \code{ggplot2}. } \details{ The interpretation of "I" will be named "Increased exposure" for all EUCAST guidelines since 2019, and will be named "Intermediate" in all other cases. diff --git a/man/random.Rd b/man/random.Rd index c35c2b72..0073b47c 100644 --- a/man/random.Rd +++ b/man/random.Rd @@ -31,7 +31,7 @@ class \verb{} for \code{\link[=random_mic]{random_mic()}} (see \code{\link[ These functions can be used for generating random MIC values and disk diffusion diameters, for AMR data analysis practice. By providing a microorganism and antimicrobial agent, the generated results will reflect reality as much as possible. } \details{ -The base R function \code{\link[=sample]{sample()}} is used for generating values. +The base \R function \code{\link[=sample]{sample()}} is used for generating values. Generated values are based on the latest EUCAST guideline implemented in the \link{rsi_translation} data set. To create specific generated values per bug or drug, set the \code{mo} and/or \code{ab} argument. }