2018-02-21 11:52:31 +01:00
|
|
|
# ==================================================================== #
|
|
|
|
# TITLE #
|
|
|
|
# Antimicrobial Resistance (AMR) Analysis #
|
|
|
|
# #
|
2019-01-02 23:24:07 +01:00
|
|
|
# SOURCE #
|
|
|
|
# https://gitlab.com/msberends/AMR #
|
2018-02-21 11:52:31 +01:00
|
|
|
# #
|
|
|
|
# LICENCE #
|
2019-01-02 23:24:07 +01:00
|
|
|
# (c) 2019 Berends MS (m.s.berends@umcg.nl), Luz CF (c.f.luz@umcg.nl) #
|
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. #
|
|
|
|
# #
|
|
|
|
# This R package was created for academic research and was publicly #
|
|
|
|
# released in the hope that it will be useful, but it comes WITHOUT #
|
|
|
|
# ANY WARRANTY OR LIABILITY. #
|
2019-04-05 18:47:39 +02:00
|
|
|
# Visit our website for more info: https://msberends.gitlab.io/AMR. #
|
2018-02-21 11:52:31 +01:00
|
|
|
# ==================================================================== #
|
|
|
|
|
2018-07-04 17:20:03 +02:00
|
|
|
# No export, no Rd
|
|
|
|
addin_insert_in <- function() {
|
|
|
|
rstudioapi::insertText(" %in% ")
|
|
|
|
}
|
|
|
|
|
|
|
|
# No export, no Rd
|
|
|
|
addin_insert_like <- function() {
|
|
|
|
rstudioapi::insertText(" %like% ")
|
|
|
|
}
|
|
|
|
|
2019-03-15 13:57:25 +01:00
|
|
|
#' @importFrom crayon blue bold red
|
2019-01-15 12:45:24 +01:00
|
|
|
#' @importFrom dplyr %>% pull
|
2019-05-23 16:58:59 +02:00
|
|
|
search_type_in_df <- function(x, type) {
|
2019-01-15 12:45:24 +01:00
|
|
|
# try to find columns based on type
|
|
|
|
found <- NULL
|
|
|
|
|
2019-05-23 16:58:59 +02:00
|
|
|
colnames(x) <- trimws(colnames(x))
|
2019-01-29 00:06:50 +01:00
|
|
|
|
2019-01-15 12:45:24 +01:00
|
|
|
# -- mo
|
|
|
|
if (type == "mo") {
|
2019-05-23 16:58:59 +02:00
|
|
|
if ("mo" %in% lapply(x, class)) {
|
|
|
|
found <- colnames(x)[lapply(x, class) == "mo"][1]
|
2019-07-11 13:39:18 +02:00
|
|
|
} else if (any(colnames(x) %like% "^(mo|microorganism|organism|bacteria|bacterie)s?$")) {
|
|
|
|
found <- colnames(x)[colnames(x) %like% "^(mo|microorganism|organism|bacteria|bacterie)s?$"][1]
|
|
|
|
} else if (any(colnames(x) %like% "^(microorganism|organism|bacteria|bacterie)")) {
|
|
|
|
found <- colnames(x)[colnames(x) %like% "^(microorganism|organism|bacteria|bacterie)"][1]
|
2019-05-23 16:58:59 +02:00
|
|
|
} else if (any(colnames(x) %like% "species")) {
|
|
|
|
found <- colnames(x)[colnames(x) %like% "species"][1]
|
2019-01-15 12:45:24 +01:00
|
|
|
}
|
2019-01-29 00:06:50 +01:00
|
|
|
|
2019-01-15 12:45:24 +01:00
|
|
|
}
|
|
|
|
# -- key antibiotics
|
|
|
|
if (type == "keyantibiotics") {
|
2019-05-23 16:58:59 +02:00
|
|
|
if (any(colnames(x) %like% "^key.*(ab|antibiotics)")) {
|
|
|
|
found <- colnames(x)[colnames(x) %like% "^key.*(ab|antibiotics)"][1]
|
2019-01-15 12:45:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
# -- date
|
|
|
|
if (type == "date") {
|
2019-05-23 16:58:59 +02:00
|
|
|
if (any(colnames(x) %like% "^(specimen date|specimen_date|spec_date)")) {
|
2019-03-15 13:57:25 +01:00
|
|
|
# WHONET support
|
2019-05-23 16:58:59 +02:00
|
|
|
found <- colnames(x)[colnames(x) %like% "^(specimen date|specimen_date|spec_date)"][1]
|
|
|
|
if (!any(class(x %>% pull(found)) %in% c("Date", "POSIXct"))) {
|
2019-03-15 13:57:25 +01:00
|
|
|
stop(red(paste0("ERROR: Found column `", bold(found), "` to be used as input for `col_", type,
|
|
|
|
"`, but this column contains no valid dates. Transform its values to valid dates first.")),
|
|
|
|
call. = FALSE)
|
|
|
|
}
|
|
|
|
} else {
|
2019-05-23 16:58:59 +02:00
|
|
|
for (i in 1:ncol(x)) {
|
|
|
|
if (any(class(x %>% pull(i)) %in% c("Date", "POSIXct"))) {
|
|
|
|
found <- colnames(x)[i]
|
2019-03-15 13:57:25 +01:00
|
|
|
break
|
|
|
|
}
|
2019-01-15 12:45:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# -- patient id
|
|
|
|
if (type == "patient_id") {
|
2019-05-23 16:58:59 +02:00
|
|
|
if (any(colnames(x) %like% "^(identification |patient|patid)")) {
|
|
|
|
found <- colnames(x)[colnames(x) %like% "^(identification |patient|patid)"][1]
|
2019-01-29 00:06:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
# -- specimen
|
|
|
|
if (type == "specimen") {
|
2019-05-23 16:58:59 +02:00
|
|
|
if (any(colnames(x) %like% "(specimen type|spec_type)")) {
|
|
|
|
found <- colnames(x)[colnames(x) %like% "(specimen type|spec_type)"][1]
|
|
|
|
} else if (any(colnames(x) %like% "^(specimen)")) {
|
|
|
|
found <- colnames(x)[colnames(x) %like% "^(specimen)"][1]
|
2019-01-15 12:45:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is.null(found)) {
|
|
|
|
msg <- paste0("NOTE: Using column `", bold(found), "` as input for `col_", type, "`.")
|
2019-01-29 00:06:50 +01:00
|
|
|
if (type %in% c("keyantibiotics", "specimen")) {
|
2019-05-17 20:22:04 +02:00
|
|
|
msg <- paste(msg, "Use", bold(paste0("col_", type), "= FALSE"), "to prevent this.")
|
2019-01-15 12:45:24 +01:00
|
|
|
}
|
|
|
|
message(blue(msg))
|
|
|
|
}
|
|
|
|
found
|
|
|
|
}
|
2019-03-26 14:24:03 +01:00
|
|
|
|
|
|
|
stopifnot_installed_package <- function(package) {
|
2019-06-01 20:40:49 +02:00
|
|
|
# no "utils::installed.packages()" since it requires non-staged install since R 3.6.0
|
|
|
|
# https://developer.r-project.org/Blog/public/2019/02/14/staged-install/index.html
|
|
|
|
get(".packageName", envir = asNamespace(package))
|
|
|
|
return(invisible())
|
2019-03-26 14:24:03 +01:00
|
|
|
}
|
2019-05-10 16:44:59 +02:00
|
|
|
|
2019-05-20 19:12:41 +02:00
|
|
|
"%or%" <- function(x, y) {
|
2019-06-16 21:42:40 +02:00
|
|
|
if (is.null(x) | is.null(y)) {
|
|
|
|
if (is.null(x)) {
|
|
|
|
return(y)
|
|
|
|
} else {
|
|
|
|
return(x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ifelse(!is.na(x),
|
|
|
|
x,
|
|
|
|
ifelse(!is.na(y), y, NA))
|
2019-05-20 19:12:41 +02:00
|
|
|
}
|
2019-08-26 16:02:03 +02:00
|
|
|
|
|
|
|
class_integrity_check <- function(value, type, check_vector) {
|
|
|
|
if (!all(value[!is.na(value)] %in% check_vector)) {
|
|
|
|
warning(paste0("invalid ", type, ", NA generated"), call. = FALSE)
|
|
|
|
value[!value %in% check_vector] <- NA
|
|
|
|
}
|
|
|
|
value
|
|
|
|
}
|
2019-10-04 15:36:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Percentages -------------------------------------------------------------
|
|
|
|
# Can all be removed when clean 1.2.0 is on CRAN
|
|
|
|
|
|
|
|
getdecimalplaces <- function(x, minimum = 0, maximum = 3) {
|
|
|
|
if (maximum < minimum) {
|
|
|
|
maximum <- minimum
|
|
|
|
}
|
|
|
|
if (minimum > maximum) {
|
|
|
|
minimum <- maximum
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
max(min(max_places,
|
|
|
|
maximum, na.rm = TRUE),
|
|
|
|
minimum, na.rm = TRUE)
|
|
|
|
}
|
|
|
|
|
|
|
|
round2 <- function(x, digits = 0, force_zero = TRUE) {
|
|
|
|
# https://stackoverflow.com/a/12688836/4575331
|
|
|
|
val <- (trunc((abs(x) * 10 ^ digits) + 0.5) / 10 ^ digits) * sign(x)
|
|
|
|
if (digits > 0 & force_zero == TRUE) {
|
2019-10-06 21:07:38 +02:00
|
|
|
val[val != as.integer(val) & !is.na(val)] <- paste0(val[val != as.integer(val) & !is.na(val)],
|
|
|
|
strrep("0", max(0, digits - nchar(gsub(".*[.](.*)$", "\\1", val[val != as.integer(val) & !is.na(val)])))))
|
2019-10-04 15:36:12 +02:00
|
|
|
}
|
|
|
|
val
|
|
|
|
}
|
|
|
|
|
|
|
|
percentage <- function(x, digits = NULL, ...) {
|
|
|
|
if (is.null(digits)) {
|
|
|
|
digits <- getdecimalplaces(x, minimum = 0, maximum = 1)
|
|
|
|
}
|
|
|
|
# round right: percentage(0.4455) should return "44.6%", not "44.5%"
|
|
|
|
x <- as.numeric(round2(x, digits = digits + 2))
|
|
|
|
x_formatted <- format(as.double(x) * 100, scientific = FALSE, digits = digits, nsmall = digits, ...)
|
|
|
|
x_formatted[!is.na(x)] <- paste0(x_formatted[!is.na(x)], "%")
|
|
|
|
x_formatted
|
|
|
|
}
|