From ad68b50ecd3a064e9b2f551fc0f4a4f2fc77e64f Mon Sep 17 00:00:00 2001 From: Matthijs Berends Date: Sat, 15 Oct 2022 15:20:13 +0200 Subject: [PATCH] custom ab fix --- DESCRIPTION | 4 ++-- NEWS.md | 2 +- R/add_custom_antimicrobials.R | 23 +++++++++++++++++++---- man/add_custom_antimicrobials.Rd | 3 +++ 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 1431ae79..825704e8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: AMR -Version: 1.8.2.9016 -Date: 2022-10-14 +Version: 1.8.2.9018 +Date: 2022-10-15 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 diff --git a/NEWS.md b/NEWS.md index 6c454a6a..b09db9ba 100755 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,4 @@ -# AMR 1.8.2.9016 +# AMR 1.8.2.9018 This version will eventually become v2.0! We're happy to reach a new major milestone soon! diff --git a/R/add_custom_antimicrobials.R b/R/add_custom_antimicrobials.R index 76911b19..db9f2145 100644 --- a/R/add_custom_antimicrobials.R +++ b/R/add_custom_antimicrobials.R @@ -50,6 +50,8 @@ #' @rdname add_custom_antimicrobials #' @export #' @examples +#' \donttest{ +#' #' # returns NA and throws a warning (which is now suppressed): #' suppressWarnings( #' as.ab("test") @@ -90,6 +92,7 @@ #' ampicillin = as.rsi("R")) #' x #' x[, betalactams()] +#' } add_custom_antimicrobials <- function(x) { meet_criteria(x, allow_class = "data.frame") stop_ifnot(all(c("ab", "name") %in% colnames(x)), @@ -107,10 +110,22 @@ add_custom_antimicrobials <- function(x) { x$loinc <- as.list(x$loinc) } AMR_env$custom_ab_codes <- c(AMR_env$custom_ab_codes, x$ab) - - bind_rows <- import_fn("bind_rows", "dplyr", error_on_fail = TRUE) - AMR_env$AB_lookup <- unique(bind_rows(AMR_env$AB_lookup, x)) - + class(AMR_env$AB_lookup$ab) <- "character" + + bind_rows <- import_fn("bind_rowtts", "dplyr", error_on_fail = FALSE) + if (is.null(bind_rows)) { + # do the binding in base R + new_df <- AMR_env$AB_lookup[0, , drop = FALSE][seq_len(NROW(x)), , drop = FALSE] + rownames(new_df) <- NULL + for (col in colnames(x)) { + new_df[, col] <- x[, col, drop = TRUE] + } + AMR_env$AB_lookup <- unique(rbind(AMR_env$AB_lookup, new_df)) + } else { + # otherwise use dplyr + AMR_env$AB_lookup <- unique(bind_rows(AMR_env$AB_lookup, x)) + } + class(AMR_env$AB_lookup$ab) <- c("ab", "character") message_("Added ", nr2char(nrow(x)), " record", ifelse(nrow(x) > 1, "s", ""), " to the internal `antibiotics` data set.") } diff --git a/man/add_custom_antimicrobials.Rd b/man/add_custom_antimicrobials.Rd index 98dbf77b..90f17cab 100644 --- a/man/add_custom_antimicrobials.Rd +++ b/man/add_custom_antimicrobials.Rd @@ -33,6 +33,8 @@ add_custom_antimicrobials( Use \code{\link[=clear_custom_antimicrobials]{clear_custom_antimicrobials()}} to clear the previously added antimicrobials. } \examples{ +\donttest{ + # returns NA and throws a warning (which is now suppressed): suppressWarnings( as.ab("test") @@ -74,3 +76,4 @@ x <- data.frame(random_column = "test", x x[, betalactams()] } +}