2018-08-28 13:51:13 +02:00
# ==================================================================== #
# TITLE #
# Antimicrobial Resistance (AMR) Analysis #
# #
# AUTHORS #
# Berends MS (m.s.berends@umcg.nl), Luz CF (c.f.luz@umcg.nl) #
# #
# LICENCE #
2018-12-16 22:45:12 +01:00
# This package is free software; you can redistribute it and/or modify #
2018-08-28 13:51:13 +02:00
# it under the terms of the GNU General Public License version 2.0, #
# as published by the Free Software Foundation. #
# #
2018-12-16 22:45:12 +01:00
# This R package is distributed in the hope that it will be useful, #
2018-08-28 13:51:13 +02:00
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
2018-12-16 22:45:12 +01:00
# GNU General Public License version 2.0 for more details. #
2018-08-28 13:51:13 +02:00
# ==================================================================== #
#' Property of an antibiotic
#'
#' Use these functions to return a specific property of an antibiotic from the \code{\link{antibiotics}} data set, based on their ATC code. Get such a code with \code{\link{as.atc}}.
#' @param x a (vector of a) valid \code{\link{atc}} code or any text that can be coerced to a valid atc with \code{\link{as.atc}}
#' @param property one of the column names of one of the \code{\link{antibiotics}} data set, like \code{"atc"} and \code{"official"}
2018-09-25 16:44:40 +02:00
#' @param language language of the returned text, defaults to English (\code{"en"}) and can be set with \code{\link{getOption}("AMR_locale")}. Either one of \code{"en"} (English) or \code{"nl"} (Dutch).
2018-08-28 13:51:13 +02:00
#' @rdname ab_property
2018-08-29 12:27:37 +02:00
#' @return A vector of values. In case of \code{ab_tradenames}, if \code{x} is of length one, a vector will be returned. Otherwise a \code{\link{list}}, with \code{x} as names.
2018-08-28 13:51:13 +02:00
#' @export
#' @importFrom dplyr %>% left_join pull
#' @seealso \code{\link{antibiotics}}
#' @examples
#' ab_atc("amcl") # J01CR02
2018-09-16 16:43:29 +02:00
#' ab_name("amcl") # Amoxicillin and beta-lactamase inhibitor
#' ab_name("amcl", "nl") # Amoxicilline met enzymremmer
2018-08-28 13:51:13 +02:00
#' ab_trivial_nl("amcl") # Amoxicilline/clavulaanzuur
#' ab_certe("amcl") # amcl
#' ab_umcg("amcl") # AMCL
ab_property <- function ( x , property = ' official' ) {
property <- property [1 ]
2018-09-16 22:11:17 +02:00
if ( ! property %in% colnames ( AMR :: antibiotics ) ) {
2018-09-04 11:33:30 +02:00
stop ( " invalid property: " , property , " - use a column name of the `antibiotics` data set" )
2018-08-28 13:51:13 +02:00
}
if ( ! is.atc ( x ) ) {
x <- as.atc ( x ) # this will give a warning if x cannot be coerced
}
suppressWarnings (
data.frame ( atc = x , stringsAsFactors = FALSE ) %>%
left_join ( AMR :: antibiotics , by = " atc" ) %>%
pull ( property )
)
}
#' @rdname ab_property
#' @export
ab_atc <- function ( x ) {
as.character ( as.atc ( x ) )
}
#' @rdname ab_property
#' @export
2018-09-16 16:43:29 +02:00
ab_official <- function ( x , language = NULL ) {
if ( is.null ( language ) ) {
2018-09-25 16:44:40 +02:00
language <- getOption ( " AMR_locale" , default = " en" ) [1L ]
2018-09-16 16:43:29 +02:00
} else {
language <- tolower ( language [1 ] )
}
if ( language %in% c ( " en" , " " ) ) {
ab_property ( x , " official" )
} else if ( language == " nl" ) {
ab_property ( x , " official_nl" )
} else {
stop ( " Unsupported language: '" , language , " ' - use one of: 'en', 'nl'" , call. = FALSE )
}
2018-08-28 13:51:13 +02:00
}
#' @rdname ab_property
#' @export
2018-09-16 16:43:29 +02:00
ab_name <- ab_official
2018-08-28 13:51:13 +02:00
#' @rdname ab_property
#' @export
ab_trivial_nl <- function ( x ) {
ab_property ( x , " trivial_nl" )
}
#' @rdname ab_property
#' @export
ab_certe <- function ( x ) {
ab_property ( x , " certe" )
}
#' @rdname ab_property
#' @export
ab_umcg <- function ( x ) {
ab_property ( x , " umcg" )
}
2018-08-29 12:27:37 +02:00
#' @rdname ab_property
#' @export
ab_tradenames <- function ( x ) {
res <- ab_property ( x , " trade_name" )
res <- strsplit ( res , " |" , fixed = TRUE )
if ( length ( x ) == 1 ) {
res <- unlist ( res )
} else {
names ( res ) <- x
}
res
}