2018-09-08 16:06:47 +02:00
|
|
|
# ==================================================================== #
|
|
|
|
# TITLE #
|
2022-10-05 09:12:22 +02:00
|
|
|
# AMR: An R Package for Working with Antimicrobial Resistance Data #
|
2018-09-08 16:06:47 +02:00
|
|
|
# #
|
2019-01-02 23:24:07 +01:00
|
|
|
# SOURCE #
|
2020-07-08 14:48:06 +02:00
|
|
|
# https://github.com/msberends/AMR #
|
2018-09-08 16:06:47 +02:00
|
|
|
# #
|
2022-10-05 09:12:22 +02:00
|
|
|
# CITE AS #
|
|
|
|
# Berends MS, Luz CF, Friedrich AW, Sinha BNM, Albers CJ, Glasner C #
|
|
|
|
# (2022). AMR: An R Package for Working with Antimicrobial Resistance #
|
|
|
|
# Data. Journal of Statistical Software, 104(3), 1-31. #
|
|
|
|
# doi:10.18637/jss.v104.i03 #
|
|
|
|
# #
|
2022-12-27 15:16:15 +01:00
|
|
|
# Developed at the University of Groningen and the University Medical #
|
|
|
|
# Center Groningen in The Netherlands, in collaboration with many #
|
|
|
|
# colleagues from around the world, see our website. #
|
2018-09-08 16:06:47 +02:00
|
|
|
# #
|
2019-01-02 23:24:07 +01:00
|
|
|
# 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. #
|
2020-01-05 17:22:09 +01:00
|
|
|
# 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. #
|
2020-10-08 11:16:03 +02:00
|
|
|
# #
|
|
|
|
# Visit our website for the full manual and a complete tutorial about #
|
2021-02-02 23:57:35 +01:00
|
|
|
# how to conduct AMR data analysis: https://msberends.github.io/AMR/ #
|
2018-09-08 16:06:47 +02:00
|
|
|
# ==================================================================== #
|
|
|
|
|
2020-12-29 21:23:01 +01:00
|
|
|
# set up package environment, used by numerous AMR functions
|
2022-10-05 09:12:22 +02:00
|
|
|
AMR_env <- new.env(hash = FALSE)
|
|
|
|
AMR_env$mo_uncertainties <- data.frame(
|
|
|
|
original_input = character(0),
|
|
|
|
input = character(0),
|
|
|
|
fullname = character(0),
|
|
|
|
mo = character(0),
|
|
|
|
candidates = character(0),
|
|
|
|
minimum_matching_score = integer(0),
|
|
|
|
keep_synonyms = logical(0),
|
|
|
|
stringsAsFactors = FALSE
|
|
|
|
)
|
|
|
|
AMR_env$mo_renamed <- list()
|
|
|
|
AMR_env$mo_previously_coerced <- data.frame(
|
|
|
|
x = character(0),
|
|
|
|
mo = character(0),
|
|
|
|
stringsAsFactors = FALSE
|
2022-08-28 10:31:50 +02:00
|
|
|
)
|
2022-10-10 15:44:59 +02:00
|
|
|
AMR_env$ab_previously_coerced <- data.frame(
|
|
|
|
x = character(0),
|
|
|
|
ab = character(0),
|
|
|
|
stringsAsFactors = FALSE
|
|
|
|
)
|
2022-11-13 08:46:10 +01:00
|
|
|
AMR_env$av_previously_coerced <- data.frame(
|
|
|
|
x = character(0),
|
|
|
|
av = character(0),
|
|
|
|
stringsAsFactors = FALSE
|
|
|
|
)
|
2023-01-21 23:47:20 +01:00
|
|
|
AMR_env$sir_interpretation_history <- data.frame(
|
2022-09-01 15:20:57 +02:00
|
|
|
datetime = Sys.time()[0],
|
|
|
|
index = integer(0),
|
|
|
|
ab_input = character(0),
|
|
|
|
ab_considered = character(0),
|
|
|
|
mo_input = character(0),
|
|
|
|
mo_considered = character(0),
|
|
|
|
guideline = character(0),
|
|
|
|
ref_table = character(0),
|
|
|
|
method = character(0),
|
|
|
|
breakpoint_S = double(0),
|
|
|
|
breakpoint_R = double(0),
|
|
|
|
input = double(0),
|
|
|
|
interpretation = character(0),
|
|
|
|
stringsAsFactors = FALSE
|
|
|
|
)
|
2022-10-11 10:49:55 +02:00
|
|
|
AMR_env$custom_ab_codes <- character(0)
|
2022-12-27 15:16:15 +01:00
|
|
|
AMR_env$custom_mo_codes <- character(0)
|
2022-10-30 21:05:46 +01:00
|
|
|
AMR_env$is_dark_theme <- NULL
|
2020-12-29 21:23:01 +01:00
|
|
|
|
2021-05-12 18:15:03 +02:00
|
|
|
# determine info icon for messages
|
|
|
|
utf8_supported <- isTRUE(base::l10n_info()$`UTF-8`)
|
|
|
|
is_latex <- tryCatch(import_fn("is_latex_output", "knitr", error_on_fail = FALSE)(),
|
2022-08-28 10:31:50 +02:00
|
|
|
error = function(e) FALSE
|
|
|
|
)
|
2021-05-12 18:15:03 +02:00
|
|
|
if (utf8_supported && !is_latex) {
|
|
|
|
# \u2139 is a symbol officially named 'information source'
|
2022-10-05 09:12:22 +02:00
|
|
|
AMR_env$info_icon <- "\u2139"
|
2022-11-24 20:29:00 +01:00
|
|
|
AMR_env$bullet_icon <- "\u2022"
|
2021-05-12 18:15:03 +02:00
|
|
|
} else {
|
2022-10-05 09:12:22 +02:00
|
|
|
AMR_env$info_icon <- "i"
|
2022-11-24 20:29:00 +01:00
|
|
|
AMR_env$bullet_icon <- "*"
|
2021-05-12 18:15:03 +02:00
|
|
|
}
|
|
|
|
|
2022-10-05 09:12:22 +02:00
|
|
|
.onLoad <- function(lib, pkg) {
|
2020-09-29 23:35:46 +02:00
|
|
|
# Support for tibble headers (type_sum) and tibble columns content (pillar_shaft)
|
2022-08-28 10:31:50 +02:00
|
|
|
# without the need to depend on other packages. This was suggested by the
|
|
|
|
# developers of the vctrs package:
|
2020-09-12 08:49:01 +02:00
|
|
|
# https://github.com/r-lib/vctrs/blob/05968ce8e669f73213e3e894b5f4424af4f46316/R/register-s3.R
|
2020-08-26 15:34:12 +02:00
|
|
|
s3_register("pillar::pillar_shaft", "ab")
|
2022-11-13 08:46:10 +01:00
|
|
|
s3_register("pillar::pillar_shaft", "av")
|
2020-08-26 11:33:54 +02:00
|
|
|
s3_register("pillar::pillar_shaft", "mo")
|
2023-01-21 23:47:20 +01:00
|
|
|
s3_register("pillar::pillar_shaft", "sir")
|
|
|
|
s3_register("pillar::pillar_shaft", "rsi") # remove in a later version
|
2020-08-26 11:33:54 +02:00
|
|
|
s3_register("pillar::pillar_shaft", "mic")
|
|
|
|
s3_register("pillar::pillar_shaft", "disk")
|
2022-11-24 20:29:00 +01:00
|
|
|
s3_register("pillar::type_sum", "ab")
|
|
|
|
s3_register("pillar::type_sum", "av")
|
|
|
|
s3_register("pillar::type_sum", "mo")
|
2023-01-21 23:47:20 +01:00
|
|
|
s3_register("pillar::type_sum", "sir")
|
|
|
|
s3_register("pillar::type_sum", "rsi") # remove in a later version
|
2022-11-24 20:29:00 +01:00
|
|
|
s3_register("pillar::type_sum", "mic")
|
|
|
|
s3_register("pillar::type_sum", "disk")
|
2020-09-29 23:35:46 +02:00
|
|
|
# Support for frequency tables from the cleaner package
|
2020-08-28 21:55:47 +02:00
|
|
|
s3_register("cleaner::freq", "mo")
|
2023-01-21 23:47:20 +01:00
|
|
|
s3_register("cleaner::freq", "sir")
|
2021-07-23 21:42:11 +02:00
|
|
|
# Support for skim() from the skimr package
|
2023-02-18 14:56:06 +01:00
|
|
|
if (pkg_is_available("skimr", min_version = "2.0.0")) {
|
2021-12-23 13:38:25 +01:00
|
|
|
s3_register("skimr::get_skimmers", "mo")
|
2023-01-21 23:47:20 +01:00
|
|
|
s3_register("skimr::get_skimmers", "sir")
|
2021-12-23 13:38:25 +01:00
|
|
|
s3_register("skimr::get_skimmers", "mic")
|
|
|
|
s3_register("skimr::get_skimmers", "disk")
|
|
|
|
}
|
2021-07-23 21:42:11 +02:00
|
|
|
# Support for autoplot() from the ggplot2 package
|
2023-01-21 23:47:20 +01:00
|
|
|
s3_register("ggplot2::autoplot", "sir")
|
2021-06-14 22:04:04 +02:00
|
|
|
s3_register("ggplot2::autoplot", "mic")
|
|
|
|
s3_register("ggplot2::autoplot", "disk")
|
|
|
|
s3_register("ggplot2::autoplot", "resistance_predict")
|
2023-02-10 16:18:00 +01:00
|
|
|
s3_register("ggplot2::autoplot", "antibiogram")
|
2021-11-01 13:51:13 +01:00
|
|
|
# Support for fortify from the ggplot2 package
|
2023-01-21 23:47:20 +01:00
|
|
|
s3_register("ggplot2::fortify", "sir")
|
2021-11-01 13:51:13 +01:00
|
|
|
s3_register("ggplot2::fortify", "mic")
|
|
|
|
s3_register("ggplot2::fortify", "disk")
|
2023-02-23 16:27:40 +01:00
|
|
|
# Support for knitr / R Markdown
|
|
|
|
s3_register("knitr::knit_print", "antibiogram")
|
2021-07-23 21:42:11 +02:00
|
|
|
# Support vctrs package for use in e.g. dplyr verbs
|
2022-10-31 11:19:06 +01:00
|
|
|
# S3: ab_selector
|
2021-07-23 21:42:11 +02:00
|
|
|
s3_register("vctrs::vec_ptype2", "character.ab_selector")
|
2022-10-31 11:19:06 +01:00
|
|
|
s3_register("vctrs::vec_ptype2", "ab_selector.character")
|
2021-07-23 21:42:11 +02:00
|
|
|
s3_register("vctrs::vec_cast", "character.ab_selector")
|
2022-10-31 11:19:06 +01:00
|
|
|
# S3: ab_selector_any_all
|
2022-08-21 16:37:20 +02:00
|
|
|
s3_register("vctrs::vec_ptype2", "logical.ab_selector_any_all")
|
2022-10-31 11:19:06 +01:00
|
|
|
s3_register("vctrs::vec_ptype2", "ab_selector_any_all.logical")
|
2022-08-21 16:37:20 +02:00
|
|
|
s3_register("vctrs::vec_cast", "logical.ab_selector_any_all")
|
2022-10-31 11:19:06 +01:00
|
|
|
# S3: ab
|
|
|
|
s3_register("vctrs::vec_ptype2", "character.ab")
|
|
|
|
s3_register("vctrs::vec_ptype2", "ab.character")
|
|
|
|
s3_register("vctrs::vec_cast", "character.ab")
|
|
|
|
s3_register("vctrs::vec_cast", "ab.character")
|
2022-11-13 08:46:10 +01:00
|
|
|
# S3: av
|
|
|
|
s3_register("vctrs::vec_ptype2", "character.av")
|
|
|
|
s3_register("vctrs::vec_ptype2", "av.character")
|
|
|
|
s3_register("vctrs::vec_cast", "character.av")
|
|
|
|
s3_register("vctrs::vec_cast", "av.character")
|
2022-10-31 11:19:06 +01:00
|
|
|
# S3: mo
|
|
|
|
s3_register("vctrs::vec_ptype2", "character.mo")
|
|
|
|
s3_register("vctrs::vec_ptype2", "mo.character")
|
|
|
|
s3_register("vctrs::vec_cast", "character.mo")
|
|
|
|
s3_register("vctrs::vec_cast", "mo.character")
|
|
|
|
# S3: disk
|
2021-07-23 21:42:11 +02:00
|
|
|
s3_register("vctrs::vec_ptype2", "integer.disk")
|
2022-10-31 11:19:06 +01:00
|
|
|
s3_register("vctrs::vec_ptype2", "disk.integer")
|
2021-07-23 21:42:11 +02:00
|
|
|
s3_register("vctrs::vec_cast", "integer.disk")
|
2022-10-31 11:19:06 +01:00
|
|
|
s3_register("vctrs::vec_cast", "disk.integer")
|
|
|
|
s3_register("vctrs::vec_cast", "double.disk")
|
|
|
|
s3_register("vctrs::vec_cast", "disk.double")
|
|
|
|
s3_register("vctrs::vec_cast", "character.disk")
|
|
|
|
s3_register("vctrs::vec_cast", "disk.character")
|
|
|
|
# S3: mic
|
2022-08-30 21:48:02 +02:00
|
|
|
s3_register("vctrs::vec_cast", "character.mic")
|
|
|
|
s3_register("vctrs::vec_cast", "double.mic")
|
2022-10-31 11:19:06 +01:00
|
|
|
s3_register("vctrs::vec_cast", "mic.character")
|
|
|
|
s3_register("vctrs::vec_cast", "mic.double")
|
2022-08-30 21:48:02 +02:00
|
|
|
s3_register("vctrs::vec_math", "mic")
|
2023-01-21 23:47:20 +01:00
|
|
|
# S3: sir
|
|
|
|
s3_register("vctrs::vec_ptype2", "character.sir")
|
|
|
|
s3_register("vctrs::vec_ptype2", "sir.character")
|
|
|
|
s3_register("vctrs::vec_cast", "character.sir")
|
|
|
|
s3_register("vctrs::vec_cast", "sir.character")
|
2022-08-28 10:31:50 +02:00
|
|
|
|
2020-12-24 23:29:10 +01:00
|
|
|
# if mo source exists, fire it up (see mo_source())
|
2022-10-05 09:12:22 +02:00
|
|
|
if (tryCatch(file.exists(getOption("AMR_mo_source", "~/mo_source.rds")), error = function(e) FALSE)) {
|
2022-12-27 15:16:15 +01:00
|
|
|
try(invisible(get_mo_source()), silent = TRUE)
|
2022-10-05 09:12:22 +02:00
|
|
|
}
|
2022-08-27 20:49:37 +02:00
|
|
|
# be sure to print tibbles as tibbles
|
2023-02-18 14:56:06 +01:00
|
|
|
if (pkg_is_available("tibble")) {
|
2022-12-27 15:16:15 +01:00
|
|
|
try(loadNamespace("tibble"), silent = TRUE)
|
2022-08-27 20:49:37 +02:00
|
|
|
}
|
2023-02-09 13:07:39 +01:00
|
|
|
|
2023-01-21 23:47:20 +01:00
|
|
|
# reference data - they have additional to improve algorithm speed
|
2022-08-27 20:49:37 +02:00
|
|
|
# they cannot be part of R/sysdata.rda since CRAN thinks it would make the package too large (+3 MB)
|
2023-01-21 23:47:20 +01:00
|
|
|
AMR_env$AB_lookup <- cbind(AMR::antibiotics, AB_LOOKUP)
|
|
|
|
AMR_env$AV_lookup <- cbind(AMR::antivirals, AV_LOOKUP)
|
2022-12-29 16:23:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
.onAttach <- function(lib, pkg) {
|
2022-12-27 15:16:15 +01:00
|
|
|
# if custom ab option is available, load it
|
|
|
|
if (!is.null(getOption("AMR_custom_ab")) && file.exists(getOption("AMR_custom_ab", default = ""))) {
|
|
|
|
packageStartupMessage("Adding custom antimicrobials from '", getOption("AMR_custom_ab"), "'...", appendLF = FALSE)
|
|
|
|
x <- readRDS2(getOption("AMR_custom_ab"))
|
2023-01-23 15:01:21 +01:00
|
|
|
tryCatch(
|
|
|
|
{
|
|
|
|
suppressWarnings(suppressMessages(add_custom_antimicrobials(x)))
|
|
|
|
packageStartupMessage("OK.")
|
|
|
|
},
|
|
|
|
error = function(e) packageStartupMessage("Failed: ", e$message)
|
|
|
|
)
|
2022-12-27 15:16:15 +01:00
|
|
|
}
|
|
|
|
# if custom mo option is available, load it
|
|
|
|
if (!is.null(getOption("AMR_custom_mo")) && file.exists(getOption("AMR_custom_mo", default = ""))) {
|
|
|
|
packageStartupMessage("Adding custom microorganisms from '", getOption("AMR_custom_mo"), "'...", appendLF = FALSE)
|
|
|
|
x <- readRDS2(getOption("AMR_custom_mo"))
|
2023-01-23 15:01:21 +01:00
|
|
|
tryCatch(
|
|
|
|
{
|
|
|
|
suppressWarnings(suppressMessages(add_custom_microorganisms(x)))
|
|
|
|
packageStartupMessage("OK.")
|
|
|
|
},
|
|
|
|
error = function(e) packageStartupMessage("Failed: ", e$message)
|
|
|
|
)
|
2022-12-27 15:16:15 +01:00
|
|
|
}
|
2021-05-30 22:14:38 +02:00
|
|
|
}
|