2019-02-04 12:24:07 +01:00
# ==================================================================== #
# TITLE #
# Antimicrobial Resistance (AMR) Analysis #
# #
# SOURCE #
2020-07-08 14:48:06 +02:00
# https://github.com/msberends/AMR #
2019-02-04 12:24:07 +01:00
# #
# LICENCE #
2020-01-05 17:22:09 +01:00
# (c) 2018-2020 Berends MS, Luz CF et al. #
2019-02-04 12: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-07-08 14:48:06 +02:00
# Visit our website for more info: https://msberends.github.io/AMR. #
2019-02-04 12:24:07 +01:00
# ==================================================================== #
#' Check availability of columns
#'
2020-01-05 17:22:09 +01:00
#' Easy check for data availability of all columns in a data set. This makes it easy to get an idea of which antimicrobial combinations can be used for calculation with e.g. [susceptibility()] and [resistance()].
#' @inheritSection lifecycle Stable lifecycle
2020-09-18 16:05:53 +02:00
#' @param tbl a [data.frame] or [list]
2019-03-26 15:34:04 +01:00
#' @param width number of characters to present the visual availability, defaults to filling the width of the console
2020-09-18 16:05:53 +02:00
#' @details The function returns a [data.frame] with columns `"resistant"` and `"visual_resistance"`. The values in that columns are calculated with [resistance()].
#' @return [data.frame] with column names of `tbl` as row names
2019-02-21 18:55:52 +01:00
#' @inheritSection AMR Read more on our website!
2019-02-04 12:24:07 +01:00
#' @export
#' @examples
2019-08-27 16:45:42 +02:00
#' availability(example_isolates)
2019-02-04 12:24:07 +01:00
#'
2020-09-29 23:35:46 +02:00
#' if (require("dplyr")) {
#' example_isolates %>%
#' filter(mo == as.mo("E. coli")) %>%
#' select_if(is.rsi) %>%
#' availability()
2020-05-16 21:40:50 +02:00
#' }
2019-03-26 15:34:04 +01:00
availability <- function ( tbl , width = NULL ) {
2020-06-22 11:18:40 +02:00
stop_ifnot ( is.data.frame ( tbl ) , " `tbl` must be a data.frame" )
2020-09-03 12:31:48 +02:00
x <- sapply ( tbl , function ( x ) {
1 - sum ( is.na ( x ) ) / length ( x )
2019-10-11 17:21:02 +02:00
} )
2020-09-03 12:31:48 +02:00
n <- sapply ( tbl , function ( x ) length ( x [ ! is.na ( x ) ] ) )
R <- sapply ( tbl , function ( x ) ifelse ( is.rsi ( x ) , resistance ( x , minimum = 0 ) , NA ) )
2019-08-25 22:53:22 +02:00
R_print <- character ( length ( R ) )
2019-09-30 16:45:36 +02:00
R_print [ ! is.na ( R ) ] <- percentage ( R [ ! is.na ( R ) ] )
2019-08-25 22:53:22 +02:00
R_print [is.na ( R ) ] <- " "
2020-07-13 09:17:24 +02:00
2019-03-26 15:34:04 +01:00
if ( is.null ( width ) ) {
width <- options ( ) $ width -
( max ( nchar ( colnames ( tbl ) ) ) +
# count col
8 +
# available % column
10 +
# resistant % column
10 +
# extra margin
5 )
width <- width / 2
}
2020-07-13 09:17:24 +02:00
2019-08-25 22:53:22 +02:00
if ( length ( R [is.na ( R ) ] ) == ncol ( tbl ) ) {
2019-03-26 15:34:04 +01:00
width <- width * 2 + 10
}
2020-07-13 09:17:24 +02:00
2019-08-25 22:53:22 +02:00
x_chars_R <- strrep ( " #" , round ( width * R , digits = 2 ) )
x_chars_SI <- strrep ( " -" , width - nchar ( x_chars_R ) )
vis_resistance <- paste0 ( " |" , x_chars_R , x_chars_SI , " |" )
vis_resistance [is.na ( R ) ] <- " "
2020-07-13 09:17:24 +02:00
2019-03-26 15:34:04 +01:00
x_chars <- strrep ( " #" , round ( x , digits = 2 ) / ( 1 / width ) )
x_chars_empty <- strrep ( " -" , width - nchar ( x_chars ) )
2020-07-13 09:17:24 +02:00
2019-03-26 15:34:04 +01:00
df <- data.frame ( count = n ,
2019-09-30 16:45:36 +02:00
available = percentage ( x ) ,
2019-03-26 15:34:04 +01:00
visual_availabilty = paste0 ( " |" , x_chars , x_chars_empty , " |" ) ,
2019-08-25 22:53:22 +02:00
resistant = R_print ,
2019-03-26 15:34:04 +01:00
visual_resistance = vis_resistance )
2019-08-25 22:53:22 +02:00
if ( length ( R [is.na ( R ) ] ) == ncol ( tbl ) ) {
2019-10-11 17:21:02 +02:00
df [ , 1 : 3 ]
2019-03-26 15:34:04 +01:00
} else {
df
}
2019-02-04 12:24:07 +01:00
}