diff --git a/DESCRIPTION b/DESCRIPTION index 50595307..50d7a2f2 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: AMR -Version: 0.1.0 -Date: 2018-02-20 -Title: Antimicrobial Resistance (AMR) Analysis +Version: 0.1.1 +Date: 2018-02-22 +Title: Antimicrobial Resistance Analysis Authors@R: c( person( given = c("Matthijs", "S."), @@ -19,8 +19,9 @@ Authors@R: c( email = "e.hassing@certe.nl", role = "ctb")) Description: Functions to simplify the analysis of Antimicrobial Resistance (AMR) - of microbial isolates, by using new S3 classes and applying EUCAST expert rules - on antibiograms. + of microbial isolates, by using new S3 classes and applying EUCAST expert rules + on antibiograms according to Leclercq (2013) + . Depends: R (>= 3.0) Imports: dplyr (>= 0.7.0), reshape2 (>= 1.4.0), xml2, rvest URL: https://github.com/msberends/AMR diff --git a/NAMESPACE b/NAMESPACE index 01ea53e4..831b7089 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -21,7 +21,6 @@ export(interpretive_reading) export(is.mic) export(is.rsi) export(key_antibiotics) -export(key_antibiotics_equal) export(left_join_bactlist) export(mo_property) export(right_join_bactlist) diff --git a/R/EUCAST.R b/R/EUCAST.R index 2e6a80b2..a9f50585 100644 --- a/R/EUCAST.R +++ b/R/EUCAST.R @@ -28,6 +28,7 @@ #' @rdname EUCAST #' @export #' @importFrom dplyr %>% left_join select +#' @return table with edited variables of antibiotics. #' @source #' EUCAST Expert Rules Version 2.0: \cr #' Leclercq et al. \strong{EUCAST expert rules in antimicrobial susceptibility testing.} \emph{Clin Microbiol Infect.} 2013;19(2):141-60. \cr @@ -37,7 +38,7 @@ #' \url{http://www.eucast.org/expert_rules_and_intrinsic_resistance} #' @examples #' \dontrun{ -#' tbl <- interpretive_reading(tbl) +#' tbl <- EUCAST_rules(tbl) #' } EUCAST_rules <- function(tbl, col_bactcode = 'bacteriecode', diff --git a/R/atc.R b/R/atc.R index c45da077..3bf2c11d 100644 --- a/R/atc.R +++ b/R/atc.R @@ -18,7 +18,7 @@ #' Properties of an ATC code #' -#' Gets data from the WHO to determine properties of an ATC of e.g. an antibiotic. +#' Gets data from the WHO to determine properties of an ATC of e.g. an antibiotic. \strong{This function requires an internet connection.} #' @param atc_code a character or character vector with ATC code(s) of antibiotic(s) #' @param property property of an ATC code. Valid values are \code{"ATC code"}, \code{"Name"}, \code{"DDD"}, \code{"U"} (\code{"unit"}), \code{"Adm.R"} en \code{"Note"}. #' @param administration type of administration, see \emph{Details} @@ -54,6 +54,11 @@ #' @importFrom xml2 read_html #' @importFrom rvest html_nodes html_table #' @source \url{https://www.whocc.no/atc_ddd_alterations__cumulative/ddd_alterations/abbrevations/} +#' @examples +#' \donttest{ +#' atc_property("J01CA04", "DDD", "O") # oral DDD of amoxicillin +#' atc_property("J01CA04", "DDD", "P") # parenteral DDD of amoxicillin +#' } atc_property <- function(atc_code, property, administration = 'O', @@ -128,6 +133,7 @@ atc_property <- function(atc_code, #' @param textbetween text to put between multiple returned texts #' @param tolower return output as lower case with function \code{\link{tolower}}. #' @keywords ab antibiotics +#' @source \code{\link{ablist}} #' @export #' @importFrom dplyr %>% filter select slice #' @examples @@ -148,8 +154,6 @@ atc_property <- function(atc_code, #' #' abname("J01CR02", from = "atc", to = "umcg") #' # "AMCL" -#' -#' @source \code{\link{ablist}} abname <- function(abcode, from = 'umcg', to = 'official', textbetween = ' + ', tolower = FALSE) { ablist <- AMR::ablist diff --git a/R/classes.R b/R/classes.R index d6cb42fd..288df563 100644 --- a/R/classes.R +++ b/R/classes.R @@ -26,8 +26,16 @@ #' @importFrom dplyr %>% #' @examples #' rsi_data <- as.rsi(c(rep("S", 474), rep("I", 36), rep("R", 370))) -#' #' rsi_data <- as.rsi(c(rep("S", 474), rep("I", 36), rep("R", 370), "A", "B", "C")) +#' is.rsi(rsi_data) +#' plot(rsi_data) +#' +#' \donttest{ +#' library(dplyr) +#' tbl %>% +#' mutate_at(vars(ends_with("_rsi")), as.rsi) +#' sapply(mic_data, is.rsi) +#' } as.rsi <- function(x) { if (is.rsi(x)) { x @@ -157,6 +165,17 @@ plot.rsi <- function(x, ...) { #' @return New class \code{mic} #' @export #' @importFrom dplyr %>% +#' @examples +#' mic_data <- as.mic(c(">=32", "1.0", "1", "1.00", 8, "<=0.128", "8", "16", "16")) +#' is.mic(mic_data) +#' plot(mic_data) +#' +#' \donttest{ +#' library(dplyr) +#' tbl %>% +#' mutate_at(vars(ends_with("_mic")), as.mic) +#' sapply(mic_data, is.mic) +#' } as.mic <- function(x, na.rm = FALSE) { if (is.mic(x)) { x diff --git a/R/first_isolates.R b/R/first_isolates.R index 94589c79..2b70fbd4 100644 --- a/R/first_isolates.R +++ b/R/first_isolates.R @@ -46,6 +46,7 @@ #' @examples #' \dontrun{ #' +#' # set key antibiotics to a new variable #' tbl$keyab <- key_antibiotics(tbl) #' #' tbl$first_isolate <- @@ -355,7 +356,13 @@ first_isolate <- function(tbl, #' @param amcl,amox,cfot,cfta,cftr,cfur,cipr,clar,clin,clox,doxy,gent,line,mero,peni,pita,rifa,teic,trsu,vanc column names of antibiotics. #' @export #' @importFrom dplyr %>% mutate if_else +#' @return Character of length 1. #' @seealso \code{\link{mo_property}} \code{\link{ablist}} +#' @examples +#' \donttest{ +#' #' # set key antibiotics to a new variable +#' tbl$keyab <- key_antibiotics(tbl) +#' } key_antibiotics <- function(tbl, col_bactcode = 'bacteriecode', info = TRUE, @@ -439,15 +446,17 @@ key_antibiotics <- function(tbl, } -#' Compare key antibiotics -#' -#' Check whether two text values with key antibiotics match. Supports vectors. -#' @param x,y tekst (or multiple text vectors) with antimicrobial interpretations -#' @param ignore_I ignore \code{"I"} as antimicrobial interpretation of key antibiotics (with \code{FALSE}, changes in antibiograms from S to I and I to R will be interpreted as difference) -#' @param info print progress -#' @return logical -#' @export -#' @seealso \code{\link{key_antibiotics}} +# Compare key antibiotics +# +# Check whether two text values with key antibiotics match. Supports vectors. +# @param x,y tekst (or multiple text vectors) with antimicrobial interpretations +# @param ignore_I ignore \code{"I"} as antimicrobial interpretation of key antibiotics (with \code{FALSE}, changes in antibiograms from S to I and I to R will be interpreted as difference) +# @param info print progress +# @return logical +# @export +# @seealso \code{\link{key_antibiotics}} + +# only internal use key_antibiotics_equal <- function(x, y, ignore_I = TRUE, info = FALSE) { if (length(x) != length(y)) { stop('Length of `x` and `y` must be equal.') diff --git a/R/join.R b/R/join.R index 2035f18f..08838e79 100644 --- a/R/join.R +++ b/R/join.R @@ -1,4 +1,4 @@ -#' Join van tabel en \code{bactlist} +#' Join a table with \code{bactlist} #' #' Join the list of microorganisms \code{\link{bactlist}} easily to an existing table. #' @rdname join @@ -9,6 +9,17 @@ #' @param ... other parameters to pass trhough to \code{dplyr::\link[dplyr]{join}}. #' @details As opposed to the \code{\link[dplyr]{join}} functions of \code{dplyr}, at default existing columns will get a suffix \code{"2"} and the newly joined columns will not get a suffix. See \code{\link[dplyr]{join}} for more information. #' @export +#' @examples +#' df <- data.frame(date = seq(from = as.Date("2018-01-01"), +#' to = as.Date("2018-01-07"), +#' by = 1), +#' bacteria_id = c("STAAUR", "STAAUR", "STAAUR", "STAAUR", +#' "ESCCOL", "ESCCOL", "ESCCOL"), +#' stringsAsFactors = FALSE) +#' +#' colnames(df) +#' df2 <- left_join_bactlist(df, "bacteria_id") +#' colnames(df2) inner_join_bactlist <- function(x, by = 'bactid', ...) { # no name set to `by` parameter if (is.null(names(by))) { diff --git a/R/rsi_analysis.R b/R/rsi_analysis.R index de969b23..e241a7d8 100644 --- a/R/rsi_analysis.R +++ b/R/rsi_analysis.R @@ -167,7 +167,7 @@ rsi_df <- function(tbl, #' Resistance of isolates #' -#' This function can be used in \code{\link[dplyr]{summarise}}, see \emph{Examples}. CaBerekent het percentage S, SI, I, IR of R van een lijst isolaten. +#' This function can be used in \code{dplyr}s \code{\link[dplyr]{summarise}}, see \emph{Examples}. Calculate the percentage S, SI, I, IR or R of a vector of isolates. #' @param ab1,ab2 list with interpretations of an antibiotic #' @inheritParams rsi_df #' @details This function uses the \code{\link{rsi_df}} function internally. @@ -177,20 +177,19 @@ rsi_df <- function(tbl, #' @examples #' \dontrun{ #' tbl %>% +#' group_by(hospital) %>% +#' summarise(cipr = rsi(cipr)) +#' +#' tbl %>% #' group_by(year, hospital) %>% #' summarise( #' isolates = n(), -#' cipro = rsi(cipr, percent = TRUE), -#' amoxi = rsi(amox, percent = TRUE) -#' ) +#' cipro = rsi(cipr %>% as.rsi(), percent = TRUE), +#' amoxi = rsi(amox %>% as.rsi(), percent = TRUE)) +#' +#' rsi(as.rsi(isolates$amox)) #' -#' tbl %>% -#' group_by(hospital) %>% -#' summarise(cipr = rsi(cipr)) -#' -#' rsi(isolates$amox) -#' -#' rsi(isolates$amcl, interpretation = "S") +#' rsi(as.rsi(isolates$amcl), interpretation = "S") #' } rsi <- function(ab1, ab2 = NA, interpretation = 'IR', minimum = 30, percent = FALSE, info = FALSE, warning = FALSE) { functietekst <- as.character(match.call()) @@ -258,6 +257,7 @@ rsi <- function(ab1, ab2 = NA, interpretation = 'IR', minimum = 30, percent = FA #' rsi_predict(tbl[which(first_isolate == TRUE & genus == "Haemophilus"),], "amcl") #' #' # or with dplyr so you can actually read it: +#' library(dplyr) #' tbl %>% #' filter(first_isolate == TRUE, #' genus == "Haemophilus") %>% diff --git a/README.md b/README.md index 550cb66a..577a3ff8 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,26 @@ AMR can also be predicted for the forthcoming years with the `rsi_predict` funct It also contains functions to translate antibiotic codes from the lab (like `"AMOX"`) or the [WHO](https://www.whocc.no/atc_ddd_index/?code=J01CA04&showdescription=no) (like `"J01CA04"`) to trivial names (like `"amoxicillin"`) and vice versa. +## How to get it? +[![CRAN_Badge](http://www.r-pkg.org/badges/version/AMR)](http://cran.r-project.org/package=AMR) + +This package is available on CRAN (latest stable version) and also here on GitHub (latest development version). + +#### Latest stable version from CRAN (recommended) +RStudio: +- Click on `Tools` and then `Install Packages..` +- Type in `AMR` and press Install + +Other: +```r +install.packages("AMR") +``` + +#### Latest development version from GitHub +```r +devtools::install_github("msberends/AMR") +``` + ## How to use it? ```r # Call it with: @@ -95,28 +115,6 @@ abname(...) abname("J01CR02", from = "atc", to = "umcg") # "AMCL" ``` -## How to get it? -This package is only available here on GitHub, but respects the [CRAN Repository Policy](https://cran.r-project.org/web/packages/policies.html). - -*Installation commands:* -```r -library(devtools) -install_github("msberends/AMR") -``` - -*Working behind a proxy? Then use:* -```r -library(httr) -library(devtools) -set_config(use_proxy("yourproxydomain.com", - 8080, - "username", - "password", - "any")) # change "any" to "basic" or "digest" if needed -install_github("msberends/AMR") -reset_config() -``` - ## Authors - [Berends MS](https://github.com/msberends)1,2, PhD Student diff --git a/man/EUCAST.Rd b/man/EUCAST.Rd index 69d713ea..020bda9e 100644 --- a/man/EUCAST.Rd +++ b/man/EUCAST.Rd @@ -44,11 +44,14 @@ interpretive_reading(...) \item{...}{parameters that are passed on to \code{EUCAST_rules}} } +\value{ +table with edited variables of antibiotics. +} \description{ Apply expert rules (like intrinsic resistance), as defined by the European Committee on Antimicrobial Susceptibility Testing (EUCAST, \url{http://eucast.org}), see \emph{Source}. } \examples{ \dontrun{ -tbl <- interpretive_reading(tbl) +tbl <- EUCAST_rules(tbl) } } diff --git a/man/abname.Rd b/man/abname.Rd index 4f253ff8..59c44adc 100644 --- a/man/abname.Rd +++ b/man/abname.Rd @@ -40,7 +40,6 @@ abname("AMCL", to = "atc") abname("J01CR02", from = "atc", to = "umcg") # "AMCL" - } \keyword{ab} \keyword{antibiotics} diff --git a/man/as.mic.Rd b/man/as.mic.Rd index 1e7bff7f..c524fdca 100644 --- a/man/as.mic.Rd +++ b/man/as.mic.Rd @@ -20,3 +20,15 @@ New class \code{mic} \description{ This transforms a vector to a new class\code{mic}, which is an ordered factor valid MIC values as levels. Invalid MIC values will be translated as \code{NA} with a warning. } +\examples{ +mic_data <- as.mic(c(">=32", "1.0", "1", "1.00", 8, "<=0.128", "8", "16", "16")) +is.mic(mic_data) +plot(mic_data) + +\donttest{ +library(dplyr) +tbl \%>\% + mutate_at(vars(ends_with("_mic")), as.mic) +sapply(mic_data, is.mic) +} +} diff --git a/man/as.rsi.Rd b/man/as.rsi.Rd index 4d7d2947..64ff680b 100644 --- a/man/as.rsi.Rd +++ b/man/as.rsi.Rd @@ -20,6 +20,14 @@ This transforms a vector to a new class \code{rsi}, which is an ordered factor w } \examples{ rsi_data <- as.rsi(c(rep("S", 474), rep("I", 36), rep("R", 370))) - rsi_data <- as.rsi(c(rep("S", 474), rep("I", 36), rep("R", 370), "A", "B", "C")) +is.rsi(rsi_data) +plot(rsi_data) + +\donttest{ +library(dplyr) +tbl \%>\% + mutate_at(vars(ends_with("_rsi")), as.rsi) +sapply(mic_data, is.rsi) +} } diff --git a/man/atc_property.Rd b/man/atc_property.Rd index 8328de22..edd82fbe 100644 --- a/man/atc_property.Rd +++ b/man/atc_property.Rd @@ -20,7 +20,7 @@ atc_property(atc_code, property, administration = "O", \item{url}{url of website of the WHO. The sign \code{\%s} can be used as a placeholder for ATC codes.} } \description{ -Gets data from the WHO to determine properties of an ATC of e.g. an antibiotic. +Gets data from the WHO to determine properties of an ATC of e.g. an antibiotic. \strong{This function requires an internet connection.} } \details{ Abbreviations for the property \code{"Adm.R"} (parameter \code{administration}): @@ -49,3 +49,9 @@ Abbreviations for the property \code{"U"} (unit): \item{\code{"ml"}}{ = milliliter (e.g. eyedrops)} } } +\examples{ +\donttest{ +atc_property("J01CA04", "DDD", "O") # oral DDD of amoxicillin +atc_property("J01CA04", "DDD", "P") # parenteral DDD of amoxicillin +} +} diff --git a/man/first_isolate.Rd b/man/first_isolate.Rd index 649ff66f..0ea54ef8 100644 --- a/man/first_isolate.Rd +++ b/man/first_isolate.Rd @@ -57,6 +57,7 @@ To conduct an analysis of antimicrobial resistance, you should only include the \examples{ \dontrun{ +# set key antibiotics to a new variable tbl$keyab <- key_antibiotics(tbl) tbl$first_isolate <- diff --git a/man/join.Rd b/man/join.Rd index d778df2f..1d274b3a 100644 --- a/man/join.Rd +++ b/man/join.Rd @@ -9,7 +9,7 @@ \alias{full_join_bactlist} \alias{semi_join_bactlist} \alias{anti_join_bactlist} -\title{Join van tabel en \code{bactlist}} +\title{Join a table with \code{bactlist}} \usage{ inner_join_bactlist(x, by = "bactid", ...) @@ -36,3 +36,15 @@ Join the list of microorganisms \code{\link{bactlist}} easily to an existing tab \details{ As opposed to the \code{\link[dplyr]{join}} functions of \code{dplyr}, at default existing columns will get a suffix \code{"2"} and the newly joined columns will not get a suffix. See \code{\link[dplyr]{join}} for more information. } +\examples{ +df <- data.frame(date = seq(from = as.Date("2018-01-01"), + to = as.Date("2018-01-07"), + by = 1), + bacteria_id = c("STAAUR", "STAAUR", "STAAUR", "STAAUR", + "ESCCOL", "ESCCOL", "ESCCOL"), + stringsAsFactors = FALSE) + +colnames(df) +df2 <- left_join_bactlist(df, "bacteria_id") +colnames(df2) +} diff --git a/man/key_antibiotics.Rd b/man/key_antibiotics.Rd index 0255a36a..202c8d22 100644 --- a/man/key_antibiotics.Rd +++ b/man/key_antibiotics.Rd @@ -20,9 +20,18 @@ key_antibiotics(tbl, col_bactcode = "bacteriecode", info = TRUE, \item{amcl, amox, cfot, cfta, cftr, cfur, cipr, clar, clin, clox, doxy, gent, line, mero, peni, pita, rifa, teic, trsu, vanc}{column names of antibiotics.} } +\value{ +Character of length 1. +} \description{ Key antibiotics based on bacteria ID } +\examples{ +\donttest{ +#' # set key antibiotics to a new variable +tbl$keyab <- key_antibiotics(tbl) +} +} \seealso{ \code{\link{mo_property}} \code{\link{ablist}} } diff --git a/man/key_antibiotics_equal.Rd b/man/key_antibiotics_equal.Rd deleted file mode 100644 index 88de3776..00000000 --- a/man/key_antibiotics_equal.Rd +++ /dev/null @@ -1,24 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/first_isolates.R -\name{key_antibiotics_equal} -\alias{key_antibiotics_equal} -\title{Compare key antibiotics} -\usage{ -key_antibiotics_equal(x, y, ignore_I = TRUE, info = FALSE) -} -\arguments{ -\item{x, y}{tekst (or multiple text vectors) with antimicrobial interpretations} - -\item{ignore_I}{ignore \code{"I"} as antimicrobial interpretation of key antibiotics (with \code{FALSE}, changes in antibiograms from S to I and I to R will be interpreted as difference)} - -\item{info}{print progress} -} -\value{ -logical -} -\description{ -Check whether two text values with key antibiotics match. Supports vectors. -} -\seealso{ -\code{\link{key_antibiotics}} -} diff --git a/man/rsi.Rd b/man/rsi.Rd index f3b9afb0..a46b84d8 100644 --- a/man/rsi.Rd +++ b/man/rsi.Rd @@ -24,28 +24,27 @@ rsi(ab1, ab2 = NA, interpretation = "IR", minimum = 30, percent = FALSE, Double or, when \code{percent = TRUE}, a character. } \description{ -This function can be used in \code{\link[dplyr]{summarise}}, see \emph{Examples}. CaBerekent het percentage S, SI, I, IR of R van een lijst isolaten. +This function can be used in \code{dplyr}s \code{\link[dplyr]{summarise}}, see \emph{Examples}. Calculate the percentage S, SI, I, IR or R of a vector of isolates. } \details{ This function uses the \code{\link{rsi_df}} function internally. } \examples{ \dontrun{ +tbl \%>\% + group_by(hospital) \%>\% + summarise(cipr = rsi(cipr)) + tbl \%>\% group_by(year, hospital) \%>\% summarise( isolates = n(), - cipro = rsi(cipr, percent = TRUE), - amoxi = rsi(amox, percent = TRUE) - ) + cipro = rsi(cipr \%>\% as.rsi(), percent = TRUE), + amoxi = rsi(amox \%>\% as.rsi(), percent = TRUE)) + +rsi(as.rsi(isolates$amox)) -tbl \%>\% - group_by(hospital) \%>\% - summarise(cipr = rsi(cipr)) - -rsi(isolates$amox) - -rsi(isolates$amcl, interpretation = "S") +rsi(as.rsi(isolates$amcl), interpretation = "S") } } \keyword{antibiotics} diff --git a/man/rsi_predict.Rd b/man/rsi_predict.Rd index 37c9ef8b..99adb83c 100644 --- a/man/rsi_predict.Rd +++ b/man/rsi_predict.Rd @@ -40,6 +40,7 @@ Create a prediction model to predict antimicrobial resistance for the next years rsi_predict(tbl[which(first_isolate == TRUE & genus == "Haemophilus"),], "amcl") # or with dplyr so you can actually read it: +library(dplyr) tbl \%>\% filter(first_isolate == TRUE, genus == "Haemophilus") \%>\%