diff --git a/DESCRIPTION b/DESCRIPTION index f17d4156..8416b237 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,5 +1,5 @@ Package: AMR -Version: 1.1.0.9004 +Version: 1.1.0.9005 Date: 2020-05-16 Title: Antimicrobial Resistance Analysis Authors@R: c( diff --git a/NAMESPACE b/NAMESPACE index b6511af9..45028336 100755 --- a/NAMESPACE +++ b/NAMESPACE @@ -215,7 +215,6 @@ export(proportion_R) export(proportion_S) export(proportion_SI) export(proportion_df) -export(read.4D) export(resistance) export(resistance_predict) export(right_join_microorganisms) diff --git a/NEWS.md b/NEWS.md index f5bc4abd..fce08c36 100755 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,4 @@ -# AMR 1.1.0.9004 +# AMR 1.1.0.9005 ## Last updated: 16-May-2020 ### Breaking @@ -12,7 +12,7 @@ ### Other * Removed dependency on **all** packages that were needed for the `AMR` package to work properly: `crayon`, `data.table`, `dplyr`, `ggplot2`, `R6`, `rlang` and `tidyr`. This is a major code change, but will probably not be noticeable by users. Making this package independent on especially the tidyverse (packages `dplyr`, `ggplot2` and `tidyr`) tremendously increases sustainability on the long term, since tidyverse functions change quite often. Most of our functions are replaced with versions that only rely on base R, which keeps this package fully functional for many years to come, without requiring a lot of maintenance to keep up with other packages anymore. The only dependencies that remained are for extending methods of other packages, like `pillar` and `vctrs` for printing and working with tibbles using our classes `mo` and `ab`. - +* Removed function `read.4d()`, that was only useful for reading from an old test database. # AMR 1.1.0 diff --git a/R/aa_helper_functions.R b/R/aa_helper_functions.R index dc07bfad..1bff1aca 100755 --- a/R/aa_helper_functions.R +++ b/R/aa_helper_functions.R @@ -63,12 +63,16 @@ filter_join_worker <- function(x, y, by = NULL, type = c("anti", "semi")) { # No export, no Rd addin_insert_in <- function() { - rstudioapi::insertText(" %in% ") + if (!require("rstudioapi")) { + insertText(" %in% ") + } } # No export, no Rd addin_insert_like <- function() { - rstudioapi::insertText(" %like% ") + if (!require("rstudioapi")) { + insertText(" %like% ") + } } check_dataset_integrity <- function() { diff --git a/R/atc_online.R b/R/atc_online.R index e421696e..9ef534c7 100644 --- a/R/atc_online.R +++ b/R/atc_online.R @@ -83,8 +83,12 @@ atc_online_property <- function(atc_code, if (!all(atc_code %in% antibiotics)) { atc_code <- as.character(ab_atc(atc_code)) } - - if (!curl::has_internet()) { + + require("curl") + require("xml2") + require("rvest") + + if (!has_internet()) { message("There appears to be no internet connection.") return(rep(NA, length(atc_code))) } @@ -129,15 +133,15 @@ atc_online_property <- function(atc_code, atc_url <- sub("%s", atc_code[i], url, fixed = TRUE) if (property == "groups") { - tbl <- xml2::read_html(atc_url) %>% - rvest::html_node("#content") %>% - rvest::html_children() %>% - rvest::html_node("a") + tbl <- read_html(atc_url) %>% + html_node("#content") %>% + html_children() %>% + html_node("a") # get URLS of items - hrefs <- tbl %>% rvest::html_attr("href") + hrefs <- tbl %>% html_attr("href") # get text of items - texts <- tbl %>% rvest::html_text() + texts <- tbl %>% html_text() # select only text items where URL like "code=" texts <- texts[grepl("?code=", tolower(hrefs), fixed = TRUE)] # last one is antibiotics, skip it @@ -145,9 +149,9 @@ atc_online_property <- function(atc_code, returnvalue <- c(list(texts), returnvalue) } else { - tbl <- xml2::read_html(atc_url) %>% - rvest::html_nodes("table") %>% - rvest::html_table(header = TRUE) %>% + tbl <- read_html(atc_url) %>% + html_nodes("table") %>% + html_table(header = TRUE) %>% as.data.frame(stringsAsFactors = FALSE) # case insensitive column names diff --git a/R/count.R b/R/count.R index a8382546..db56c254 100755 --- a/R/count.R +++ b/R/count.R @@ -21,7 +21,7 @@ #' Count available isolates #' -#' @description These functions can be used to count resistant/susceptible microbial isolates. All functions support quasiquotation with pipes, can be used in [summarise()] and support grouped variables, see *Examples*. +#' @description These functions can be used to count resistant/susceptible microbial isolates. All functions support quasiquotation with pipes, can be used in `summarise()` from the `dplyr` package and also support grouped variables, please see *Examples*. #' #' [count_resistant()] should be used to count resistant isolates, [count_susceptible()] should be used to count susceptible isolates. #' @inheritSection lifecycle Stable lifecycle @@ -32,7 +32,7 @@ #' #' The function [count_resistant()] is equal to the function [count_R()]. The function [count_susceptible()] is equal to the function [count_SI()]. #' -#' The function [n_rsi()] is an alias of [count_all()]. They can be used to count all available isolates, i.e. where all input antibiotics have an available result (S, I or R). Their use is equal to [n_distinct()]. Their function is equal to `count_susceptible(...) + count_resistant(...)`. +#' The function [n_rsi()] is an alias of [count_all()]. They can be used to count all available isolates, i.e. where all input antibiotics have an available result (S, I or R). Their use is equal to `n_distinct()`. Their function is equal to `count_susceptible(...) + count_resistant(...)`. #' #' The function [count_df()] takes any variable from `data` that has an [`rsi`] class (created with [as.rsi()]) and counts the number of S's, I's and R's. It also supports grouped variables. The function [rsi_df()] works exactly like [count_df()], but adds the percentage of S, I and R. #' @inheritSection proportion Combination therapy @@ -68,39 +68,40 @@ #' count_susceptible(example_isolates$AMX) #' susceptibility(example_isolates$AMX) * n_rsi(example_isolates$AMX) #' -#' library(dplyr) -#' example_isolates %>% -#' group_by(hospital_id) %>% -#' summarise(R = count_R(CIP), -#' I = count_I(CIP), -#' S = count_S(CIP), -#' n1 = count_all(CIP), # the actual total; sum of all three -#' n2 = n_rsi(CIP), # same - analogous to n_distinct -#' total = n()) # NOT the number of tested isolates! -#' -#' # Count co-resistance between amoxicillin/clav acid and gentamicin, -#' # so we can see that combination therapy does a lot more than mono therapy. -#' # Please mind that `susceptibility()` calculates percentages right away instead. -#' example_isolates %>% count_susceptible(AMC) # 1433 -#' example_isolates %>% count_all(AMC) # 1879 -#' -#' example_isolates %>% count_susceptible(GEN) # 1399 -#' example_isolates %>% count_all(GEN) # 1855 -#' -#' example_isolates %>% count_susceptible(AMC, GEN) # 1764 -#' example_isolates %>% count_all(AMC, GEN) # 1936 - -#' # Get number of S+I vs. R immediately of selected columns -#' example_isolates %>% -#' select(AMX, CIP) %>% -#' count_df(translate = FALSE) -#' -#' # It also supports grouping variables -#' example_isolates %>% -#' select(hospital_id, AMX, CIP) %>% -#' group_by(hospital_id) %>% -#' count_df(translate = FALSE) -#' +#' +#' if (!require("dplyr")) { +#' example_isolates %>% +#' group_by(hospital_id) %>% +#' summarise(R = count_R(CIP), +#' I = count_I(CIP), +#' S = count_S(CIP), +#' n1 = count_all(CIP), # the actual total; sum of all three +#' n2 = n_rsi(CIP), # same - analogous to n_distinct +#' total = n()) # NOT the number of tested isolates! +#' +#' # Count co-resistance between amoxicillin/clav acid and gentamicin, +#' # so we can see that combination therapy does a lot more than mono therapy. +#' # Please mind that `susceptibility()` calculates percentages right away instead. +#' example_isolates %>% count_susceptible(AMC) # 1433 +#' example_isolates %>% count_all(AMC) # 1879 +#' +#' example_isolates %>% count_susceptible(GEN) # 1399 +#' example_isolates %>% count_all(GEN) # 1855 +#' +#' example_isolates %>% count_susceptible(AMC, GEN) # 1764 +#' example_isolates %>% count_all(AMC, GEN) # 1936 +#' +#' # Get number of S+I vs. R immediately of selected columns +#' example_isolates %>% +#' select(AMX, CIP) %>% +#' count_df(translate = FALSE) +#' +#' # It also supports grouping variables +#' example_isolates %>% +#' select(hospital_id, AMX, CIP) %>% +#' group_by(hospital_id) %>% +#' count_df(translate = FALSE) +#' } count_resistant <- function(..., only_all_tested = FALSE) { rsi_calc(..., ab_result = "R", diff --git a/R/ggplot_pca.R b/R/ggplot_pca.R index f949cde7..6ccc4cbb 100755 --- a/R/ggplot_pca.R +++ b/R/ggplot_pca.R @@ -51,7 +51,7 @@ #' 3. Added total amount of explained variance as a caption in the plot #' 4. Cleaned all syntax based on the `lintr` package and added integrity checks #' 5. Updated documentation -#' @details The colours for labels and points can be changed by adding another scale layer for colour, like [scale_colour_viridis_d()] or [scale_colour_brewer()]. +#' @details The colours for labels and points can be changed by adding another scale layer for colour, like `scale_colour_viridis_d()` or `scale_colour_brewer()`. #' @rdname ggplot_pca #' @export #' @examples diff --git a/R/globals.R b/R/globals.R index a4915079..64e4e9c6 100755 --- a/R/globals.R +++ b/R/globals.R @@ -27,7 +27,10 @@ globalVariables(c(".", "angle", "antibiotic", "antibiotics", + "atc_group1", + "atc_group2", "CNS_CPS", + "code", "col_id", "count", "count.x", @@ -39,6 +42,7 @@ globalVariables(c(".", "fullname_lower", "g_species", "genus", + "gr", "gramstain", "group", "hjust", @@ -63,6 +67,8 @@ globalVariables(c(".", "microorganisms.old", "missing_names", "mo", + "mo_new", + "mo_old", "mono_count", "more_than_episode_ago", "name", diff --git a/R/key_antibiotics.R b/R/key_antibiotics.R index bb7b898d..0632ca2a 100755 --- a/R/key_antibiotics.R +++ b/R/key_antibiotics.R @@ -219,11 +219,7 @@ key_antibiotics <- function(x, x$gramstain <- mo_gramstain(x[, col_mo, drop = TRUE], language = NULL) x$key_ab <- NA_character_ - # mutate_at(vars(col_mo), as.mo) %>% - # left_join_microorganisms(by = col_mo) %>% - # mutate(key_ab = NA_character_, - # gramstain = mo_gramstain(pull(., col_mo), language = NULL)) - # + # Gram + x$key_ab <- if_else(x$gramstain == "Gram-positive", tryCatch(apply(X = x[, gram_positive], diff --git a/R/proportion.R b/R/proportion.R index 31cb22c9..b2d138ba 100755 --- a/R/proportion.R +++ b/R/proportion.R @@ -21,7 +21,7 @@ #' Calculate microbial resistance #' -#' @description These functions can be used to calculate the (co-)resistance or susceptibility of microbial isolates (i.e. percentage of S, SI, I, IR or R). All functions support quasiquotation with pipes, can be used in [summarise()] from the `dplyr` package and also supports grouped variables, please see *Examples*. +#' @description These functions can be used to calculate the (co-)resistance or susceptibility of microbial isolates (i.e. percentage of S, SI, I, IR or R). All functions support quasiquotation with pipes, can be used in `summarise()` from the `dplyr` package and also support grouped variables, please see *Examples*. #' #' [resistance()] should be used to calculate resistance, [susceptibility()] should be used to calculate susceptibility.\cr #' @inheritSection lifecycle Stable lifecycle @@ -99,71 +99,71 @@ #' proportion_IR(example_isolates$AMX) #' proportion_R(example_isolates$AMX) #' -#' \dontrun{ -#' library(dplyr) -#' example_isolates %>% -#' group_by(hospital_id) %>% -#' 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) %>% -#' summarise(R = resistance(CIP, as_percent = TRUE), -#' SI = susceptibility(CIP, as_percent = TRUE), -#' n1 = count_all(CIP), # the actual total; sum of all three -#' n2 = n_rsi(CIP), # same - analogous to n_distinct -#' total = n()) # NOT the number of tested isolates! -#' -#' # Calculate co-resistance between amoxicillin/clav acid and gentamicin, -#' # so we can see that combination therapy does a lot more than mono therapy: -#' example_isolates %>% susceptibility(AMC) # %SI = 76.3% -#' example_isolates %>% count_all(AMC) # n = 1879 -#' -#' example_isolates %>% susceptibility(GEN) # %SI = 75.4% -#' example_isolates %>% count_all(GEN) # n = 1855 -#' -#' example_isolates %>% susceptibility(AMC, GEN) # %SI = 94.1% -#' example_isolates %>% count_all(AMC, GEN) # n = 1939 -#' -#' -#' # See Details on how `only_all_tested` works. Example: -#' example_isolates %>% -#' summarise(numerator = count_susceptible(AMC, GEN), -#' denominator = count_all(AMC, GEN), -#' proportion = susceptibility(AMC, GEN)) - -#' example_isolates %>% -#' summarise(numerator = count_susceptible(AMC, GEN, only_all_tested = TRUE), -#' denominator = count_all(AMC, GEN, only_all_tested = TRUE), -#' proportion = susceptibility(AMC, GEN, only_all_tested = TRUE)) -#' -#' -#' example_isolates %>% -#' group_by(hospital_id) %>% -#' summarise(cipro_p = susceptibility(CIP, as_percent = TRUE), -#' cipro_n = count_all(CIP), -#' genta_p = susceptibility(GEN, as_percent = TRUE), -#' genta_n = count_all(GEN), -#' combination_p = susceptibility(CIP, GEN, as_percent = TRUE), -#' combination_n = count_all(CIP, GEN)) -#' -#' # Get proportions S/I/R immediately of all rsi columns -#' example_isolates %>% -#' select(AMX, CIP) %>% -#' proportion_df(translate = FALSE) -#' -#' # It also supports grouping variables -#' example_isolates %>% -#' select(hospital_id, AMX, CIP) %>% -#' group_by(hospital_id) %>% -#' proportion_df(translate = FALSE) -#' -#' # calculate current empiric combination therapy of Helicobacter gastritis: -#' my_table %>% -#' filter(first_isolate == TRUE, -#' genus == "Helicobacter") %>% -#' summarise(p = susceptibility(AMX, MTR), # amoxicillin with metronidazole -#' n = count_all(AMX, MTR)) +#' if (!require("dplyr")) { +#' library(dplyr) +#' example_isolates %>% +#' group_by(hospital_id) %>% +#' 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) %>% +#' summarise(R = resistance(CIP, as_percent = TRUE), +#' SI = susceptibility(CIP, as_percent = TRUE), +#' n1 = count_all(CIP), # the actual total; sum of all three +#' n2 = n_rsi(CIP), # same - analogous to n_distinct +#' total = n()) # NOT the number of tested isolates! +#' +#' # Calculate co-resistance between amoxicillin/clav acid and gentamicin, +#' # so we can see that combination therapy does a lot more than mono therapy: +#' example_isolates %>% susceptibility(AMC) # %SI = 76.3% +#' example_isolates %>% count_all(AMC) # n = 1879 +#' +#' example_isolates %>% susceptibility(GEN) # %SI = 75.4% +#' example_isolates %>% count_all(GEN) # n = 1855 +#' +#' example_isolates %>% susceptibility(AMC, GEN) # %SI = 94.1% +#' example_isolates %>% count_all(AMC, GEN) # n = 1939 +#' +#' +#' # See Details on how `only_all_tested` works. Example: +#' example_isolates %>% +#' summarise(numerator = count_susceptible(AMC, GEN), +#' denominator = count_all(AMC, GEN), +#' proportion = susceptibility(AMC, GEN)) +#' +#' example_isolates %>% +#' summarise(numerator = count_susceptible(AMC, GEN, only_all_tested = TRUE), +#' denominator = count_all(AMC, GEN, only_all_tested = TRUE), +#' proportion = susceptibility(AMC, GEN, only_all_tested = TRUE)) +#' +#' +#' example_isolates %>% +#' group_by(hospital_id) %>% +#' summarise(cipro_p = susceptibility(CIP, as_percent = TRUE), +#' cipro_n = count_all(CIP), +#' genta_p = susceptibility(GEN, as_percent = TRUE), +#' genta_n = count_all(GEN), +#' combination_p = susceptibility(CIP, GEN, as_percent = TRUE), +#' combination_n = count_all(CIP, GEN)) +#' +#' # Get proportions S/I/R immediately of all rsi columns +#' example_isolates %>% +#' select(AMX, CIP) %>% +#' proportion_df(translate = FALSE) +#' +#' # It also supports grouping variables +#' example_isolates %>% +#' select(hospital_id, AMX, CIP) %>% +#' group_by(hospital_id) %>% +#' proportion_df(translate = FALSE) +#' +#' # calculate current empiric combination therapy of Helicobacter gastritis: +#' my_table %>% +#' filter(first_isolate == TRUE, +#' genus == "Helicobacter") %>% +#' summarise(p = susceptibility(AMX, MTR), # amoxicillin with metronidazole +#' n = count_all(AMX, MTR)) #' } resistance <- function(..., minimum = 30, diff --git a/R/read.4d.R b/R/read.4d.R deleted file mode 100755 index b9f46f7e..00000000 --- a/R/read.4d.R +++ /dev/null @@ -1,176 +0,0 @@ -# ==================================================================== # -# TITLE # -# Antimicrobial Resistance (AMR) Analysis # -# # -# SOURCE # -# https://gitlab.com/msberends/AMR # -# # -# LICENCE # -# (c) 2018-2020 Berends MS, Luz CF et al. # -# # -# This R package is free software; you can freely use and distribute # -# it for both personal and commercial purposes under the terms of the # -# GNU General Public License version 2.0 (GNU GPL-2), as published by # -# the Free Software Foundation. # -# # -# We created this package for both routine data analysis and academic # -# research and it was publicly released in the hope that it will be # -# useful, but it comes WITHOUT ANY WARRANTY OR LIABILITY. # -# Visit our website for more info: https://msberends.gitlab.io/AMR. # -# ==================================================================== # -# -#' Read data from 4D database -#' -#' This function is only useful for the MMB department of the UMCG. Use this function to **import data by just defining the `file` parameter**. It will automatically transform birth dates and calculate patients age, translate the column names to English, transform the MO codes with [as.mo()] and transform all antimicrobial columns with [as.rsi()]. -#' @inheritSection lifecycle Dormant lifecycle -#' @inheritParams utils::read.table -#' @param info a logical to indicate whether info about the import should be printed, defaults to `TRUE` in interactive sessions -#' @details Column names will be transformed, but the original column names are set as a "label" attribute and can be seen in e.g. RStudio Viewer. -#' @inheritSection AMR Read more on our website! -#' @export -read.4D <- function(file, - info = interactive(), - header = TRUE, - row.names = NULL, - sep = "\t", - quote = "\"'", - dec = ",", - na.strings = c("NA", "", "."), - skip = 2, - check.names = TRUE, - strip.white = TRUE, - fill = TRUE, - blank.lines.skip = TRUE, - stringsAsFactors = FALSE, - fileEncoding = "UTF-8", - encoding = "UTF-8") { - - if (info == TRUE) { - message("Importing ", file, "... ", appendLF = FALSE) - } - data_4D <- utils::read.table(file = file, - row.names = row.names, - header = header, - sep = sep, - quote = quote, - dec = dec, - na.strings = na.strings, - skip = skip, - check.names = check.names, - strip.white = strip.white, - fill = fill, - blank.lines.skip = blank.lines.skip, - stringsAsFactors = stringsAsFactors, - fileEncoding = fileEncoding, - encoding = encoding) - - # helper function for dates - to_date_4D <- function(x) { - date_regular <- as.Date(x, format = "%d-%m-%y") - posixlt <- as.POSIXlt(date_regular) - # born after today will be born 100 years ago - # based on https://stackoverflow.com/a/3312971/4575331 - posixlt[date_regular > Sys.Date() & !is.na(posixlt)]$year <- posixlt[date_regular > Sys.Date() & !is.na(posixlt)]$year - 100 - as.Date(posixlt) - } - - if (info == TRUE) { - message("OK\nTransforming column names... ", appendLF = FALSE) - } - if ("row.names" %in% colnames(data_4D) & all(is.na(data_4D[, ncol(data_4D)]))) { - # remove first column name "row.names" and remove last empty column - colnames(data_4D) <- c(colnames(data_4D)[2:ncol(data_4D)], "_skip_last") - data_4D <- data_4D[, -ncol(data_4D)] - } - - colnames(data_4D) <- tolower(colnames(data_4D)) - if (all(c("afnamedat", "gebdatum") %in% colnames(data_4D))) { - # add age column - data_4D$age <- NA_integer_ - } - cols_wanted <- c("patientnr", "gebdatum", "age", "mv", "monsternr", "afnamedat", "bepaling", - "afd.", "spec", "mat", "matbijz.", "mocode", - "amfo", "amox", "anid", "azit", "casp", "cecl", "cefe", "cfcl", - "cfot", "cfox", "cfta", "cftr", "cfur", "chlo", "cipr", "clin", - "cocl", "ctta", "dapt", "doxy", "eryt", "fluo", "fluz", "fosf", - "fusi", "gehi", "gent", "imip", "kana", "levo", "line", "mero", - "metr", "mico", "mino", "moxi", "mupi", "nali", "nitr", "norf", - "oxac", "peni", "pipe", "pita", "poly", "posa", "quda", "rifa", - "spat", "teic", "tige", "tobr", "trim", "trsu", "vana", "vanb", - "vanc", "vori") - # this ones actually exist - cols_wanted <- cols_wanted[cols_wanted %in% colnames(data_4D)] - # order of columns - data_4D <- data_4D[, cols_wanted] - - # backup original column names - colnames.bak <- toupper(colnames(data_4D)) - colnames.bak[colnames.bak == "AGE"] <- NA_character_ - - # rename of columns - colnames(data_4D) <- gsub("patientnr", "patient_id", colnames(data_4D), fixed = TRUE) - colnames(data_4D) <- gsub("gebdatum", "date_birth", colnames(data_4D), fixed = TRUE) - colnames(data_4D) <- gsub("mv", "gender", colnames(data_4D), fixed = TRUE) - colnames(data_4D) <- gsub("monsternr", "sample_id", colnames(data_4D), fixed = TRUE) - colnames(data_4D) <- gsub("afnamedat", "date_received", colnames(data_4D), fixed = TRUE) - colnames(data_4D) <- gsub("bepaling", "sample_test", colnames(data_4D), fixed = TRUE) - colnames(data_4D) <- gsub("afd.", "department", colnames(data_4D), fixed = TRUE) - colnames(data_4D) <- gsub("spec", "specialty", colnames(data_4D), fixed = TRUE) - colnames(data_4D) <- gsub("matbijz.", "specimen_type", colnames(data_4D), fixed = TRUE) - colnames(data_4D) <- gsub("mat", "specimen_group", colnames(data_4D), fixed = TRUE) - colnames(data_4D) <- gsub("mocode", "mo", colnames(data_4D), fixed = TRUE) - - if (info == TRUE) { - message("OK\nTransforming dates and age... ", appendLF = FALSE) - } - if ("date_birth" %in% colnames(data_4D)) { - data_4D$date_birth <- to_date_4D(data_4D$date_birth) - } - if ("date_received" %in% colnames(data_4D)) { - data_4D$date_received <- to_date_4D(data_4D$date_received) - } - if ("age" %in% colnames(data_4D)) { - data_4D$age <- age(data_4D$date_birth, data_4D$date_received) - } - if ("gender" %in% colnames(data_4D)) { - data_4D$gender[data_4D$gender == "V"] <- "F" - } - - if (info == TRUE) { - message("OK\nTransforming MO codes... ", appendLF = FALSE) - } - if ("mo" %in% colnames(data_4D)) { - data_4D$mo <- as.mo(data_4D$mo) - # column right of mo is: - drug1 <- colnames(data_4D)[grep("^mo$", colnames(data_4D)) + 1] - if (!is.na(drug1)) { - # and last is: - drug_last <- colnames(data_4D)[length(data_4D)] - # transform those to rsi: - data_4D <- suppressWarnings(mutate_at(data_4D, vars(drug1:drug_last), as.rsi)) - } - } - - # set original column names as label (can be seen in RStudio Viewer) - if (info == TRUE) { - message("OK\nSetting original column names as label... ", appendLF = FALSE) - } - for (i in seq_len(ncol(data_4D))) { - if (!is.na(colnames.bak[i])) { - attr(data_4D[, i], "label") <- colnames.bak[i] - } - } - - if (info == TRUE) { - message("OK\nSetting query as label to data.frame... ", appendLF = FALSE) - } - qry <- readLines(con <- file(file, open = "r"))[1] - close(con) - attr(data_4D, "label") <- qry - - if (info == TRUE) { - message("OK") - } - - data_4D -} diff --git a/R/resistance_predict.R b/R/resistance_predict.R index 1812102e..6459c3a7 100755 --- a/R/resistance_predict.R +++ b/R/resistance_predict.R @@ -188,34 +188,7 @@ resistance_predict <- function(x, df$year <- as.integer(rownames(df)) rownames(df) <- NULL - # df <- df %>% - # filter_at(col_ab, all_vars(!is.na(.))) %>% - # mutate(year = year(pull(., col_date))) %>% - # group_by_at(c("year", col_ab)) %>% - # summarise(n()) - - # if (df %>% pull(col_ab) %>% n_distinct(na.rm = TRUE) < 2) { - # stop("No variety in antimicrobial interpretations - all isolates are '", - # df %>% pull(col_ab) %>% unique(), "'.", - # call. = FALSE) - # } - # - # colnames(df) <- c("year", "antibiotic", "observations") - df <- subset(df, sum(df$R + df$S, na.rm = TRUE) >= minimum) - - # return(df) - # - # df <- df %>% - # filter(!is.na(antibiotic)) %>% - # pivot_wider(names_from = antibiotic, - # values_from = observations, - # values_fill = list(observations = 0)) %>% - # filter((R + S) >= minimum) - # df_matrix <- df %>% - # ungroup() %>% - # select(R, S) %>% - # as.matrix() df_matrix <- as.matrix(df[, c("R", "S"), drop = FALSE]) if (NROW(df) == 0) { @@ -375,6 +348,9 @@ ggplot_rsi_predict <- function(x, main = paste("Resistance Prediction of", x_name), ribbon = TRUE, ...) { + + stopifnot_installed_package("ggplot2") + if (!"resistance_predict" %in% class(x)) { stop("`x` must be a resistance prediction model created with resistance_predict().") } diff --git a/R/rsi_calc.R b/R/rsi_calc.R index 2347638d..ad6e3ec4 100755 --- a/R/rsi_calc.R +++ b/R/rsi_calc.R @@ -102,7 +102,7 @@ rsi_calc <- function(..., if (only_all_tested == TRUE) { # THE NUMBER OF ISOLATES WHERE *ALL* ABx ARE S/I/R - x <- apply(X = x %>% mutate_all(as.integer), + x <- apply(X = as.data.frame(lapply(x, as.integer), stringsAsFactors = FALSE), MARGIN = 1, FUN = base::min) numerator <- sum(as.integer(x) %in% as.integer(ab_result), na.rm = TRUE) @@ -229,7 +229,9 @@ rsi_calc_df <- function(type, # "proportion", "count" or "both" } else { col_results$value <- rep(NA_real_, NROW(col_results)) } - out_new <- data.frame(antibiotic = ab_property(colnames(.data)[i], property = translate_ab, language = language), + out_new <- data.frame(antibiotic = ifelse(isFALSE(translate_ab), + colnames(.data)[i], + ab_property(colnames(.data)[i], property = translate_ab, language = language)), interpretation = col_results$interpretation, value = col_results$value, isolates = col_results$isolates, diff --git a/docs/404.html b/docs/404.html index 29dfcd1f..0383d297 100644 --- a/docs/404.html +++ b/docs/404.html @@ -81,7 +81,7 @@ AMR (for R) - 1.1.0.9004 + 1.1.0.9005 diff --git a/docs/LICENSE-text.html b/docs/LICENSE-text.html index d0b110e5..1c069be9 100644 --- a/docs/LICENSE-text.html +++ b/docs/LICENSE-text.html @@ -81,7 +81,7 @@ AMR (for R) - 1.1.0.9004 + 1.1.0.9005 diff --git a/docs/articles/index.html b/docs/articles/index.html index f367bf21..ccc199d4 100644 --- a/docs/articles/index.html +++ b/docs/articles/index.html @@ -81,7 +81,7 @@ AMR (for R) - 1.1.0.9004 + 1.1.0.9005 diff --git a/docs/authors.html b/docs/authors.html index ee6d3c46..f8712280 100644 --- a/docs/authors.html +++ b/docs/authors.html @@ -81,7 +81,7 @@ AMR (for R) - 1.1.0.9004 + 1.1.0.9005 diff --git a/docs/index.html b/docs/index.html index 6e830105..10189d5a 100644 --- a/docs/index.html +++ b/docs/index.html @@ -43,7 +43,7 @@ AMR (for R) - 1.1.0.9004 + 1.1.0.9005 diff --git a/docs/pkgdown.yml b/docs/pkgdown.yml index 62d7365a..a75bf77c 100644 --- a/docs/pkgdown.yml +++ b/docs/pkgdown.yml @@ -10,7 +10,7 @@ articles: WHONET: WHONET.html benchmarks: benchmarks.html resistance_predict: resistance_predict.html -last_built: 2020-05-16T11:03Z +last_built: 2020-05-16T18:05Z urls: reference: https://msberends.gitlab.io/AMR/reference article: https://msberends.gitlab.io/AMR/articles diff --git a/docs/reference/count.html b/docs/reference/count.html index ddf12e80..306242bd 100644 --- a/docs/reference/count.html +++ b/docs/reference/count.html @@ -49,7 +49,7 @@ - @@ -83,7 +83,7 @@ count_resistant() should be used to count resistant isolates, count_susceptible( AMR (for R) - 1.1.0.9004 + 1.1.0.9005 @@ -233,7 +233,7 @@ count_resistant() should be used to count resistant isolates, count_susceptible(
-

These functions can be used to count resistant/susceptible microbial isolates. All functions support quasiquotation with pipes, can be used in summarise() and support grouped variables, see Examples.

+

These functions can be used to count resistant/susceptible microbial isolates. All functions support quasiquotation with pipes, can be used in summarise() from the dplyr package and also support grouped variables, please see Examples.

count_resistant() should be used to count resistant isolates, count_susceptible() should be used to count susceptible isolates.

@@ -391,37 +391,40 @@ A microorganism is categorised as Susceptible, Increased exposure when count_susceptible(example_isolates$AMX) susceptibility(example_isolates$AMX) * n_rsi(example_isolates$AMX) -library(dplyr) -example_isolates %>% - group_by(hospital_id) %>% - summarise(R = count_R(CIP), - I = count_I(CIP), - S = count_S(CIP), - n1 = count_all(CIP), # the actual total; sum of all three - n2 = n_rsi(CIP), # same - analogous to n_distinct - total = n()) # NOT the number of tested isolates! -# Count co-resistance between amoxicillin/clav acid and gentamicin, -# so we can see that combination therapy does a lot more than mono therapy. -# Please mind that `susceptibility()` calculates percentages right away instead. -example_isolates %>% count_susceptible(AMC) # 1433 -example_isolates %>% count_all(AMC) # 1879 +if (!require("dplyr")) { + example_isolates %>% + group_by(hospital_id) %>% + summarise(R = count_R(CIP), + I = count_I(CIP), + S = count_S(CIP), + n1 = count_all(CIP), # the actual total; sum of all three + n2 = n_rsi(CIP), # same - analogous to n_distinct + total = n()) # NOT the number of tested isolates! -example_isolates %>% count_susceptible(GEN) # 1399 -example_isolates %>% count_all(GEN) # 1855 + # Count co-resistance between amoxicillin/clav acid and gentamicin, + # so we can see that combination therapy does a lot more than mono therapy. + # Please mind that `susceptibility()` calculates percentages right away instead. + example_isolates %>% count_susceptible(AMC) # 1433 + example_isolates %>% count_all(AMC) # 1879 -example_isolates %>% count_susceptible(AMC, GEN) # 1764 -example_isolates %>% count_all(AMC, GEN) # 1936 -# Get number of S+I vs. R immediately of selected columns -example_isolates %>% - select(AMX, CIP) %>% - count_df(translate = FALSE) + example_isolates %>% count_susceptible(GEN) # 1399 + example_isolates %>% count_all(GEN) # 1855 -# It also supports grouping variables -example_isolates %>% - select(hospital_id, AMX, CIP) %>% - group_by(hospital_id) %>% - count_df(translate = FALSE) + example_isolates %>% count_susceptible(AMC, GEN) # 1764 + example_isolates %>% count_all(AMC, GEN) # 1936 + + # Get number of S+I vs. R immediately of selected columns + example_isolates %>% + select(AMX, CIP) %>% + count_df(translate = FALSE) + + # It also supports grouping variables + example_isolates %>% + select(hospital_id, AMX, CIP) %>% + group_by(hospital_id) %>% + count_df(translate = FALSE) +} diff --git a/docs/reference/index.html b/docs/reference/index.html index 4abe82b9..d170852e 100644 --- a/docs/reference/index.html +++ b/docs/reference/index.html @@ -81,7 +81,7 @@ AMR (for R) - 1.1.0.9004 + 1.1.0.9005 @@ -296,12 +296,6 @@

set_mo_source() get_mo_source()

Use predefined reference data set

- - - -

read.4D()

- -

Read data from 4D database

diff --git a/docs/reference/proportion.html b/docs/reference/proportion.html index d122e804..5df530e4 100644 --- a/docs/reference/proportion.html +++ b/docs/reference/proportion.html @@ -49,7 +49,7 @@ - @@ -83,7 +83,7 @@ resistance() should be used to calculate resistance, susceptibility() should be AMR (for R) - 1.1.0.9004 + 1.1.0.9005 @@ -233,7 +233,7 @@ resistance() should be used to calculate resistance, susceptibility() should be
-

These functions can be used to calculate the (co-)resistance or susceptibility of microbial isolates (i.e. percentage of S, SI, I, IR or R). All functions support quasiquotation with pipes, can be used in summarise() from the dplyr package and also supports grouped variables, please see Examples.

+

These functions can be used to calculate the (co-)resistance or susceptibility of microbial isolates (i.e. percentage of S, SI, I, IR or R). All functions support quasiquotation with pipes, can be used in summarise() from the dplyr package and also support grouped variables, please see Examples.

resistance() should be used to calculate resistance, susceptibility() should be used to calculate susceptibility.

@@ -398,70 +398,71 @@ A microorganism is categorised as Susceptible, Increased exposure when proportion_IR(example_isolates$AMX) proportion_R(example_isolates$AMX) -if (FALSE) { -library(dplyr) -example_isolates %>% - group_by(hospital_id) %>% - summarise(r = resistance(CIP), - n = n_rsi(CIP)) # n_rsi works like n_distinct in dplyr, see ?n_rsi +if (!require("dplyr")) { + library(dplyr) + example_isolates %>% + group_by(hospital_id) %>% + 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) %>% - summarise(R = resistance(CIP, as_percent = TRUE), - SI = susceptibility(CIP, as_percent = TRUE), - n1 = count_all(CIP), # the actual total; sum of all three - n2 = n_rsi(CIP), # same - analogous to n_distinct - total = n()) # NOT the number of tested isolates! + example_isolates %>% + group_by(hospital_id) %>% + summarise(R = resistance(CIP, as_percent = TRUE), + SI = susceptibility(CIP, as_percent = TRUE), + n1 = count_all(CIP), # the actual total; sum of all three + n2 = n_rsi(CIP), # same - analogous to n_distinct + total = n()) # NOT the number of tested isolates! -# Calculate co-resistance between amoxicillin/clav acid and gentamicin, -# so we can see that combination therapy does a lot more than mono therapy: -example_isolates %>% susceptibility(AMC) # %SI = 76.3% -example_isolates %>% count_all(AMC) # n = 1879 + # Calculate co-resistance between amoxicillin/clav acid and gentamicin, + # so we can see that combination therapy does a lot more than mono therapy: + example_isolates %>% susceptibility(AMC) # %SI = 76.3% + example_isolates %>% count_all(AMC) # n = 1879 -example_isolates %>% susceptibility(GEN) # %SI = 75.4% -example_isolates %>% count_all(GEN) # n = 1855 + example_isolates %>% susceptibility(GEN) # %SI = 75.4% + example_isolates %>% count_all(GEN) # n = 1855 -example_isolates %>% susceptibility(AMC, GEN) # %SI = 94.1% -example_isolates %>% count_all(AMC, GEN) # n = 1939 + example_isolates %>% susceptibility(AMC, GEN) # %SI = 94.1% + example_isolates %>% count_all(AMC, GEN) # n = 1939 -# See Details on how `only_all_tested` works. Example: -example_isolates %>% - summarise(numerator = count_susceptible(AMC, GEN), - denominator = count_all(AMC, GEN), - proportion = susceptibility(AMC, GEN)) -example_isolates %>% - summarise(numerator = count_susceptible(AMC, GEN, only_all_tested = TRUE), - denominator = count_all(AMC, GEN, only_all_tested = TRUE), - proportion = susceptibility(AMC, GEN, only_all_tested = TRUE)) + # See Details on how `only_all_tested` works. Example: + example_isolates %>% + summarise(numerator = count_susceptible(AMC, GEN), + denominator = count_all(AMC, GEN), + proportion = susceptibility(AMC, GEN)) + + example_isolates %>% + summarise(numerator = count_susceptible(AMC, GEN, only_all_tested = TRUE), + denominator = count_all(AMC, GEN, only_all_tested = TRUE), + proportion = susceptibility(AMC, GEN, only_all_tested = TRUE)) -example_isolates %>% - group_by(hospital_id) %>% - summarise(cipro_p = susceptibility(CIP, as_percent = TRUE), - cipro_n = count_all(CIP), - genta_p = susceptibility(GEN, as_percent = TRUE), - genta_n = count_all(GEN), - combination_p = susceptibility(CIP, GEN, as_percent = TRUE), - combination_n = count_all(CIP, GEN)) + example_isolates %>% + group_by(hospital_id) %>% + summarise(cipro_p = susceptibility(CIP, as_percent = TRUE), + cipro_n = count_all(CIP), + genta_p = susceptibility(GEN, as_percent = TRUE), + genta_n = count_all(GEN), + combination_p = susceptibility(CIP, GEN, as_percent = TRUE), + combination_n = count_all(CIP, GEN)) -# Get proportions S/I/R immediately of all rsi columns -example_isolates %>% - select(AMX, CIP) %>% - proportion_df(translate = FALSE) + # Get proportions S/I/R immediately of all rsi columns + example_isolates %>% + select(AMX, CIP) %>% + proportion_df(translate = FALSE) -# It also supports grouping variables -example_isolates %>% - select(hospital_id, AMX, CIP) %>% - group_by(hospital_id) %>% - proportion_df(translate = FALSE) + # It also supports grouping variables + example_isolates %>% + select(hospital_id, AMX, CIP) %>% + group_by(hospital_id) %>% + proportion_df(translate = FALSE) -# calculate current empiric combination therapy of Helicobacter gastritis: -my_table %>% - filter(first_isolate == TRUE, - genus == "Helicobacter") %>% - summarise(p = susceptibility(AMX, MTR), # amoxicillin with metronidazole - n = count_all(AMX, MTR)) + # calculate current empiric combination therapy of Helicobacter gastritis: + my_table %>% + filter(first_isolate == TRUE, + genus == "Helicobacter") %>% + summarise(p = susceptibility(AMX, MTR), # amoxicillin with metronidazole + n = count_all(AMX, MTR)) }