1
0
mirror of https://github.com/msberends/AMR.git synced 2024-12-26 20:06:11 +01:00
AMR/R/aa_helper_functions.R

1746 lines
59 KiB
R
Raw Normal View History

2018-02-21 11:52:31 +01:00
# ==================================================================== #
# TITLE: #
2022-10-05 09:12:22 +02:00
# AMR: An R Package for Working with Antimicrobial Resistance Data #
2018-02-21 11:52:31 +01:00
# #
# SOURCE CODE: #
2020-07-08 14:48:06 +02:00
# https://github.com/msberends/AMR #
2018-02-21 11:52:31 +01:00
# #
# PLEASE CITE THIS SOFTWARE AS: #
# Berends MS, Luz CF, Friedrich AW, et al. (2022). #
# AMR: An R Package for Working with Antimicrobial Resistance Data. #
# Journal of Statistical Software, 104(3), 1-31. #
2023-05-27 10:39:22 +02:00
# https://doi.org/10.18637/jss.v104.i03 #
2022-10-05 09:12:22 +02:00
# #
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-02-21 11:52:31 +01: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. #
# 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 #
# how to conduct AMR data analysis: https://msberends.github.io/AMR/ #
2018-02-21 11:52:31 +01:00
# ==================================================================== #
# faster implementation of left_join than using merge() by poorman - we use match():
pm_left_join <- function(x, y, by = NULL, suffix = c(".x", ".y")) {
if (is.null(by)) {
by <- intersect(names(x), names(y))[1L]
if (is.na(by)) {
stop_("no common column found for pm_left_join()")
}
2020-09-19 11:54:01 +02:00
pm_join_message(by)
} else if (!is.null(names(by))) {
by <- unname(c(names(by), by))
}
if (length(by) == 1) {
by <- rep(by, 2)
}
2020-10-26 12:23:03 +01:00
int_x <- colnames(x) %in% colnames(y) & colnames(x) != by[1]
int_y <- colnames(y) %in% colnames(x) & colnames(y) != by[2]
colnames(x)[int_x] <- paste0(colnames(x)[int_x], suffix[1L])
colnames(y)[int_y] <- paste0(colnames(y)[int_y], suffix[2L])
2020-10-26 12:23:03 +01:00
2022-08-28 10:31:50 +02:00
merged <- cbind(
x,
2023-01-23 15:01:21 +01:00
y[
match(
x[, by[1], drop = TRUE],
y[, by[2], drop = TRUE]
),
colnames(y)[!colnames(y) %in% colnames(x) & !colnames(y) == by[2]],
drop = FALSE
2022-08-28 10:31:50 +02:00
]
)
2020-10-26 12:23:03 +01:00
rownames(merged) <- NULL
merged
}
2023-03-12 13:02:37 +01:00
# support where() like tidyverse (this function will also be used when running `antibiogram()`):
2023-02-09 13:07:39 +01:00
where <- function(fn) {
2023-03-12 13:02:37 +01:00
# based on https://github.com/nathaneastwood/poorman/blob/52eb6947e0b4430cd588976ed8820013eddf955f/R/where.R#L17-L32
2023-02-09 13:07:39 +01:00
if (!is.function(fn)) {
2023-02-10 17:09:48 +01:00
stop_("`", deparse(substitute(fn)), "()` is not a valid predicate function.")
}
df <- pm_select_env$.data
cols <- pm_select_env$get_colnames()
if (is.null(df)) {
df <- get_current_data("where", call = FALSE)
cols <- colnames(df)
2023-02-09 13:07:39 +01:00
}
preds <- unlist(lapply(
2023-02-10 17:09:48 +01:00
df,
2023-02-09 13:07:39 +01:00
function(x, fn) {
do.call("fn", list(x))
},
fn
))
2023-02-10 17:09:48 +01:00
if (!is.logical(preds)) stop_("`where()` must be used with functions that return `TRUE` or `FALSE`.")
data_cols <- cols
2023-02-09 13:07:39 +01:00
cols <- data_cols[preds]
which(data_cols %in% cols)
}
2023-02-11 22:28:48 +01:00
# copied and slightly rewritten from {poorman} under permissive license (2021-10-15)
# https://github.com/nathaneastwood/poorman, MIT licensed, Nathan Eastwood, 2020
2023-03-12 13:02:37 +01:00
case_when_AMR <- function(...) {
2023-02-09 13:07:39 +01:00
fs <- list(...)
lapply(fs, function(x) {
if (!inherits(x, "formula")) {
stop("`case_when()` requires formula inputs.")
}
})
n <- length(fs)
if (n == 0L) {
stop("No cases provided.")
}
validate_case_when_length <- function(query, value, fs) {
lhs_lengths <- lengths(query)
rhs_lengths <- lengths(value)
all_lengths <- unique(c(lhs_lengths, rhs_lengths))
if (length(all_lengths) <= 1L) {
return(all_lengths[[1L]])
}
non_atomic_lengths <- all_lengths[all_lengths != 1L]
len <- non_atomic_lengths[[1L]]
if (length(non_atomic_lengths) == 1L) {
return(len)
}
inconsistent_lengths <- non_atomic_lengths[-1L]
lhs_problems <- lhs_lengths %in% inconsistent_lengths
rhs_problems <- rhs_lengths %in% inconsistent_lengths
problems <- lhs_problems | rhs_problems
if (any(problems)) {
stop("The following formulas must be length ", len, " or 1, not ",
paste(inconsistent_lengths, collapse = ", "), ".\n ",
paste(fs[problems], collapse = "\n "),
call. = FALSE
)
}
}
replace_with <- function(x, i, val, arg_name) {
if (is.null(val)) {
return(x)
}
i[is.na(i)] <- FALSE
if (length(val) == 1L) {
x[i] <- val
} else {
x[i] <- val[i]
}
x
}
query <- vector("list", n)
value <- vector("list", n)
default_env <- parent.frame()
for (i in seq_len(n)) {
query[[i]] <- eval(fs[[i]][[2]], envir = default_env)
value[[i]] <- eval(fs[[i]][[3]], envir = default_env)
if (!is.logical(query[[i]])) {
stop(fs[[i]][[2]], " does not return a `logical` vector.")
}
}
m <- validate_case_when_length(query, value, fs)
out <- value[[1]][rep(NA_integer_, m)]
replaced <- rep(FALSE, m)
for (i in seq_len(n)) {
out <- replace_with(
out, query[[i]] & !replaced, value[[i]],
NULL
)
replaced <- replaced | (query[[i]] & !is.na(query[[i]]))
}
out
}
2023-03-12 13:02:37 +01:00
rbind_AMR <- function(...) {
# this is just rbind(), but with the functionality of dplyr::bind_rows(),
2023-02-12 11:20:14 +01:00
# to allow differences in available columns
l <- list(...)
l_names <- unique(unlist(lapply(l, names)))
l_new <- lapply(l, function(df) {
rownames(df) <- NULL
for (col in l_names[!l_names %in% colnames(df)]) {
2023-02-11 22:28:48 +01:00
# create the new column, could also be length 0
2023-02-12 11:20:14 +01:00
df[, col] <- rep(NA, NROW(df))
2023-02-11 22:28:48 +01:00
}
2023-02-12 11:20:14 +01:00
df
2023-02-10 17:09:48 +01:00
})
do.call(rbind, l_new)
2023-02-10 17:09:48 +01:00
}
# No export, no Rd
addin_insert_in <- function() {
2020-06-17 15:14:37 +02:00
import_fn("insertText", "rstudioapi")(" %in% ")
}
# No export, no Rd
addin_insert_like <- function() {
# we want Shift + Ctrl/Cmd + L to iterate over %like%, %unlike%, %like_case%, and %unlike_case%
2022-08-28 10:31:50 +02:00
getActiveDocumentContext <- import_fn("getActiveDocumentContext", "rstudioapi")
insertText <- import_fn("insertText", "rstudioapi")
modifyRange <- import_fn("modifyRange", "rstudioapi")
document_range <- import_fn("document_range", "rstudioapi")
document_position <- import_fn("document_position", "rstudioapi")
2022-08-28 10:31:50 +02:00
context <- getActiveDocumentContext()
current_row <- context$selection[[1]]$range$end[1]
current_col <- context$selection[[1]]$range$end[2]
current_row_txt <- context$contents[current_row]
if (is.null(current_row) || current_row_txt %unlike% "%(un)?like") {
insertText(" %like% ")
return(invisible())
}
2022-08-28 10:31:50 +02:00
pos_preceded_by <- function(txt) {
if (tryCatch(substr(current_row_txt, current_col - nchar(trimws(txt, which = "right")), current_col) == trimws(txt, which = "right"),
2022-08-28 10:31:50 +02:00
error = function(e) FALSE
)) {
return(TRUE)
}
tryCatch(substr(current_row_txt, current_col - nchar(txt), current_col) %like% paste0("^", txt),
2022-08-28 10:31:50 +02:00
error = function(e) FALSE
)
}
replace_pos <- function(old, with) {
2023-01-23 15:01:21 +01:00
modifyRange(
document_range(
document_position(current_row, current_col - nchar(old)),
document_position(current_row, current_col)
),
text = with,
id = context$id
2022-08-28 10:31:50 +02:00
)
}
2022-08-28 10:31:50 +02:00
if (pos_preceded_by(" %like% ")) {
replace_pos(" %like% ", with = " %unlike% ")
} else if (pos_preceded_by(" %unlike% ")) {
replace_pos(" %unlike% ", with = " %like_case% ")
} else if (pos_preceded_by(" %like_case% ")) {
replace_pos(" %like_case% ", with = " %unlike_case% ")
} else if (pos_preceded_by(" %unlike_case% ")) {
replace_pos(" %unlike_case% ", with = " %like% ")
} else {
insertText(" %like% ")
}
}
search_type_in_df <- function(x, type, info = TRUE, add_col_prefix = TRUE) {
2020-11-17 16:57:41 +01:00
meet_criteria(x, allow_class = "data.frame")
meet_criteria(type, allow_class = "character", has_length = 1)
2022-08-28 10:31:50 +02:00
2019-01-15 12:45:24 +01:00
# try to find columns based on type
found <- NULL
2020-10-26 12:23:03 +01:00
2020-11-17 16:57:41 +01:00
# remove attributes from other packages
2020-05-16 13:05:47 +02:00
x <- as.data.frame(x, stringsAsFactors = FALSE)
colnames_formatted <- tolower(generalise_antibiotic_name(colnames(x)))
2020-10-26 12:23:03 +01:00
2019-01-15 12:45:24 +01:00
# -- mo
if (type == "mo") {
2023-01-21 23:47:20 +01:00
add_MO_lookup_to_AMR_env()
2023-01-23 15:01:21 +01:00
if (any(vapply(FUN.VALUE = logical(1), x, is.mo))) {
2022-10-19 11:47:57 +02:00
# take first 'mo' column
found <- colnames(x)[vapply(FUN.VALUE = logical(1), x, is.mo)]
2022-10-05 09:12:22 +02:00
} else if ("mo" %in% colnames_formatted &&
2023-01-06 19:21:04 +01:00
suppressWarnings(all(x$mo %in% c(NA, AMR_env$MO_lookup$mo)))) {
2019-11-04 12:08:08 +01:00
found <- "mo"
} 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"])
2019-01-15 12:45:24 +01:00
}
}
# -- key antibiotics
2021-05-24 15:29:17 +02:00
if (type %in% c("keyantibiotics", "keyantimicrobials")) {
if (any(colnames_formatted %like_case% "^key.*(ab|antibiotics|antimicrobials)")) {
found <- sort(colnames(x)[colnames_formatted %like_case% "^key.*(ab|antibiotics|antimicrobials)"])
2019-01-15 12:45:24 +01:00
}
}
# -- date
if (type == "date") {
if (any(colnames_formatted %like_case% "^(specimen date|specimen_date|spec_date)")) {
2019-03-15 13:57:25 +01:00
# WHONET support
found <- sort(colnames(x)[colnames_formatted %like_case% "^(specimen date|specimen_date|spec_date)"])
2022-10-05 09:12:22 +02:00
if (!inherits(pm_pull(x, found), c("Date", "POSIXct"))) {
2023-01-23 15:01:21 +01:00
stop(
font_red(paste0(
"Found column '", font_bold(found), "' to be used as input for `", ifelse(add_col_prefix, "col_", ""), type,
2023-01-23 15:01:21 +01:00
"`, but this column contains no valid dates. Transform its values to valid dates first."
)),
call. = FALSE
2022-08-28 10:31:50 +02:00
)
2019-03-15 13:57:25 +01:00
}
} else if (any(vapply(FUN.VALUE = logical(1), x, function(x) inherits(x, c("Date", "POSIXct"))))) {
# take first <Date> column
found <- colnames(x)[vapply(FUN.VALUE = logical(1), x, function(x) inherits(x, c("Date", "POSIXct")))]
2019-01-15 12:45:24 +01:00
}
}
# -- patient id
if (type == "patient_id") {
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]
}
2019-01-29 00:06:50 +01:00
}
}
# -- specimen
if (type == "specimen") {
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)"])
2019-01-15 12:45:24 +01:00
}
}
# -- host (animals)
if (type == "host") {
if (any(colnames_formatted %like_case% "^(host|animal)")) {
found <- sort(colnames(x)[colnames_formatted %like_case% "^(host|animal)"])
} else if (any(colnames_formatted %like_case% "((^|[^A-Za-z])host($|[^A-Za-z])|animal)")) {
found <- sort(colnames(x)[colnames_formatted %like_case% "((^|[^A-Za-z])host($|[^A-Za-z])|animal)"])
}
}
2020-02-20 13:19:23 +01:00
# -- UTI (urinary tract infection)
if (type == "uti") {
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)"])
2020-02-20 13:19:23 +01:00
}
if (!is.null(found)) {
# this column should contain logicals
2020-02-20 13:19:23 +01:00
if (!is.logical(x[, found, drop = TRUE])) {
message_("Column '", font_bold(found), "' found as input for `", ifelse(add_col_prefix, "col_", ""), type,
2022-08-28 10:31:50 +02:00
"`, but this column does not contain 'logical' values (TRUE/FALSE) and was ignored.",
add_fn = font_red
)
2020-02-20 13:19:23 +01:00
found <- NULL
}
}
}
2022-08-28 10:31:50 +02:00
found <- found[1]
2022-08-28 10:31:50 +02:00
2022-11-14 15:20:39 +01:00
if (!is.null(found) && isTRUE(info)) {
if (message_not_thrown_before("search_in_type", type)) {
msg <- paste0("Using column '", font_bold(found), "' as input for `", ifelse(add_col_prefix, "col_", ""), type, "`.")
if (type %in% c("keyantibiotics", "keyantimicrobials", "specimen")) {
msg <- paste(msg, "Use", font_bold(paste0(ifelse(add_col_prefix, "col_", ""), type), "= FALSE"), "to prevent this.")
}
message_(msg)
2019-01-15 12:45:24 +01:00
}
}
found
}
2019-03-26 14:24:03 +01:00
2021-04-16 11:41:05 +02:00
is_valid_regex <- function(x) {
2023-01-23 15:01:21 +01:00
regex_at_all <- tryCatch(
vapply(
FUN.VALUE = logical(1),
X = strsplit(x, "", fixed = TRUE),
FUN = function(y) {
any(
y %in% c(
"$", "(", ")", "*", "+", "-",
".", "?", "[", "]", "^", "{",
"|", "}", "\\"
),
na.rm = TRUE
)
},
USE.NAMES = FALSE
),
error = function(e) rep(TRUE, length(x))
2022-08-28 10:31:50 +02:00
)
regex_valid <- vapply(
FUN.VALUE = logical(1),
X = x,
FUN = function(y) {
2022-10-05 09:12:22 +02:00
!inherits(try(grepl(y, "", perl = TRUE), silent = TRUE), "try-error")
2022-08-28 10:31:50 +02:00
},
USE.NAMES = FALSE
)
2021-04-16 11:41:05 +02:00
regex_at_all & regex_valid
2020-09-24 00:30:11 +02:00
}
stop_ifnot_installed <- function(package) {
2023-02-09 13:07:39 +01:00
installed <- vapply(FUN.VALUE = logical(1), package, requireNamespace, quietly = TRUE)
2022-10-14 15:45:20 +02:00
if (any(!installed) && any(package == "rstudioapi")) {
stop("This function only works in RStudio when using R >= 3.2.", call. = FALSE)
} else if (any(!installed)) {
stop("This requires the ", vector_and(package[!installed]), " package.",
2022-10-30 14:31:45 +01:00
"\nTry to install with install.packages().",
call. = FALSE
)
2022-10-14 15:45:20 +02:00
} else {
return(invisible())
}
2019-03-26 14:24:03 +01:00
}
2019-05-10 16:44:59 +02:00
2023-02-18 14:56:06 +01:00
pkg_is_available <- function(pkg, also_load = FALSE, min_version = NULL) {
2021-05-21 20:20:51 +02:00
if (also_load == TRUE) {
out <- suppressWarnings(require(pkg, character.only = TRUE, warn.conflicts = FALSE))
2021-05-21 20:20:51 +02:00
} else {
2023-02-09 13:07:39 +01:00
out <- requireNamespace(pkg, quietly = TRUE)
2021-05-21 20:20:51 +02:00
}
2021-10-05 09:58:08 +02:00
if (!is.null(min_version)) {
2021-10-06 16:22:36 +02:00
out <- out && utils::packageVersion(pkg) >= min_version
2021-10-05 09:58:08 +02:00
}
2021-05-21 20:20:51 +02:00
isTRUE(out)
}
import_fn <- function(name, pkg, error_on_fail = TRUE) {
if (isTRUE(error_on_fail)) {
stop_ifnot_installed(pkg)
}
2020-07-08 14:48:06 +02:00
tryCatch(
2022-08-28 10:31:50 +02:00
# don't use get() to avoid fetching non-API functions
2020-12-17 16:22:25 +01:00
getExportedValue(name = name, ns = asNamespace(pkg)),
error = function(e) {
if (isTRUE(error_on_fail)) {
stop_("function `", name, "()` is not an exported object from package '", pkg,
2022-12-30 12:57:27 +01:00
"'. Please create an issue at ", font_url("https://github.com/msberends/AMR/issues"), ". Many thanks!",
2022-08-28 10:31:50 +02:00
call = FALSE
)
} else {
return(NULL)
}
2022-08-28 10:31:50 +02:00
}
)
2020-06-17 15:14:37 +02:00
}
2020-11-10 16:35:56 +01:00
# this alternative wrapper to the message(), warning() and stop() functions:
2020-10-26 12:23:03 +01:00
# - wraps text to never break lines within words
# - ignores formatted text while wrapping
2020-12-17 16:22:25 +01:00
# - adds indentation dependent on the type of message (such as NOTE)
2020-11-10 16:35:56 +01:00
# - can add additional formatting functions like blue or bold text
word_wrap <- function(...,
2022-08-28 10:31:50 +02:00
add_fn = list(),
2020-11-10 16:35:56 +01:00
as_note = FALSE,
width = 0.95 * getOption("width"),
extra_indent = 0) {
2020-10-26 12:23:03 +01:00
msg <- paste0(c(...), collapse = "")
2022-08-28 10:31:50 +02:00
if (isTRUE(as_note)) {
2022-10-05 09:12:22 +02:00
msg <- paste0(AMR_env$info_icon, " ", gsub("^note:? ?", "", msg, ignore.case = TRUE))
}
2022-08-28 10:31:50 +02:00
2020-12-22 00:51:17 +01:00
if (msg %like% "\n") {
# run word_wraps() over every line here, bind them and return again
2023-01-23 15:01:21 +01:00
return(paste0(
vapply(
FUN.VALUE = character(1),
trimws(unlist(strsplit(msg, "\n", fixed = TRUE)), which = "right"),
word_wrap,
add_fn = add_fn,
as_note = FALSE,
width = width,
extra_indent = extra_indent
),
collapse = "\n"
2022-08-28 10:31:50 +02:00
))
}
2021-05-19 22:55:42 +02:00
# correct for operators (will add the space later on)
ops <- "([,./><\\]\\[])"
msg <- gsub(paste0(ops, " ", ops), "\\1\\2", msg, perl = TRUE)
2020-10-26 12:23:03 +01:00
# we need to correct for already applied style, that adds text like "\033[31m\"
msg_stripped <- gsub("(.*)?\\033\\]8;;.*\\a(.*?)\\033\\]8;;\\a(.*)", "\\1\\2\\3", msg, perl = TRUE) # for font_url()
msg_stripped <- font_stripstyle(msg_stripped)
2020-10-26 12:23:03 +01:00
# where are the spaces now?
2023-01-23 15:01:21 +01:00
msg_stripped_wrapped <- paste0(
strwrap(msg_stripped,
simplify = TRUE,
width = width
),
collapse = "\n"
2022-08-28 10:31:50 +02:00
)
2020-11-10 16:35:56 +01:00
msg_stripped_wrapped <- paste0(unlist(strsplit(msg_stripped_wrapped, "(\n|\\*\\|\\*)")),
2022-08-28 10:31:50 +02:00
collapse = "\n"
)
2022-10-05 09:12:22 +02:00
msg_stripped_spaces <- which(unlist(strsplit(msg_stripped, "", fixed = TRUE)) == " ")
msg_stripped_wrapped_spaces <- which(unlist(strsplit(msg_stripped_wrapped, "", fixed = TRUE)) != "\n")
2020-10-26 12:23:03 +01:00
# so these are the indices of spaces that need to be replaced
replace_spaces <- which(!msg_stripped_spaces %in% msg_stripped_wrapped_spaces)
# put it together
2022-10-05 09:12:22 +02:00
msg <- unlist(strsplit(msg, " ", fixed = TRUE))
2020-10-26 12:23:03 +01:00
msg[replace_spaces] <- paste0(msg[replace_spaces], "\n")
2021-05-19 22:55:42 +02:00
# add space around operators again
msg <- gsub(paste0(ops, ops), "\\1 \\2", msg, perl = TRUE)
2020-10-26 12:23:03 +01:00
msg <- paste0(msg, collapse = " ")
msg <- gsub("\n ", "\n", msg, fixed = TRUE)
2022-08-28 10:31:50 +02:00
if (msg_stripped %like% "\u2139 ") {
indentation <- 2 + extra_indent
2020-10-27 15:56:51 +01:00
} else if (msg_stripped %like% "^=> ") {
2020-11-10 16:35:56 +01:00
indentation <- 3 + extra_indent
2020-10-26 12:23:03 +01:00
} else {
2020-11-10 16:35:56 +01:00
indentation <- 0 + extra_indent
2020-10-26 12:23:03 +01:00
}
msg <- gsub("\n", paste0("\n", strrep(" ", indentation)), msg, fixed = TRUE)
# remove trailing empty characters
msg <- gsub("(\n| )+$", "", msg)
2021-05-19 22:55:42 +02:00
2020-10-26 12:23:03 +01:00
if (length(add_fn) > 0) {
if (!is.list(add_fn)) {
add_fn <- list(add_fn)
}
for (i in seq_len(length(add_fn))) {
msg <- add_fn[[i]](msg)
}
}
2022-08-28 10:31:50 +02:00
# format backticks
2023-07-11 09:50:45 +02:00
if (pkg_is_available("cli") &&
2023-07-14 09:50:13 +02:00
tryCatch(isTRUE(getExportedValue("ansi_has_hyperlink_support", ns = asNamespace("cli"))()), error = function(e) FALSE) &&
tryCatch(getExportedValue("isAvailable", ns = asNamespace("rstudioapi"))(), error = function(e) return(FALSE)) &&
tryCatch(getExportedValue("versionInfo", ns = asNamespace("rstudioapi"))()$version > "2023.6.0.0", error = function(e) return(FALSE))) {
# we are in a recent version of RStudio, so do something nice: add links to our help pages in the console.
2023-07-11 09:50:45 +02:00
parts <- strsplit(msg, "`", fixed = TRUE)[[1]]
cmds <- parts %in% paste0(ls(envir = asNamespace("AMR")), "()")
# functions with a dot are not allowed: https://github.com/rstudio/rstudio/issues/11273#issuecomment-1156193252
2023-07-14 09:50:13 +02:00
# lead them to the help page of our package
2023-07-11 09:50:45 +02:00
parts[cmds & parts %like% "[.]"] <- font_url(url = paste0("ide:help:AMR::", gsub("()", "", parts[cmds & parts %like% "[.]"], fixed = TRUE)),
txt = parts[cmds & parts %like% "[.]"])
2023-07-14 09:50:13 +02:00
# otherwise, give a 'click to run' popup
2023-07-11 09:50:45 +02:00
parts[cmds & parts %unlike% "[.]"] <- font_url(url = paste0("ide:run:AMR::", parts[cmds & parts %unlike% "[.]"]),
txt = parts[cmds & parts %unlike% "[.]"])
# text starting with `?` must also lead to the help page
parts[parts %like% "^[?]"] <- font_url(url = paste0("ide:help:AMR::", gsub("()", "", gsub("^[?]", "", parts[parts %like% "^[?]"]), fixed = TRUE)),
txt = parts[parts %like% "^[?]"])
2023-07-11 09:50:45 +02:00
msg <- paste0(parts, collapse = "`")
}
2022-11-24 20:29:00 +01:00
msg <- gsub("`(.+?)`", font_grey_bg("\\1"), msg)
2023-07-11 09:50:45 +02:00
2023-07-14 09:50:13 +02:00
# clean introduced whitespace in between fullstops
msg <- gsub("[.] +[.]", "..", msg)
2023-05-11 21:56:27 +02:00
# remove extra space that was introduced (e.g. "Smith et al. , 2022")
2022-10-05 09:12:22 +02:00
msg <- gsub(". ,", ".,", msg, fixed = TRUE)
msg <- gsub("[ ,", "[,", msg, fixed = TRUE)
2022-12-30 12:57:27 +01:00
msg <- gsub("/ /", "//", msg, fixed = TRUE)
2022-10-30 14:31:45 +01:00
2020-11-10 16:35:56 +01:00
msg
}
message_ <- function(...,
appendLF = TRUE,
add_fn = list(font_blue),
as_note = TRUE) {
2023-01-23 15:01:21 +01:00
message(
word_wrap(...,
add_fn = add_fn,
as_note = as_note
),
appendLF = appendLF
2022-08-28 10:31:50 +02:00
)
2020-11-10 16:35:56 +01:00
}
warning_ <- function(...,
add_fn = list(),
immediate = FALSE,
call = FALSE) {
2023-01-23 15:01:21 +01:00
warning(
trimws2(word_wrap(...,
2023-01-23 15:01:21 +01:00
add_fn = add_fn,
as_note = FALSE
)),
2023-01-23 15:01:21 +01:00
immediate. = immediate,
call. = call
2022-08-28 10:31:50 +02:00
)
2020-10-26 12:23:03 +01:00
}
# this alternative to the stop() function:
# - adds the function name where the error was thrown
# - wraps text to never break lines within words
stop_ <- function(..., call = TRUE) {
2020-11-28 22:15:44 +01:00
msg <- paste0(c(...), collapse = "")
msg_call <- ""
if (!isFALSE(call)) {
if (isTRUE(call)) {
call <- as.character(sys.call(-1)[1])
} else {
2023-01-21 23:47:20 +01:00
# so you can go back more than 1 call, as used in sir_calc(), that now throws a reference to e.g. n_sir()
call <- as.character(sys.call(call)[1])
}
msg_call <- paste0("in ", call, "():")
}
msg <- trimws2(word_wrap(msg, add_fn = list(), as_note = FALSE))
if (!is.null(AMR_env$cli_abort) && length(unlist(strsplit(msg, "\n", fixed = TRUE))) <= 1) {
if (is.character(call)) {
call <- as.call(str2lang(paste0(call, "()")))
} else {
call <- NULL
}
AMR_env$cli_abort(msg, call = call)
} else {
stop(paste(msg_call, msg), call. = FALSE)
}
}
stop_if <- function(expr, ..., call = TRUE) {
if (isTRUE(expr)) {
if (isTRUE(call)) {
call <- -1
}
2020-06-26 10:21:22 +02:00
if (!isFALSE(call)) {
# since we're calling stop_(), which is another call
call <- call - 1
2020-06-26 10:21:22 +02:00
}
stop_(..., call = call)
2020-03-08 11:18:59 +01:00
}
}
stop_ifnot <- function(expr, ..., call = TRUE) {
2020-09-24 00:30:11 +02:00
if (isFALSE(expr)) {
if (isTRUE(call)) {
call <- -1
}
2020-06-26 10:21:22 +02:00
if (!isFALSE(call)) {
# since we're calling stop_(), which is another call
call <- call - 1
2020-06-26 10:21:22 +02:00
}
stop_(..., call = call)
}
}
2020-03-08 11:18:59 +01:00
2019-05-20 19:12:41 +02:00
"%or%" <- function(x, y) {
2022-10-05 09:12:22 +02:00
if (is.null(x) || is.null(y)) {
if (is.null(x)) {
return(y)
} else {
return(x)
}
}
2022-10-05 09:12:22 +02:00
ifelse(is.na(x), y, x)
2019-05-20 19:12:41 +02:00
}
return_after_integrity_check <- function(value, type, check_vector) {
if (!all(value[!is.na(value)] %in% check_vector)) {
warning_(paste0("invalid ", type, ", NA generated"))
value[!value %in% check_vector] <- NA
}
value
}
2020-01-27 19:14:23 +01:00
2022-08-27 20:49:37 +02:00
# transforms data set to a tibble with only ASCII values, to comply with CRAN policies
2020-01-27 19:14:23 +01:00
dataset_UTF8_to_ASCII <- function(df) {
trans <- function(vect) {
iconv(vect, from = "UTF-8", to = "ASCII//TRANSLIT")
}
df <- as.data.frame(df, stringsAsFactors = FALSE)
for (i in seq_len(NCOL(df))) {
col <- df[, i]
if (is.list(col)) {
col <- lapply(col, function(j) trans(j))
df[, i] <- list(col)
} else {
if (is.factor(col)) {
levels(col) <- trans(levels(col))
} else if (is.character(col)) {
col <- trans(col)
} else {
col
}
df[, i] <- col
}
}
2022-08-28 19:34:04 +02:00
import_fn("as_tibble", "tibble")(df)
2020-01-27 19:14:23 +01:00
}
2020-05-16 13:05:47 +02:00
2022-10-05 09:12:22 +02:00
documentation_date <- function(d) {
2023-05-26 16:10:01 +02:00
day <- as.integer(format(d, "%e"))
suffix <- rep("th", length(day))
suffix[day %in% c(1, 21, 31)] <- "st"
suffix[day %in% c(2, 22)] <- "nd"
suffix[day %in% c(3, 23)] <- "rd"
paste0(month.name[as.integer(format(d, "%m"))], " ", day, suffix, ", ", format(d, "%Y"))
2022-10-05 09:12:22 +02:00
}
format_included_data_number <- function(data) {
2023-03-12 13:02:37 +01:00
if (is.numeric(data) && length(data) == 1) {
n <- data
} else if (is.data.frame(data)) {
2022-10-05 09:12:22 +02:00
n <- nrow(data)
} else {
n <- length(unique(data))
}
if (n > 10000) {
rounder <- -3 # round on thousands
} else if (n > 1000) {
rounder <- -2 # round on hundreds
2023-04-14 11:12:26 +02:00
} else if (n < 50) {
# do not round
rounder <- 0
2022-10-05 09:12:22 +02:00
} else {
rounder <- -1 # round on tens
}
2023-04-14 11:12:26 +02:00
paste0(ifelse(rounder == 0, "", "~"), format(round(n, rounder), decimal.mark = ".", big.mark = " "))
2022-10-05 09:12:22 +02:00
}
2020-11-28 22:15:44 +01:00
# for eucast_rules() and mdro(), creates markdown output with URLs and names
2021-05-18 11:29:31 +02:00
create_eucast_ab_documentation <- function() {
2022-10-05 09:12:22 +02:00
x <- trimws(unique(toupper(unlist(strsplit(EUCAST_RULES_DF$then_change_these_antibiotics, ",", fixed = TRUE)))))
2021-05-18 11:29:31 +02:00
ab <- character()
for (val in x) {
2021-08-17 14:34:11 +02:00
if (paste0("AB_", val) %in% ls(envir = asNamespace("AMR"))) {
# antibiotic group names, as defined in data-raw/_pre_commit_checks.R, such as `CARBAPENEMS`
2021-08-17 14:34:11 +02:00
val <- eval(parse(text = paste0("AB_", val)), envir = asNamespace("AMR"))
2022-10-14 13:02:50 +02:00
} else if (val %in% AMR_env$AB_lookup$ab) {
2021-05-18 11:29:31 +02:00
# separate drugs, such as `AMX`
val <- as.ab(val)
} else {
2023-01-21 23:47:20 +01:00
val <- as.sir(NA)
2021-05-18 11:29:31 +02:00
}
ab <- c(ab, val)
}
ab <- unique(ab)
2021-08-17 14:34:11 +02:00
atcs <- ab_atc(ab, only_first = TRUE)
2021-05-18 11:29:31 +02:00
# only keep ABx with an ATC code:
ab <- ab[!is.na(atcs)]
2023-03-16 08:55:37 +01:00
atcs <- atcs[!is.na(atcs)]
# sort all vectors on name:
2020-09-24 00:30:11 +02:00
ab_names <- ab_name(ab, language = NULL, tolower = TRUE)
ab <- ab[order(ab_names)]
2023-03-16 08:55:37 +01:00
atcs <- atcs[order(ab_names)]
2020-09-24 00:30:11 +02:00
ab_names <- ab_names[order(ab_names)]
2023-03-16 08:55:37 +01:00
# create the text:
atc_txt <- paste0("[", atcs, "](", ab_url(ab), ")")
2021-05-18 11:29:31 +02:00
out <- paste0(ab_names, " (`", ab, "`, ", atc_txt, ")", collapse = ", ")
2020-09-24 00:30:11 +02:00
substr(out, 1, 1) <- toupper(substr(out, 1, 1))
out
}
vector_or <- function(v, quotes = TRUE, reverse = FALSE, sort = TRUE, initial_captital = FALSE, last_sep = " or ") {
# makes unique and sorts, and this also removed NAs
v <- unique(v)
if (isTRUE(sort)) {
v <- sort(v)
}
if (isTRUE(reverse)) {
v <- rev(v)
}
if (isTRUE(quotes)) {
quotes <- '"'
} else if (isFALSE(quotes)) {
quotes <- ""
} else {
quotes <- quotes[1L]
}
if (isTRUE(initial_captital)) {
v[1] <- gsub("^([a-z])", "\\U\\1", v[1], perl = TRUE)
}
if (length(v) <= 1) {
return(paste0(quotes, v, quotes))
}
if (identical(v, c("I", "R", "S"))) {
2023-01-21 23:47:20 +01:00
# class 'sir' should be sorted like this
v <- c("S", "I", "R")
}
if (identical(v, c("I", "NI", "R", "S", "SDD"))) {
2024-05-20 15:27:04 +02:00
# class 'sir' should be sorted like this
v <- c("S", "SDD", "I", "R", "NI")
2024-05-20 15:27:04 +02:00
}
# oxford comma
if (last_sep %in% c(" or ", " and ") && length(v) > 2) {
last_sep <- paste0(",", last_sep)
}
# all commas except for last item, so will become '"val1", "val2", "val3" or "val4"'
2022-08-28 10:31:50 +02:00
paste0(
paste0(quotes, v[seq_len(length(v) - 1)], quotes, collapse = ", "),
last_sep, paste0(quotes, v[length(v)], quotes)
)
}
vector_and <- function(v, quotes = TRUE, reverse = FALSE, sort = TRUE, initial_captital = FALSE) {
2022-08-28 10:31:50 +02:00
vector_or(
v = v, quotes = quotes, reverse = reverse, sort = sort,
initial_captital = initial_captital, last_sep = " and "
)
}
2021-05-12 18:15:03 +02:00
format_class <- function(class, plural = FALSE) {
class.bak <- class
class[class == "numeric"] <- "number"
class[class == "integer"] <- "whole number"
if (all(c("numeric", "integer") %in% class.bak, na.rm = TRUE)) {
class[class %in% c("number", "whole number")] <- "(whole) number"
}
class[class == "character"] <- "text string"
2022-10-05 09:12:22 +02:00
class[class == "Date"] <- "date"
class[class %in% c("POSIXt", "POSIXct", "POSIXlt")] <- "date/time"
2022-08-28 10:31:50 +02:00
class[class != class.bak] <- paste0(
ifelse(plural, "", "a "),
class[class != class.bak],
ifelse(plural, "s", "")
)
# exceptions
class[class == "logical"] <- ifelse(plural, "a vector of `TRUE`/`FALSE`", "`TRUE` or `FALSE`")
2021-05-12 18:15:03 +02:00
class[class == "data.frame"] <- "a data set"
if ("list" %in% class) {
class <- "a list"
}
if ("matrix" %in% class) {
class <- "a matrix"
}
2021-04-07 08:37:42 +02:00
if ("custom_eucast_rules" %in% class) {
class <- "input created with `custom_eucast_rules()`"
2021-01-22 10:55:07 +01:00
}
2023-01-21 23:47:20 +01:00
if (any(c("mo", "ab", "sir") %in% class)) {
2023-01-30 17:24:03 +01:00
class <- paste0("of class '", class[1L], "'")
}
2023-01-30 17:24:03 +01:00
class[class == class.bak] <- paste0("of class '", class[class == class.bak], "'")
# output
vector_or(class, quotes = FALSE, sort = FALSE)
}
# a check for every single argument in all functions
2023-03-11 14:24:34 +01:00
meet_criteria <- function(object, # can be literally `list(...)` for `allow_arguments_from`
allow_class = NULL,
has_length = NULL,
looks_like = NULL,
is_in = NULL,
is_positive = NULL,
2021-04-07 08:37:42 +02:00
is_positive_or_zero = NULL,
is_finite = NULL,
allow_NULL = FALSE,
allow_NA = FALSE,
ignore.case = FALSE,
2023-03-11 14:24:34 +01:00
allow_arguments_from = NULL, # 1 function, or a list of functions
.call_depth = 0) { # depth in calling
2020-10-26 12:23:03 +01:00
obj_name <- deparse(substitute(object))
call_depth <- -2 - abs(.call_depth)
2022-08-28 10:31:50 +02:00
# if object is missing, or another error:
tryCatch(invisible(object),
2022-10-05 09:12:22 +02:00
error = function(e) AMR_env$meet_criteria_error_txt <- e$message
2022-08-28 10:31:50 +02:00
)
2022-10-05 09:12:22 +02:00
if (!is.null(AMR_env$meet_criteria_error_txt)) {
error_txt <- AMR_env$meet_criteria_error_txt
AMR_env$meet_criteria_error_txt <- NULL
2022-08-27 20:49:37 +02:00
stop(error_txt, call. = FALSE) # don't use stop_() here, our pkg may not be loaded yet
}
2022-10-05 09:12:22 +02:00
AMR_env$meet_criteria_error_txt <- NULL
2020-10-26 12:23:03 +01:00
if (is.null(object)) {
stop_if(allow_NULL == FALSE, "argument `", obj_name, "` must not be NULL", call = call_depth)
return(invisible())
}
2021-01-04 12:29:25 +01:00
if (is.null(dim(object)) && length(object) == 1 && suppressWarnings(is.na(object))) { # suppressWarnings for functions
stop_if(allow_NA == FALSE, "argument `", obj_name, "` must not be NA", call = call_depth)
return(invisible())
}
2024-10-15 17:12:55 +02:00
if (identical(class(object), "list") && !"list" %in% allow_class) {
# coming from Python, possibly - turn lists (not data.frame) to the underlying data type
object <- unlist(object)
}
2020-10-26 12:23:03 +01:00
2024-04-19 10:18:21 +02:00
if (!is.null(allow_class) && !(suppressWarnings(all(is.na(object))) && allow_NA == TRUE)) {
2020-10-26 12:23:03 +01:00
stop_ifnot(inherits(object, allow_class), "argument `", obj_name,
2022-08-28 10:31:50 +02:00
"` must be ", format_class(allow_class, plural = isTRUE(has_length > 1)),
", i.e. not be ", format_class(class(object), plural = isTRUE(has_length > 1)),
call = call_depth
)
# check data.frames for data
if (inherits(object, "data.frame")) {
2020-10-26 12:23:03 +01:00
stop_if(any(dim(object) == 0),
2022-08-28 10:31:50 +02:00
"the data provided in argument `", obj_name,
"` must contain rows and columns (current dimensions: ",
paste(dim(object), collapse = "x"), ")",
call = call_depth
)
}
}
if (!is.null(has_length)) {
2020-10-26 12:23:03 +01:00
stop_ifnot(length(object) %in% has_length, "argument `", obj_name,
2022-08-28 10:31:50 +02:00
"` must ", # ifelse(allow_NULL, "be NULL or must ", ""),
"be of length ", vector_or(has_length, quotes = FALSE),
", not ", length(object),
call = call_depth
)
}
if (!is.null(looks_like)) {
2020-10-26 12:23:03 +01:00
stop_ifnot(object %like% looks_like, "argument `", obj_name,
2022-08-28 10:31:50 +02:00
"` must ", # ifelse(allow_NULL, "be NULL or must ", ""),
"resemble the regular expression \"", looks_like, "\"",
call = call_depth
)
}
if (!is.null(is_in)) {
if (ignore.case == TRUE) {
object <- tolower(object)
is_in <- tolower(is_in)
}
2023-12-03 01:06:00 +01:00
is_in.bak <- is_in
if ("logical" %in% allow_class) {
is_in <- is_in[!is_in %in% c("TRUE", "FALSE")]
}
2024-04-05 16:44:43 +02:00
or_values <- vector_or(is_in, quotes = !isTRUE(any(c("numeric", "integer") %in% allow_class)))
2023-12-03 01:06:00 +01:00
if ("logical" %in% allow_class) {
or_values <- paste0(or_values, ", or TRUE or FALSE")
}
stop_ifnot(all(object %in% is_in.bak, na.rm = TRUE), "argument `", obj_name, "` ",
2022-08-28 10:31:50 +02:00
ifelse(!is.null(has_length) && length(has_length) == 1 && has_length == 1,
"must be either ",
"must only contain values "
),
2023-12-03 01:06:00 +01:00
or_values,
2022-08-28 10:31:50 +02:00
ifelse(allow_NA == TRUE, ", or NA", ""),
call = call_depth
)
}
2021-04-12 12:35:13 +02:00
if (isTRUE(is_positive)) {
stop_if(is.numeric(object) && !all(object > 0, na.rm = TRUE), "argument `", obj_name,
2022-08-28 10:31:50 +02:00
"` must ",
ifelse(!is.null(has_length) && length(has_length) == 1 && has_length == 1,
"be a number higher than zero",
"all be numbers higher than zero"
),
call = call_depth
)
2021-04-07 08:37:42 +02:00
}
2021-04-12 12:35:13 +02:00
if (isTRUE(is_positive_or_zero)) {
2021-04-07 08:37:42 +02:00
stop_if(is.numeric(object) && !all(object >= 0, na.rm = TRUE), "argument `", obj_name,
2022-08-28 10:31:50 +02:00
"` must ",
ifelse(!is.null(has_length) && length(has_length) == 1 && has_length == 1,
"be zero or a positive number",
"all be zero or numbers higher than zero"
),
call = call_depth
)
}
2021-04-12 12:35:13 +02:00
if (isTRUE(is_finite)) {
stop_if(is.numeric(object) && !all(is.finite(object[!is.na(object)]), na.rm = TRUE), "argument `", obj_name,
2022-08-28 10:31:50 +02:00
"` must ",
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)",
call = call_depth
)
}
2023-03-11 14:24:34 +01:00
if (!is.null(allow_arguments_from) && !is.null(names(object))) {
args_given <- names(object)
if (is.function(allow_arguments_from)) {
allow_arguments_from <- list(allow_arguments_from)
}
args_allowed <- sort(unique(unlist(lapply(allow_arguments_from, function(x) names(formals(x))))))
args_allowed <- args_allowed[args_allowed != "..."]
disallowed <- args_given[!args_given %in% args_allowed]
stop_if(length(disallowed) > 0,
ifelse(length(disallowed) == 1,
paste("the argument", vector_and(disallowed), "is"),
paste("the arguments", vector_and(disallowed), "are")
),
" not valid. Valid arguments are: ",
vector_and(args_allowed), ".",
call = call_depth
)
}
return(invisible())
}
2024-10-15 17:12:55 +02:00
ascertain_sir_classes <- function(x, obj_name) {
sirs <- vapply(FUN.VALUE = logical(1), x, is.sir)
if (!any(sirs, na.rm = TRUE)) {
warning_("the data provided in argument `", obj_name,
"` should contain at least one column of class 'sir'. Eligible SIR column were now guessed. ",
"See `?as.sir`.")
sirs_eligible <- is_sir_eligible(x)
for (col in colnames(x)[sirs_eligible]) {
x[[col]] <- as.sir(x[[col]])
}
}
x
}
get_current_data <- function(arg_name, call) {
valid_df <- function(x) {
!is.null(x) && is.data.frame(x)
}
2022-10-30 14:31:45 +01:00
2023-02-15 19:48:34 +01:00
frms <- sys.frames()
2023-03-11 14:24:34 +01:00
2023-02-15 19:48:34 +01:00
# check dplyr environments to support dplyr groups
with_mask <- vapply(FUN.VALUE = logical(1), frms, function(e) !is.null(e$mask))
for (env in frms[which(with_mask)]) {
if (is.function(env$mask$current_rows) && (valid_df(env$data) || valid_df(env$`.data`))) {
2023-02-15 17:02:10 +01:00
# an element `.data` or `data` (containing all data) and `mask` (containing functions) will be in the environment when using dplyr verbs
# we use their mask$current_rows() to get the group rows, since dplyr::cur_data_all() is deprecated and will be removed in the future
# e.g. for `example_isolates %>% group_by(ward) %>% mutate(first = first_isolate(.))`
if (valid_df(env$data)) {
# support for dplyr 1.1.x
2023-02-15 17:37:49 +01:00
df <- env$data
2023-02-15 17:02:10 +01:00
} else {
# support for dplyr 1.0.x
2023-02-15 17:37:49 +01:00
df <- env$`.data`
2023-02-15 17:02:10 +01:00
}
2023-02-15 17:37:49 +01:00
rows <- tryCatch(env$mask$current_rows(), error = function(e) seq_len(NROW(df)))
return(df[rows, , drop = FALSE])
2023-02-15 19:48:34 +01:00
}
}
# now go over all underlying environments looking for other dplyr, data.table and base R selection environments
2023-02-15 19:48:34 +01:00
with_generic <- vapply(FUN.VALUE = logical(1), frms, function(e) !is.null(e$`.Generic`))
for (env in frms[which(with_generic)]) {
if (valid_df(env$`.data`)) {
# an element `.data` will be in the environment when using dplyr::select()
return(env$`.data`)
} else if (valid_df(env$xx)) {
# an element `xx` will be in the environment for rows + cols in base R, e.g. `example_isolates[c(1:3), carbapenems()]`
return(env$xx)
} else if (valid_df(env$x)) {
# an element `x` will be in the environment for only cols in base R, e.g. `example_isolates[, carbapenems()]`
# this element will also be present in data.table environments where there's a .Generic available
2023-02-15 19:48:34 +01:00
return(env$x)
}
}
2023-03-11 14:24:34 +01:00
2023-02-15 19:48:34 +01:00
# now a special case for dplyr's 'scoped' variants
with_tbl <- vapply(FUN.VALUE = logical(1), frms, function(e) valid_df(e$`.tbl`))
for (env in frms[which(with_tbl)]) {
if (!is.null(names(env)) && all(c(".tbl", ".vars", ".cols") %in% names(env), na.rm = TRUE)) {
2022-10-21 15:13:19 +02:00
# an element `.tbl` will be in the environment when using scoped dplyr variants, with or without `dplyr::vars()`
# (e.g. `dplyr::summarise_at()` or `dplyr::mutate_at()`)
return(env$`.tbl`)
}
}
2022-10-30 14:31:45 +01:00
# 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])
2022-08-28 10:31:50 +02:00
examples <- paste0(
", e.g.:\n",
" ", AMR_env$bullet_icon, " your_data %>% select(", fn, "())\n",
" ", AMR_env$bullet_icon, " your_data %>% select(column_a, column_b, ", fn, "())\n",
" ", AMR_env$bullet_icon, " your_data[, ", fn, "()]\n",
" ", AMR_env$bullet_icon, " your_data[, c(\"column_a\", \"column_b\", ", fn, "())]"
2022-08-28 10:31:50 +02:00
)
} else {
examples <- ""
}
stop_("this function must be used inside a `dplyr` verb or `data.frame` call",
2022-08-28 10:31:50 +02:00
examples,
call = call
)
} else {
# mimic a base R error that the argument is missing
stop_("argument `", arg_name, "` is missing with no default", call = call)
}
}
get_current_column <- function() {
# try dplyr::cur_columns() first
cur_column <- import_fn("cur_column", "dplyr", error_on_fail = FALSE)
out <- tryCatch(cur_column(), error = function(e) NULL)
if (!is.null(out)) {
return(out)
}
2022-08-28 10:31:50 +02:00
# cur_column() doesn't always work (only allowed for certain conditions set by dplyr), but it's probably still possible:
frms <- lapply(sys.frames(), function(env) {
2023-05-26 20:37:00 +02:00
if (tryCatch(!is.null(env$i), error = function(e) FALSE)) {
if (!is.null(env$tibble_vars)) {
# for mutate_if()
# TODO remove later, was part of older dplyr versions (at least not in dplyr 1.1.4)
env$tibble_vars[env$i]
} else {
# for mutate(across())
if (!is.null(env$data) && is.data.frame(env$data)) {
df <- env$data
} else {
df <- tryCatch(get_current_data(NA, 0), error = function(e) NULL)
}
if (is.data.frame(df)) {
colnames(df)[env$i]
} else {
env$i
}
}
} else {
NULL
}
})
2022-08-28 10:31:50 +02:00
vars <- unlist(frms)
if (length(vars) > 0) {
vars[length(vars)]
} else {
# not found, so:
NULL
}
}
is_null_or_grouped_tbl <- function(x) {
# class "grouped_data" is from {poorman}, see aa_helper_pm_functions.R
# class "grouped_df" is from {dplyr} and might change at one point, so only set in one place; here.
is.null(x) || inherits(x, "grouped_data") || inherits(x, "grouped_df")
}
get_group_names <- function(x) {
if ("pm_groups" %in% names(attributes(x))) {
pm_get_groups(x)
} else if (!is.null(x) && is_null_or_grouped_tbl(x)) {
grps <- colnames(attributes(x)$groups)
grps[!grps %in% c(".group_id", ".rows")]
} else {
character(0)
}
}
unique_call_id <- function(entire_session = FALSE, match_fn = NULL) {
if (entire_session == TRUE) {
return(c(envir = "session", call = "session"))
}
2022-08-28 10:31:50 +02:00
# combination of environment ID (such as "0x7fed4ee8c848")
# and relevant system call (where 'match_fn' is being called in)
calls <- sys.calls()
2022-10-05 09:12:22 +02:00
in_test <- any(as.character(calls[[1]]) %like_case% "run_test_dir|run_test_file|test_all|tinytest|test_package|testthat", na.rm = TRUE)
2023-01-05 14:43:18 +01:00
if (!isTRUE(in_test) && !is.null(match_fn)) {
2021-12-11 15:13:44 +01:00
for (i in seq_len(length(calls))) {
call_clean <- gsub("[^a-zA-Z0-9_().-]", "", as.character(calls[[i]]), perl = TRUE)
2022-11-24 20:29:00 +01:00
if (match_fn %in% call_clean || any(call_clean %like% paste0(match_fn, "\\("), na.rm = TRUE)) {
2022-08-28 10:31:50 +02:00
return(c(
envir = gsub("<environment: (.*)>", "\\1", utils::capture.output(sys.frames()[[1]]), perl = TRUE),
call = paste0(deparse(calls[[i]]), collapse = "")
))
2021-12-11 15:13:44 +01:00
}
}
}
2022-08-28 10:31:50 +02:00
c(
2022-10-05 09:12:22 +02:00
envir = paste0(sample(c(0:9, letters[1:6]), size = 32, replace = TRUE), collapse = ""),
call = paste0(sample(c(0:9, letters[1:6]), size = 32, replace = TRUE), collapse = "")
2022-08-28 10:31:50 +02:00
)
}
#' @noRd
#' @param fn name of the function as a character
#' @param ... character elements to be pasted together as a 'salt'
#' @param entire_session show message once per session
message_not_thrown_before <- function(fn, ..., entire_session = FALSE) {
# this is to prevent that messages/notes will be printed for every dplyr group or more than once per session
2022-08-27 20:49:37 +02:00
# e.g. this would show a msg 4 times: example_isolates %>% group_by(ward) %>% filter(mo_is_gram_negative())
2022-10-05 09:12:22 +02:00
salt <- gsub("[^a-zA-Z0-9|_-]", "?", substr(paste(c(...), sep = "|", collapse = "|"), 1, 512), perl = TRUE)
not_thrown_before <- is.null(AMR_env[[paste0("thrown_msg.", fn, ".", salt)]]) ||
2022-08-28 10:31:50 +02:00
!identical(
2022-10-05 09:12:22 +02:00
AMR_env[[paste0("thrown_msg.", fn, ".", salt)]],
2022-08-28 10:31:50 +02:00
unique_call_id(
entire_session = entire_session,
match_fn = fn
)
)
if (isTRUE(not_thrown_before)) {
# message was not thrown before - remember this so on the next run it will return FALSE:
2022-08-28 10:31:50 +02:00
assign(
x = paste0("thrown_msg.", fn, ".", salt),
value = unique_call_id(entire_session = entire_session, match_fn = fn),
2022-10-05 09:12:22 +02:00
envir = AMR_env
2022-08-28 10:31:50 +02:00
)
}
not_thrown_before
2020-12-27 00:07:00 +01:00
}
2020-06-17 15:14:37 +02:00
has_colour <- function() {
2020-12-27 00:07:00 +01:00
# this is a base R version of crayon::has_color, but disables colours on emacs
2022-08-28 10:31:50 +02:00
2020-12-27 00:07:00 +01:00
if (Sys.getenv("EMACS") != "" || Sys.getenv("INSIDE_EMACS") != "") {
# disable on emacs, which only supports 8 colours
2020-12-27 00:07:00 +01:00
return(FALSE)
}
enabled <- getOption("crayon.enabled")
if (!is.null(enabled)) {
return(isTRUE(enabled))
}
rstudio_with_ansi_support <- function(x) {
if (Sys.getenv("RSTUDIO", "") == "") {
return(FALSE)
}
2021-05-24 00:06:28 +02:00
if ((cols <- Sys.getenv("RSTUDIO_CONSOLE_COLOR", "")) != "" && !is.na(as.double(cols))) {
return(TRUE)
}
2022-09-01 15:20:57 +02:00
tryCatch(getExportedValue("isAvailable", ns = asNamespace("rstudioapi"))(), error = function(e) {
2022-08-28 10:31:50 +02:00
return(FALSE)
}) &&
2022-09-01 15:20:57 +02:00
tryCatch(getExportedValue("hasFun", ns = asNamespace("rstudioapi"))("getConsoleHasColor"), error = function(e) {
2022-08-28 10:31:50 +02:00
return(FALSE)
})
}
if (rstudio_with_ansi_support() && sink.number() == 0) {
return(TRUE)
}
if (!isatty(stdout())) {
2020-05-16 13:05:47 +02:00
return(FALSE)
}
if (tolower(Sys.info()["sysname"]) == "windows") {
if (Sys.getenv("ConEmuANSI") == "ON") {
2020-05-16 13:05:47 +02:00
return(TRUE)
}
if (Sys.getenv("CMDER_ROOT") != "") {
return(TRUE)
}
return(FALSE)
}
if ("COLORTERM" %in% names(Sys.getenv())) {
return(TRUE)
}
if (Sys.getenv("TERM") == "dumb") {
return(FALSE)
}
2022-08-28 10:31:50 +02:00
grepl(
pattern = "^screen|^xterm|^vt100|color|ansi|cygwin|linux",
x = Sys.getenv("TERM"),
ignore.case = TRUE,
perl = TRUE
)
2020-05-16 13:05:47 +02:00
}
# set colours if console has_colour()
2020-05-16 13:05:47 +02:00
try_colour <- function(..., before, after, collapse = " ") {
2022-10-05 09:12:22 +02:00
if (length(c(...)) == 0) {
return(character(0))
}
txt <- paste0(c(...), collapse = collapse)
2020-05-16 13:05:47 +02:00
if (isTRUE(has_colour())) {
if (is.null(collapse)) {
paste0(before, txt, after, collapse = NULL)
} else {
paste0(before, txt, after, collapse = "")
}
} else {
txt
}
}
2022-10-30 21:05:46 +01:00
is_dark <- function() {
if (is.null(AMR_env$is_dark_theme)) {
2023-02-18 13:08:08 +01:00
AMR_env$is_dark_theme <- !has_colour() || tryCatch(isTRUE(getExportedValue("getThemeInfo", ns = asNamespace("rstudioapi"))()$dark), error = function(e) FALSE)
2022-10-30 21:05:46 +01:00
}
isTRUE(AMR_env$is_dark_theme)
}
font_black <- function(..., collapse = " ", adapt = TRUE) {
2022-09-01 15:20:57 +02:00
before <- "\033[38;5;232m"
after <- "\033[39m"
if (isTRUE(adapt) && is_dark()) {
2022-09-01 15:20:57 +02:00
# white
before <- "\033[37m"
after <- "\033[39m"
}
try_colour(..., before = before, after = after, collapse = collapse)
}
font_white <- function(..., collapse = " ", adapt = TRUE) {
2022-09-01 15:20:57 +02:00
before <- "\033[37m"
after <- "\033[39m"
if (isTRUE(adapt) && is_dark()) {
2022-09-01 15:20:57 +02:00
# black
before <- "\033[38;5;232m"
after <- "\033[39m"
}
try_colour(..., before = before, after = after, collapse = collapse)
2020-05-16 13:05:47 +02:00
}
font_blue <- function(..., collapse = " ") {
try_colour(..., before = "\033[34m", after = "\033[39m", collapse = collapse)
}
font_green <- function(..., collapse = " ") {
try_colour(..., before = "\033[32m", after = "\033[39m", collapse = collapse)
}
font_magenta <- function(..., collapse = " ") {
try_colour(..., before = "\033[35m", after = "\033[39m", collapse = collapse)
}
font_red <- function(..., collapse = " ") {
try_colour(..., before = "\033[31m", after = "\033[39m", collapse = collapse)
}
font_silver <- function(..., collapse = " ") {
try_colour(..., before = "\033[90m", after = "\033[39m", collapse = collapse)
}
font_yellow <- function(..., collapse = " ") {
try_colour(..., before = "\033[33m", after = "\033[39m", collapse = collapse)
}
font_subtle <- function(..., collapse = " ") {
try_colour(..., before = "\033[38;5;246m", after = "\033[39m", collapse = collapse)
}
font_grey <- function(..., collapse = " ") {
try_colour(..., before = "\033[38;5;249m", after = "\033[39m", collapse = collapse)
}
font_grey_bg <- function(..., collapse = " ") {
2022-10-31 13:25:41 +01:00
if (is_dark()) {
# similar to HTML #444444
2022-08-28 10:31:50 +02:00
try_colour(..., before = "\033[48;5;238m", after = "\033[49m", collapse = collapse)
} else {
# similar to HTML #f0f0f0
try_colour(..., before = "\033[48;5;255m", after = "\033[49m", collapse = collapse)
}
}
2022-10-05 09:12:22 +02:00
font_red_bg <- function(..., collapse = " ") {
2023-01-21 23:47:20 +01:00
# this is #ed553b (picked to be colourblind-safe with other SIR colours)
try_colour(font_black(..., collapse = collapse, adapt = FALSE), before = "\033[48;5;203m", after = "\033[49m", collapse = collapse)
2020-05-16 13:05:47 +02:00
}
2022-10-05 09:12:22 +02:00
font_orange_bg <- function(..., collapse = " ") {
2023-01-21 23:47:20 +01:00
# this is #f6d55c (picked to be colourblind-safe with other SIR colours)
2023-07-10 13:41:52 +02:00
try_colour(font_black(..., collapse = collapse, adapt = FALSE), before = "\033[48;5;222m", after = "\033[49m", collapse = collapse)
2020-12-27 00:07:00 +01:00
}
2022-10-05 09:12:22 +02:00
font_yellow_bg <- function(..., collapse = " ") {
2023-07-10 13:41:52 +02:00
try_colour(font_black(..., collapse = collapse, adapt = FALSE), before = "\033[48;5;228m", after = "\033[49m", collapse = collapse)
2020-12-27 00:07:00 +01:00
}
2022-10-05 09:12:22 +02:00
font_green_bg <- function(..., collapse = " ") {
2023-01-21 23:47:20 +01:00
# this is #3caea3 (picked to be colourblind-safe with other SIR colours)
2023-07-10 13:41:52 +02:00
try_colour(font_black(..., collapse = collapse, adapt = FALSE), before = "\033[48;5;79m", after = "\033[49m", collapse = collapse)
2020-12-27 00:07:00 +01:00
}
2022-10-05 09:12:22 +02:00
font_purple_bg <- function(..., collapse = " ") {
2023-07-10 13:41:52 +02:00
try_colour(font_black(..., collapse = collapse, adapt = FALSE), before = "\033[48;5;89m", after = "\033[49m", collapse = collapse)
2020-05-16 13:05:47 +02:00
}
2022-10-05 09:12:22 +02:00
font_rose_bg <- function(..., collapse = " ") {
2023-07-10 13:41:52 +02:00
try_colour(font_black(..., collapse = collapse, adapt = FALSE), before = "\033[48;5;217m", after = "\033[49m", collapse = collapse)
2020-05-16 13:05:47 +02:00
}
font_na <- function(..., collapse = " ") {
font_red(..., collapse = collapse)
}
2020-05-16 13:05:47 +02:00
font_bold <- function(..., collapse = " ") {
try_colour(..., before = "\033[1m", after = "\033[22m", collapse = collapse)
}
font_italic <- function(..., collapse = " ") {
try_colour(..., before = "\033[3m", after = "\033[23m", collapse = collapse)
}
font_underline <- function(..., collapse = " ") {
try_colour(..., before = "\033[4m", after = "\033[24m", collapse = collapse)
}
2022-11-24 20:29:00 +01:00
font_url <- function(url, txt = url) {
if (tryCatch(isTRUE(getExportedValue("ansi_has_hyperlink_support", ns = asNamespace("cli"))()), error = function(e) FALSE)) {
paste0("\033]8;;", url, "\a", txt, "\033]8;;\a")
} else {
url
}
}
2020-05-16 13:05:47 +02:00
font_stripstyle <- function(x) {
2022-11-24 20:29:00 +01:00
# remove URLs
x <- gsub("\033]8;;(.*?)\a.*?\033]8;;\a", "\\1", x)
2020-05-16 13:05:47 +02:00
# from crayon:::ansi_regex
2022-11-24 20:29:00 +01:00
x <- gsub("(?:(?:\\x{001b}\\[)|\\x{009b})(?:(?:[0-9]{1,3})?(?:(?:;[0-9]{0,3})*)?[A-M|f-m])|\\x{001b}[A-M]", "", x, perl = TRUE)
x
2020-05-16 13:05:47 +02:00
}
progress_ticker <- function(n = 1, n_min = 0, print = TRUE, clear = TRUE, title = "", only_bar_percent = FALSE, ...) {
if (print == FALSE || n < n_min) {
2022-10-06 11:33:30 +02:00
# create fake/empty object
2020-05-16 13:05:47 +02:00
pb <- list()
pb$tick <- function() {
invisible()
}
pb$kill <- function() {
invisible()
}
set_clean_class(pb, new_class = "txtProgressBar")
2020-07-29 10:33:47 +02:00
} else if (n >= n_min) {
title <- trimws2(title)
if (title != "") {
title <- paste0(title, " ")
}
2021-12-13 10:18:28 +01:00
progress_bar <- import_fn("progress_bar", "progress", error_on_fail = FALSE)
if (!is.null(progress_bar)) {
# so we use progress::progress_bar
# a close()-method was also added, see below for that
2022-08-28 10:31:50 +02:00
pb <- progress_bar$new(
show_after = 0,
format = paste0(title,
ifelse(only_bar_percent == TRUE, "[:bar] :percent", "[:bar] :percent (:current/:total,:eta)")),
clear = clear,
2022-08-28 10:31:50 +02:00
total = n
)
2021-12-13 10:18:28 +01:00
} else {
# use base R's txtProgressBar
cat(title, "\n", sep = "")
2021-12-13 10:18:28 +01:00
pb <- utils::txtProgressBar(max = n, style = 3)
pb$tick <- function() {
pb$up(pb$getVal() + 1)
}
2020-07-29 10:33:47 +02:00
}
pb
2020-05-16 13:05:47 +02:00
}
}
2021-12-13 10:18:28 +01:00
#' @method close progress_bar
#' @export
#' @noRd
close.progress_bar <- function(con, ...) {
2022-10-06 11:33:30 +02:00
# for progress::progress_bar$new()
2021-12-13 10:18:28 +01:00
con$terminate()
}
set_clean_class <- function(x, new_class) {
# return the object with only the new class and no additional attributes where possible
if (is.null(x)) {
x <- NA_character_
}
if (is.factor(x)) {
# keep only levels and remove all other attributes
lvls <- levels(x)
attributes(x) <- NULL
levels(x) <- lvls
2020-11-16 20:02:20 +01:00
} else if (!is.list(x) && !is.function(x)) {
attributes(x) <- NULL
}
class(x) <- new_class
x
}
2020-12-17 16:22:25 +01:00
formatted_filesize <- function(...) {
size_kb <- file.size(...) / 1024
if (size_kb < 1) {
paste(round(size_kb, 1), "kB")
} else if (size_kb < 100) {
paste(round(size_kb, 0), "kB")
} else {
paste(round(size_kb / 1024, 1), "MB")
}
}
create_pillar_column <- function(x, ...) {
new_pillar_shaft_simple <- import_fn("new_pillar_shaft_simple", "pillar")
new_pillar_shaft_simple(x, ...)
}
as_original_data_class <- function(df, old_class = NULL, extra_class = NULL) {
2023-02-18 14:56:06 +01:00
if ("tbl_df" %in% old_class && pkg_is_available("tibble")) {
2023-01-05 14:43:18 +01:00
# this will then also remove groups
2022-08-27 20:49:37 +02:00
fn <- import_fn("as_tibble", "tibble")
2023-02-18 14:56:06 +01:00
} else if ("data.table" %in% old_class && pkg_is_available("data.table")) {
2022-08-27 20:49:37 +02:00
fn <- import_fn("as.data.table", "data.table")
} else {
2023-01-05 14:43:18 +01:00
fn <- function(x) base::as.data.frame(df, stringsAsFactors = FALSE)
2022-08-27 20:49:37 +02:00
}
out <- fn(df)
if (!is.null(extra_class)) {
class(out) <- c(extra_class, class(out))
}
out
2022-08-27 20:49:37 +02:00
}
# works exactly like round(), but rounds `round2(44.55, 1)` to 44.6 instead of 44.5
2020-05-16 13:05:47 +02:00
# and adds decimal zeroes until `digits` is reached when force_zero = TRUE
2021-05-24 15:29:17 +02:00
round2 <- function(x, digits = 1, force_zero = TRUE) {
2020-05-16 13:05:47 +02:00
x <- as.double(x)
# https://stackoverflow.com/a/12688836/4575331
2022-08-28 10:31:50 +02:00
val <- (trunc((abs(x) * 10^digits) + 0.5) / 10^digits) * sign(x)
2022-10-05 09:12:22 +02:00
if (digits > 0 && force_zero == TRUE) {
2020-05-16 13:05:47 +02:00
values_trans <- val[val != as.integer(val) & !is.na(val)]
2022-08-28 10:31:50 +02:00
val[val != as.integer(val) & !is.na(val)] <- paste0(
values_trans,
strrep(
"0",
max(
0,
digits - nchar(
format(
as.double(
gsub(
".*[.](.*)$",
"\\1",
values_trans
)
),
scientific = FALSE
)
)
)
)
)
2020-05-16 13:05:47 +02:00
}
as.double(val)
}
# percentage from our other package: 'cleaner'
percentage <- function(x, digits = NULL, ...) {
# getdecimalplaces() function
getdecimalplaces <- function(x, minimum = 0, maximum = 3) {
if (maximum < minimum) {
maximum <- minimum
}
if (minimum > maximum) {
minimum <- maximum
}
2022-08-28 10:31:50 +02:00
max_places <- max(unlist(lapply(
strsplit(sub(
"0+$", "",
as.character(x * 100)
), ".", fixed = TRUE),
function(y) ifelse(length(y) == 2, nchar(y[2]), 0)
)), na.rm = TRUE)
2023-01-23 15:01:21 +01:00
max(
min(max_places,
maximum,
na.rm = TRUE
),
minimum,
2022-08-28 10:31:50 +02:00
na.rm = TRUE
)
2020-05-16 13:05:47 +02:00
}
2020-10-26 12:23:03 +01:00
2020-05-16 13:05:47 +02:00
# format_percentage() function
format_percentage <- function(x, digits = NULL, ...) {
if (is.null(digits)) {
digits <- getdecimalplaces(x)
}
2021-05-20 11:42:39 +02:00
if (is.null(digits) || is.na(digits) || !is.numeric(digits)) {
digits <- 2
}
2020-10-26 12:23:03 +01:00
2020-05-16 13:05:47 +02:00
# round right: percentage(0.4455) and format(as.percentage(0.4455), 1) should return "44.6%", not "44.5%"
x_formatted <- format(round2(as.double(x), digits = digits + 2) * 100,
2022-08-28 10:31:50 +02:00
scientific = FALSE,
digits = max(1, digits),
nsmall = digits,
...
)
2020-05-16 13:05:47 +02:00
x_formatted <- paste0(x_formatted, "%")
x_formatted[!grepl(pattern = "^[0-9.,e-]+$", x = x)] <- NA_character_
x_formatted
}
2020-10-26 12:23:03 +01:00
2020-05-16 13:05:47 +02:00
# the actual working part
x <- as.double(x)
if (is.null(digits)) {
# max one digit if undefined
digits <- getdecimalplaces(x, minimum = 0, maximum = 1)
}
2023-01-23 15:01:21 +01:00
format_percentage(
structure(
.Data = as.double(x),
class = c("percentage", "numeric")
),
digits = digits, ...
2022-08-28 10:31:50 +02:00
)
2020-05-16 13:05:47 +02:00
}
2020-05-18 13:59:34 +02:00
2023-01-14 19:50:25 +01:00
add_intrinsic_resistance_to_AMR_env <- function() {
# for mo_is_intrinsic_resistant() - saves a lot of time when executed on this vector
if (is.null(AMR_env$intrinsic_resistant)) {
AMR_env$intrinsic_resistant <- paste(AMR::intrinsic_resistant$mo, AMR::intrinsic_resistant$ab)
}
}
2023-01-21 23:47:20 +01:00
add_MO_lookup_to_AMR_env <- function() {
# for all MO functions, saves a lot of time on package load and in package size
if (is.null(AMR_env$MO_lookup)) {
MO_lookup <- AMR::microorganisms
2023-01-23 15:01:21 +01:00
2023-01-21 23:47:20 +01:00
MO_lookup$kingdom_index <- NA_real_
MO_lookup[which(MO_lookup$kingdom == "Bacteria" | MO_lookup$mo == "UNKNOWN"), "kingdom_index"] <- 1
MO_lookup[which(MO_lookup$kingdom == "Fungi"), "kingdom_index"] <- 1.25
MO_lookup[which(MO_lookup$kingdom == "Protozoa"), "kingdom_index"] <- 1.5
2024-09-30 18:46:55 +02:00
MO_lookup[which(MO_lookup$kingdom == "Chromista"), "kingdom_index"] <- 1.75
MO_lookup[which(MO_lookup$kingdom == "Archaea"), "kingdom_index"] <- 2
2023-01-21 23:47:20 +01:00
# all the rest
MO_lookup[which(is.na(MO_lookup$kingdom_index)), "kingdom_index"] <- 3
2023-01-23 15:01:21 +01:00
2023-01-21 23:47:20 +01:00
# the fullname lowercase, important for the internal algorithms in as.mo()
MO_lookup$fullname_lower <- tolower(trimws2(paste(
2023-01-21 23:47:20 +01:00
MO_lookup$genus,
MO_lookup$species,
MO_lookup$subspecies
)))
ind <- MO_lookup$genus == "" | grepl("^[(]unknown ", MO_lookup$fullname, perl = TRUE)
MO_lookup[ind, "fullname_lower"] <- tolower(MO_lookup[ind, "fullname", drop = TRUE])
MO_lookup$fullname_lower <- trimws2(gsub("[^.a-z0-9/ \\-]+", "", MO_lookup$fullname_lower, perl = TRUE))
2023-01-21 23:47:20 +01:00
# special for Salmonella - they have cities as subspecies but not the species (enterica) in the fullname:
MO_lookup$fullname_lower[which(MO_lookup$subspecies %like_case% "^[A-Z]")] <- gsub(" enterica ", " ", MO_lookup$fullname_lower[which(MO_lookup$subspecies %like_case% "^[A-Z]")], fixed = TRUE)
2023-01-23 15:01:21 +01:00
MO_lookup$genus_lower <- tolower(MO_lookup$genus)
2023-01-21 23:47:20 +01:00
MO_lookup$full_first <- substr(MO_lookup$fullname_lower, 1, 1)
MO_lookup$species_first <- tolower(substr(MO_lookup$species, 1, 1)) # tolower for groups (Streptococcus, Salmonella)
MO_lookup$subspecies_first <- tolower(substr(MO_lookup$subspecies, 1, 1)) # tolower for Salmonella serovars
AMR_env$MO_lookup <- MO_lookup
}
}
2022-10-05 09:12:22 +02:00
trimws2 <- function(..., whitespace = "[\u0009\u000A\u000B\u000C\u000D\u0020\u0085\u00A0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u200B\u200C\u200D\u2028\u2029\u202F\u205F\u2060\u3000\uFEFF]") {
# this is even faster than trimws() itself which sets "[ \t\r\n]".
2022-10-05 09:12:22 +02:00
trimws(..., whitespace = whitespace)
2020-05-18 13:59:34 +02:00
}
2022-10-05 09:12:22 +02:00
2023-06-22 15:24:18 +02:00
totitle <- function(x) {
gsub("^(.)", "\\U\\1", x, perl = TRUE)
}
2023-03-12 13:02:37 +01:00
readRDS_AMR <- function(file, refhook = NULL) {
2022-12-27 15:16:15 +01:00
# this is readRDS with remote file support
con <- file(file)
on.exit(close(con))
readRDS(con, refhook = refhook)
}
2022-10-05 09:12:22 +02:00
# Faster data.table implementations ----
match <- function(x, table, ...) {
if (!is.null(AMR_env$chmatch) && inherits(x, "character") && inherits(table, "character")) {
# data.table::chmatch() is much faster than base::match() for character
2023-12-03 01:06:00 +01:00
tryCatch(AMR_env$chmatch(x, table, ...), error = function(e) base::match(x, table, ...))
2022-10-05 09:12:22 +02:00
} else {
base::match(x, table, ...)
}
}
2022-10-05 09:12:22 +02:00
`%in%` <- function(x, table) {
if (!is.null(AMR_env$chin) && inherits(x, "character") && inherits(table, "character")) {
# data.table::`%chin%`() is much faster than base::`%in%`() for character
2023-12-03 01:06:00 +01:00
tryCatch(AMR_env$chin(x, table), error = function(e) base::`%in%`(x, table))
2022-10-05 09:12:22 +02:00
} else {
base::`%in%`(x, table)
}
2020-10-08 11:16:03 +02:00
}
2022-10-05 09:12:22 +02:00
# nolint start
# Register S3 methods ----
# copied from vctrs::s3_register by their permission:
# https://github.com/r-lib/vctrs/blob/05968ce8e669f73213e3e894b5f4424af4f46316/R/register-s3.R
s3_register <- function(generic, class, method = NULL) {
stopifnot(is.character(generic), length(generic) == 1)
stopifnot(is.character(class), length(class) == 1)
pieces <- strsplit(generic, "::")[[1]]
stopifnot(length(pieces) == 2)
package <- pieces[[1]]
generic <- pieces[[2]]
caller <- parent.frame()
get_method_env <- function() {
top <- topenv(caller)
if (isNamespace(top)) {
asNamespace(environmentName(top))
} else {
caller
}
}
get_method <- function(method, env) {
if (is.null(method)) {
get(paste0(generic, ".", class), envir = get_method_env())
} else {
method
}
}
method_fn <- get_method(method)
stopifnot(is.function(method_fn))
setHook(packageEvent(package, "onLoad"), function(...) {
ns <- asNamespace(package)
method_fn <- get_method(method)
registerS3method(generic, class, method_fn, envir = ns)
})
if (!isNamespaceLoaded(package)) {
return(invisible())
}
envir <- asNamespace(package)
if (exists(generic, envir)) {
registerS3method(generic, class, method_fn, envir = envir)
}
invisible()
2020-12-17 16:22:25 +01:00
}
2021-04-12 14:24:40 +02:00
2022-10-05 09:12:22 +02:00
# Support old R versions ----
# these functions were not available in previous versions of R
# see here for the full list: https://github.com/r-lib/backports
if (getRversion() < "3.1.0") {
2021-04-12 14:24:40 +02:00
# R-3.0 does not contain these functions, set them here to prevent installation failure
2022-10-19 11:47:57 +02:00
# (required for extension of the 'mic' class)
2021-04-12 14:24:40 +02:00
cospi <- function(...) 1
sinpi <- function(...) 1
tanpi <- function(...) 1
}
2022-10-05 09:12:22 +02:00
if (getRversion() < "3.2.0") {
anyNA <- function(x, recursive = FALSE) {
if (isTRUE(recursive) && (is.list(x) || is.pairlist(x))) {
return(any(rapply(x, anyNA, how = "unlist", recursive = FALSE)))
}
any(is.na(x))
}
dir.exists <- function(paths) {
x <- base::file.info(paths)$isdir
!is.na(x) & x
}
file.size <- function(...) {
file.info(...)$size
}
file.mtime <- function(...) {
file.info(...)$mtime
}
isNamespaceLoaded <- function(pkg) {
pkg %in% loadedNamespaces()
}
lengths <- function(x, use.names = TRUE) {
vapply(x, length, FUN.VALUE = NA_integer_, USE.NAMES = use.names)
}
}
if (getRversion() < "3.3.0") {
strrep <- function(x, times) {
x <- as.character(x)
if (length(x) == 0L) {
return(x)
}
unlist(.mapply(function(x, times) {
if (is.na(x) || is.na(times)) {
return(NA_character_)
}
if (times <= 0L) {
return("")
}
paste0(replicate(times, x), collapse = "")
}, list(x = x, times = times), MoreArgs = list()), use.names = FALSE)
}
}
if (getRversion() < "3.5.0") {
isFALSE <- function(x) {
is.logical(x) && length(x) == 1L && !is.na(x) && !x
}
}
if (getRversion() < "3.6.0") {
str2lang <- function(s) {
stopifnot(length(s) == 1L)
ex <- parse(text = s, keep.source = FALSE)
stopifnot(length(ex) == 1L)
ex[[1L]]
}
# trims() was introduced in 3.3.0, but its argument `whitespace` only in 3.6.0
trimws <- function(x, which = c("both", "left", "right"), whitespace = "[ \t\r\n]") {
which <- match.arg(which)
mysub <- function(re, x) sub(re, "", x, perl = TRUE)
switch(which,
left = mysub(paste0("^", whitespace, "+"), x),
right = mysub(paste0(whitespace, "+$"), x),
both = mysub(paste0(whitespace, "+$"), mysub(paste0("^", whitespace, "+"), x))
)
}
}
if (getRversion() < "4.0.0") {
deparse1 <- function(expr, collapse = " ", width.cutoff = 500L, ...) {
paste(deparse(expr, width.cutoff, ...), collapse = collapse)
}
2021-05-21 20:20:51 +02:00
}
2022-10-05 09:12:22 +02:00
# nolint end