2019-05-10 16:44:59 +02:00
|
|
|
# ==================================================================== #
|
|
|
|
# TITLE #
|
|
|
|
# Antimicrobial Resistance (AMR) Analysis #
|
|
|
|
# #
|
|
|
|
# SOURCE #
|
|
|
|
# https://gitlab.com/msberends/AMR #
|
|
|
|
# #
|
|
|
|
# LICENCE #
|
2020-01-05 17:22:09 +01:00
|
|
|
# (c) 2018-2020 Berends MS, Luz CF et al. #
|
2019-05-10 16:44:59 +02: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. #
|
2019-05-10 16:44:59 +02:00
|
|
|
# Visit our website for more info: https://msberends.gitlab.io/AMR. #
|
|
|
|
# ==================================================================== #
|
|
|
|
|
|
|
|
#' Class 'disk'
|
|
|
|
#'
|
2019-12-21 10:56:06 +01:00
|
|
|
#' This transforms a vector to a new class [`disk`], which is a growth zone size (around an antibiotic disk) in millimeters between 6 and 50.
|
2020-01-05 17:22:09 +01:00
|
|
|
#' @inheritSection lifecycle Stable lifecycle
|
2019-05-10 16:44:59 +02:00
|
|
|
#' @rdname as.disk
|
|
|
|
#' @param x vector
|
|
|
|
#' @param na.rm a logical indicating whether missing values should be removed
|
2019-11-28 22:32:17 +01:00
|
|
|
#' @details Interpret disk values as RSI values with [as.rsi()]. It supports guidelines from EUCAST and CLSI.
|
2019-12-21 10:56:06 +01:00
|
|
|
#' @return An [`integer`] with additional new class [`disk`]
|
2019-11-06 14:43:23 +01:00
|
|
|
#' @aliases disk
|
2019-05-10 16:44:59 +02:00
|
|
|
#' @export
|
2019-11-28 22:32:17 +01:00
|
|
|
#' @seealso [as.rsi()]
|
2019-05-10 16:44:59 +02:00
|
|
|
#' @inheritSection AMR Read more on our website!
|
|
|
|
#' @examples
|
|
|
|
#' # interpret disk values
|
|
|
|
#' as.rsi(x = 12,
|
|
|
|
#' mo = as.mo("S. pneumoniae"),
|
|
|
|
#' ab = "AMX",
|
|
|
|
#' guideline = "EUCAST")
|
|
|
|
#' as.rsi(x = 12,
|
|
|
|
#' mo = as.mo("S. pneumoniae"),
|
|
|
|
#' ab = "AMX",
|
|
|
|
#' guideline = "CLSI")
|
|
|
|
as.disk <- function(x, na.rm = FALSE) {
|
|
|
|
if (is.disk(x)) {
|
|
|
|
x
|
|
|
|
} else {
|
|
|
|
x <- x %>% unlist()
|
|
|
|
if (na.rm == TRUE) {
|
|
|
|
x <- x[!is.na(x)]
|
|
|
|
}
|
|
|
|
x.bak <- x
|
|
|
|
|
|
|
|
na_before <- length(x[is.na(x)])
|
|
|
|
|
|
|
|
# force it to be integer
|
|
|
|
x <- suppressWarnings(as.integer(x))
|
|
|
|
|
2019-12-21 10:56:06 +01:00
|
|
|
# disks can never be less than 6 mm (size of smallest disk) or more than 50 mm
|
|
|
|
x[x < 6 | x > 50] <- NA_integer_
|
2019-05-10 16:44:59 +02:00
|
|
|
na_after <- length(x[is.na(x)])
|
|
|
|
|
|
|
|
if (na_before != na_after) {
|
|
|
|
list_missing <- x.bak[is.na(x) & !is.na(x.bak)] %>%
|
|
|
|
unique() %>%
|
|
|
|
sort()
|
2019-10-11 17:21:02 +02:00
|
|
|
list_missing <- paste0('"', list_missing, '"', collapse = ", ")
|
|
|
|
warning(na_after - na_before, " results truncated (",
|
2019-05-10 16:44:59 +02:00
|
|
|
round(((na_after - na_before) / length(x)) * 100),
|
2019-10-11 17:21:02 +02:00
|
|
|
"%) that were invalid disk zones: ",
|
2019-05-10 16:44:59 +02:00
|
|
|
list_missing, call. = FALSE)
|
|
|
|
}
|
|
|
|
|
2019-10-11 17:21:02 +02:00
|
|
|
class(x) <- c("disk", "integer")
|
2019-05-10 16:44:59 +02:00
|
|
|
x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#' @rdname as.disk
|
|
|
|
#' @export
|
|
|
|
#' @importFrom dplyr %>%
|
|
|
|
is.disk <- function(x) {
|
2019-10-11 17:21:02 +02:00
|
|
|
class(x) %>% identical(c("disk", "integer"))
|
2019-05-10 16:44:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#' @exportMethod print.disk
|
|
|
|
#' @export
|
|
|
|
#' @noRd
|
|
|
|
print.disk <- function(x, ...) {
|
|
|
|
cat("Class 'disk'\n")
|
|
|
|
print(as.integer(x), quote = FALSE)
|
|
|
|
}
|
2019-08-12 14:48:09 +02:00
|
|
|
|
|
|
|
#' @importFrom pillar type_sum
|
|
|
|
#' @export
|
|
|
|
type_sum.disk <- function(x) {
|
|
|
|
"disk"
|
|
|
|
}
|
|
|
|
|
|
|
|
#' @importFrom pillar pillar_shaft
|
|
|
|
#' @export
|
|
|
|
pillar_shaft.disk <- function(x, ...) {
|
|
|
|
out <- trimws(format(x))
|
|
|
|
out[is.na(x)] <- pillar::style_na(NA)
|
|
|
|
pillar::new_pillar_shaft_simple(out, align = "right", min_width = 3)
|
|
|
|
}
|