unit tests

This commit is contained in:
dr. M.S. (Matthijs) Berends 2022-11-12 22:08:12 +01:00
parent 8fab745ab1
commit 1162d1d9b1
9 changed files with 302 additions and 121 deletions

View File

@ -59,38 +59,35 @@
#' @inheritSection AMR Reference Data Publicly Available
#' @examples
#' # all properties:
#' ab_name("AMX") # "Amoxicillin"
#' ab_atc("AMX") # "J01CA04" (ATC code from the WHO)
#' ab_cid("AMX") # 33613 (Compound ID from PubChem)
#' ab_synonyms("AMX") # a list with brand names of amoxicillin
#' ab_tradenames("AMX") # same
#' ab_group("AMX") # "Beta-lactams/penicillins"
#' ab_atc_group1("AMX") # "Beta-lactam antibacterials, penicillins"
#' ab_atc_group2("AMX") # "Penicillins with extended spectrum"
#' ab_url("AMX") # link to the official WHO page
#' ab_name("AMX")
#' ab_atc("AMX")
#' ab_cid("AMX")
#' ab_synonyms("AMX")
#' ab_tradenames("AMX")
#' ab_group("AMX")
#' ab_atc_group1("AMX")
#' ab_atc_group2("AMX")
#' ab_url("AMX")
#'
#' # smart lowercase tranformation
#' ab_name(x = c("AMC", "PLB")) # "Amoxicillin/clavulanic acid" "Polymyxin B"
#' ab_name(
#' x = c("AMC", "PLB"),
#' tolower = TRUE
#' ) # "amoxicillin/clavulanic acid" "polymyxin B"
#' ab_name(x = c("AMC", "PLB"))
#' ab_name(x = c("AMC", "PLB"), tolower = TRUE)
#'
#' # defined daily doses (DDD)
#' ab_ddd("AMX", "oral") # 1.5
#' ab_ddd_units("AMX", "oral") # "g"
#' ab_ddd("AMX", "iv") # 3
#' ab_ddd_units("AMX", "iv") # "g"
#' ab_ddd("AMX", "oral")
#' ab_ddd_units("AMX", "oral")
#' ab_ddd("AMX", "iv")
#' ab_ddd_units("AMX", "iv")
#'
#' ab_info("AMX") # all properties as a list
#'
#' # all ab_* functions use as.ab() internally, so you can go from 'any' to 'any':
#' ab_atc("AMP") # ATC code of AMP (ampicillin)
#' ab_group("J01CA01") # Drug group of ampicillins ATC code
#' ab_loinc("ampicillin") # LOINC codes of ampicillin
#' ab_name("21066-6") # "Ampicillin" (using LOINC)
#' ab_name(6249) # "Ampicillin" (using CID)
#' ab_name("J01CA01") # "Ampicillin" (using ATC)
#' ab_atc("AMP")
#' ab_group("J01CA01")
#' ab_loinc("ampicillin")
#' ab_name("21066-6")
#' ab_name(6249)
#' ab_name("J01CA01")
#'
#' # spelling from different languages and dyslexia are no problem
#' ab_atc("ceftriaxon")

7
R/av.R
View File

@ -383,13 +383,8 @@ as.av <- function(x, flag_multiple_results = TRUE, info = interactive(), ...) {
next
}
# first 5 except for cephalosporins, then first 7 (those cephalosporins all start quite the same!)
# first 5
found <- suppressWarnings(as.av(substr(x[i], 1, 5), initial_search = FALSE))
if (!is.na(found) && av_group(found, initial_search = FALSE) %unlike% "cephalosporins") {
x_new[i] <- note_if_more_than_one_found(found, i, from_text)
next
}
found <- suppressWarnings(as.av(substr(x[i], 1, 7), initial_search = FALSE))
if (!is.na(found)) {
x_new[i] <- note_if_more_than_one_found(found, i, from_text)
next

View File

@ -53,44 +53,33 @@
#' @inheritSection AMR Reference Data Publicly Available
#' @examples
#' # all properties:
#' av_name("AMX") # "Amoxicillin"
#' av_atc("AMX") # "J01CA04" (ATC code from the WHO)
#' av_cid("AMX") # 33613 (Compound ID from PubChem)
#' av_synonyms("AMX") # a list with brand names of amoxicillin
#' av_tradenames("AMX") # same
#' av_group("AMX") # "Beta-lactams/penicillins"
#' av_atc_group1("AMX") # "Beta-lactam antibacterials, penicillins"
#' av_atc_group2("AMX") # "Penicillins with extended spectrum"
#' av_url("AMX") # link to the official WHO page
#' av_name("ACI")
#' av_atc("ACI")
#' av_cid("ACI")
#' av_synonyms("ACI")
#' av_tradenames("ACI")
#' av_group("ACI")
#' av_url("ACI")
#'
#' # smart lowercase tranformation
#' av_name(x = c("AMC", "PLB")) # "Amoxicillin/clavulanic acid" "Polymyxin B"
#' av_name(
#' x = c("AMC", "PLB"),
#' tolower = TRUE
#' ) # "amoxicillin/clavulanic acid" "polymyxin B"
#' av_name(x = c("ACI", "VALA"))
#' av_name(x = c("ACI", "VALA"), tolower = TRUE)
#'
#' # defined daily doses (DDD)
#' av_ddd("AMX", "oral") # 1.5
#' av_ddd_units("AMX", "oral") # "g"
#' av_ddd("AMX", "iv") # 3
#' av_ddd_units("AMX", "iv") # "g"
#' av_ddd("ACI", "oral")
#' av_ddd_units("ACI", "oral")
#' av_ddd("ACI", "iv")
#' av_ddd_units("ACI", "iv")
#'
#' av_info("AMX") # all properties as a list
#' av_info("ACI") # all properties as a list
#'
#' # all av_* functions use as.av() internally, so you can go from 'any' to 'any':
#' av_atc("AMP") # ATC code of AMP (ampicillin)
#' av_group("J01CA01") # Drug group of ampicillins ATC code
#' av_loinc("ampicillin") # LOINC codes of ampicillin
#' av_name("21066-6") # "Ampicillin" (using LOINC)
#' av_name(6249) # "Ampicillin" (using CID)
#' av_name("J01CA01") # "Ampicillin" (using ATC)
#'
#' # spelling from different languages and dyslexia are no problem
#' av_atc("ceftriaxon")
#' av_atc("cephtriaxone")
#' av_atc("cephthriaxone")
#' av_atc("seephthriaaksone")
#' av_atc("ACI")
#' av_group("J05AB01")
#' av_loinc("abacavir")
#' av_name("29113-8")
#' av_name(135398513)
#' av_name("J05AB01")
av_name <- function(x, language = get_AMR_locale(), tolower = FALSE, ...) {
meet_criteria(x, allow_NA = TRUE)
language <- validate_language(language)
@ -137,7 +126,7 @@ av_tradenames <- function(x, ...) {
av_group <- function(x, language = get_AMR_locale(), ...) {
meet_criteria(x, allow_NA = TRUE)
language <- validate_language(language)
translate_into_language(av_validate(x = x, property = "group", ...), language = language, only_affect_ab_names = TRUE)
translate_into_language(av_validate(x = x, property = "atc_group", ...), language = language, only_affect_ab_names = TRUE)
}
#' @rdname av_property

View File

@ -102,9 +102,9 @@ navbar:
- text: "Get properties of an antibiotic"
icon: "fa-capsules"
href: "reference/ab_property.html" # reference instead of an article
# - text: "Other: benchmarks"
# icon: "fa-shipping-fast"
# href: "articles/benchmarks.html"
- text: "Get properties of an antiviral agent"
icon: "fa-capsules"
href: "reference/av_property.html" # reference instead of an article
- text: "Manual"
icon: "fa-book-open"
href: "reference/index.html"
@ -175,8 +175,19 @@ reference:
- "`mean_amr_distance`"
- "`resistance_predict`"
- "`guess_ab_col`"
- title: "Other: antiviral drugs"
desc: >
This package also provides extensive support for antiviral agents, even though it is not the primary
scope of this package. Working with data containing information about antiviral drugs was never easier.
Use these functions to get valid properties of antiviral drugs from any input or to clean your input.
You can even retrieve drug names and doses from clinical text records, using `av_from_text()`.
contents:
- "`as.av`"
- "`av_property`"
- "`av_from_text`"
- title: "Background information on included data"
- title: "Other: background information on included data"
desc: >
Some pages about our package and its external sources. Be sure to read our [How To's](./../articles/index.html)
for more information about how to work with functions in this package.

80
inst/tinytest/test-av.R Executable file
View File

@ -0,0 +1,80 @@
# ==================================================================== #
# TITLE #
# AMR: An R Package for Working with Antimicrobial Resistance Data #
# #
# SOURCE #
# https://github.com/msberends/AMR #
# #
# CITE AS #
# Berends MS, Luz CF, Friedrich AW, Sinha BNM, Albers CJ, Glasner C #
# (2022). AMR: An R Package for Working with Antimicrobial Resistance #
# Data. Journal of Statistical Software, 104(3), 1-31. #
# doi:10.18637/jss.v104.i03 #
# #
# Developed at the University of Groningen, the Netherlands, in #
# collaboration with non-profit organisations Certe Medical #
# Diagnostics & Advice, and University Medical Center Groningen. #
# #
# 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. #
# 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. #
# #
# Visit our website for the full manual and a complete tutorial about #
# how to conduct AMR data analysis: https://msberends.github.io/AMR/ #
# ==================================================================== #
expect_equal(
as.character(as.av(c(
"J05AB01",
"J 05 AB 01",
"Aciclovir",
"aciclo",
" aciclo 123",
"ACICL",
"ACI",
"Virorax",
"Zovirax"
))),
rep("ACI", 9)
)
expect_identical(class(as.av("acic")), c("av", "character"))
expect_identical(class(antivirals$av), c("av", "character"))
expect_true(is.av(as.av("acic")))
expect_stdout(print(as.av("acic")))
expect_stdout(print(data.frame(a = as.av("acic"))))
expect_warning(as.av("J00AA00")) # ATC not yet available in data set
expect_warning(as.av("UNKNOWN"))
expect_stdout(print(as.av("acic")))
expect_equal(
as.character(as.av("zovirax")),
"ACI"
)
expect_equal(
as.character(as.av(c("Abacaivr", "Celvudine"))),
c("ABA", "CLE")
)
expect_warning(as.av("Abacavir Clevudine"))
# based on Levenshtein distance
expect_identical(av_name("adevofir dypifo", language = NULL), "Adefovir dipivoxil")
# assigning and subsetting
x <- antivirals$av
expect_inherits(x[1], "av")
expect_inherits(x[[1]], "av")
expect_inherits(c(x[1], x[9]), "av")
expect_inherits(unique(x[1], x[9]), "av")
expect_inherits(rep(x[1], 2), "av")
expect_warning(x[1] <- "invalid code")
expect_warning(x[[1]] <- "invalid code")
expect_warning(c(x[1], "test"))

View File

@ -0,0 +1,58 @@
# ==================================================================== #
# TITLE #
# AMR: An R Package for Working with Antimicrobial Resistance Data #
# #
# SOURCE #
# https://github.com/msberends/AMR #
# #
# CITE AS #
# Berends MS, Luz CF, Friedrich AW, Sinha BNM, Albers CJ, Glasner C #
# (2022). AMR: An R Package for Working with Antimicrobial Resistance #
# Data. Journal of Statistical Software, 104(3), 1-31. #
# doi:10.18637/jss.v104.i03 #
# #
# Developed at the University of Groningen, the Netherlands, in #
# collaboration with non-profit organisations Certe Medical #
# Diagnostics & Advice, and University Medical Center Groningen. #
# #
# 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. #
# 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. #
# #
# Visit our website for the full manual and a complete tutorial about #
# how to conduct AMR data analysis: https://msberends.github.io/AMR/ #
# ==================================================================== #
expect_identical(
av_from_text("28/03/2020 regular aciclovir 500mg po tds")[[1]],
as.av("Aciclovir")
)
expect_identical(
av_from_text("28/03/2020 regular aciclovir 500mg po tds", thorough_search = TRUE)[[1]],
as.av("Aciclovir")
)
expect_identical(
av_from_text("28/03/2020 regular aciclovir 500mg po tds", thorough_search = FALSE)[[1]],
as.av("Aciclovir")
)
expect_identical(
av_from_text("28/03/2020 regular aciclovir 500mg po tds", translate_ab = TRUE)[[1]],
"Aciclovir"
)
expect_identical(
av_from_text("administered aciclo and valaciclo", collapse = ", ")[[1]],
"ACI, VALA"
)
expect_identical(
av_from_text("28/03/2020 regular aciclo 500mg po tds", type = "dose")[[1]],
500
)
expect_identical(
av_from_text("28/03/2020 regular aciclo 500mg po tds", type = "admin")[[1]],
"oral"
)

View File

@ -0,0 +1,65 @@
# ==================================================================== #
# TITLE #
# AMR: An R Package for Working with Antimicrobial Resistance Data #
# #
# SOURCE #
# https://github.com/msberends/AMR #
# #
# CITE AS #
# Berends MS, Luz CF, Friedrich AW, Sinha BNM, Albers CJ, Glasner C #
# (2022). AMR: An R Package for Working with Antimicrobial Resistance #
# Data. Journal of Statistical Software, 104(3), 1-31. #
# doi:10.18637/jss.v104.i03 #
# #
# Developed at the University of Groningen, the Netherlands, in #
# collaboration with non-profit organisations Certe Medical #
# Diagnostics & Advice, and University Medical Center Groningen. #
# #
# 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. #
# 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. #
# #
# Visit our website for the full manual and a complete tutorial about #
# how to conduct AMR data analysis: https://msberends.github.io/AMR/ #
# ==================================================================== #
expect_identical(av_name("ACI", language = NULL), "Aciclovir")
expect_identical(av_atc("ACI"), "J05AB01")
expect_identical(av_cid("ACI"), as.integer(135398513))
expect_inherits(av_tradenames("ACI"), "character")
expect_inherits(av_tradenames(c("ACI", "ACI")), "list")
expect_identical(av_group("ACI", language = NULL),"Nucleosides and nucleotides excl. reverse transcriptase inhibitors")
expect_identical(av_name(135398513, language = NULL), "Aciclovir")
expect_identical(av_name("J05AB01", language = NULL), "Aciclovir")
expect_identical(av_ddd("ACI", "oral"), 4)
expect_warning(av_ddd("ACI", "oral", units = TRUE)) # old behaviour
expect_identical(av_ddd_units("ACI", "iv"), "g")
expect_identical(av_ddd("ACI", "iv"), 4)
expect_identical(
av_name(x = c("ACI", "VALA"), tolower = TRUE, language = NULL),
c("aciclovir", "valaciclovir")
)
expect_inherits(av_info("ACI"), "list")
expect_error(av_property("acic", "invalid property"))
expect_error(av_name("acic", language = "INVALID"))
expect_stdout(print(av_name("acic", language = NULL)))
expect_equal(av_name("29113-8", language = NULL), "Abacavir")
expect_equal(
av_loinc("Abacavir"),
c("29113-8", "78772-1", "78773-9", "79134-3", "80118-3")
)
expect_true(av_url("ACI") %like% "whocc.no")
expect_warning(av_url("ASP"))

View File

@ -109,38 +109,35 @@ All data sets in this \code{AMR} package (about microorganisms, antibiotics, R/S
\examples{
# all properties:
ab_name("AMX") # "Amoxicillin"
ab_atc("AMX") # "J01CA04" (ATC code from the WHO)
ab_cid("AMX") # 33613 (Compound ID from PubChem)
ab_synonyms("AMX") # a list with brand names of amoxicillin
ab_tradenames("AMX") # same
ab_group("AMX") # "Beta-lactams/penicillins"
ab_atc_group1("AMX") # "Beta-lactam antibacterials, penicillins"
ab_atc_group2("AMX") # "Penicillins with extended spectrum"
ab_url("AMX") # link to the official WHO page
ab_name("AMX")
ab_atc("AMX")
ab_cid("AMX")
ab_synonyms("AMX")
ab_tradenames("AMX")
ab_group("AMX")
ab_atc_group1("AMX")
ab_atc_group2("AMX")
ab_url("AMX")
# smart lowercase tranformation
ab_name(x = c("AMC", "PLB")) # "Amoxicillin/clavulanic acid" "Polymyxin B"
ab_name(
x = c("AMC", "PLB"),
tolower = TRUE
) # "amoxicillin/clavulanic acid" "polymyxin B"
ab_name(x = c("AMC", "PLB"))
ab_name(x = c("AMC", "PLB"), tolower = TRUE)
# defined daily doses (DDD)
ab_ddd("AMX", "oral") # 1.5
ab_ddd_units("AMX", "oral") # "g"
ab_ddd("AMX", "iv") # 3
ab_ddd_units("AMX", "iv") # "g"
ab_ddd("AMX", "oral")
ab_ddd_units("AMX", "oral")
ab_ddd("AMX", "iv")
ab_ddd_units("AMX", "iv")
ab_info("AMX") # all properties as a list
# all ab_* functions use as.ab() internally, so you can go from 'any' to 'any':
ab_atc("AMP") # ATC code of AMP (ampicillin)
ab_group("J01CA01") # Drug group of ampicillins ATC code
ab_loinc("ampicillin") # LOINC codes of ampicillin
ab_name("21066-6") # "Ampicillin" (using LOINC)
ab_name(6249) # "Ampicillin" (using CID)
ab_name("J01CA01") # "Ampicillin" (using ATC)
ab_atc("AMP")
ab_group("J01CA01")
ab_loinc("ampicillin")
ab_name("21066-6")
ab_name(6249)
ab_name("J01CA01")
# spelling from different languages and dyslexia are no problem
ab_atc("ceftriaxon")

View File

@ -85,44 +85,33 @@ All data sets in this \code{AMR} package (about microorganisms, antibiotics, R/S
\examples{
# all properties:
av_name("AMX") # "Amoxicillin"
av_atc("AMX") # "J01CA04" (ATC code from the WHO)
av_cid("AMX") # 33613 (Compound ID from PubChem)
av_synonyms("AMX") # a list with brand names of amoxicillin
av_tradenames("AMX") # same
av_group("AMX") # "Beta-lactams/penicillins"
av_atc_group1("AMX") # "Beta-lactam antibacterials, penicillins"
av_atc_group2("AMX") # "Penicillins with extended spectrum"
av_url("AMX") # link to the official WHO page
av_name("ACI")
av_atc("ACI")
av_cid("ACI")
av_synonyms("ACI")
av_tradenames("ACI")
av_group("ACI")
av_url("ACI")
# smart lowercase tranformation
av_name(x = c("AMC", "PLB")) # "Amoxicillin/clavulanic acid" "Polymyxin B"
av_name(
x = c("AMC", "PLB"),
tolower = TRUE
) # "amoxicillin/clavulanic acid" "polymyxin B"
av_name(x = c("ACI", "VALA"))
av_name(x = c("ACI", "VALA"), tolower = TRUE)
# defined daily doses (DDD)
av_ddd("AMX", "oral") # 1.5
av_ddd_units("AMX", "oral") # "g"
av_ddd("AMX", "iv") # 3
av_ddd_units("AMX", "iv") # "g"
av_ddd("ACI", "oral")
av_ddd_units("ACI", "oral")
av_ddd("ACI", "iv")
av_ddd_units("ACI", "iv")
av_info("AMX") # all properties as a list
av_info("ACI") # all properties as a list
# all av_* functions use as.av() internally, so you can go from 'any' to 'any':
av_atc("AMP") # ATC code of AMP (ampicillin)
av_group("J01CA01") # Drug group of ampicillins ATC code
av_loinc("ampicillin") # LOINC codes of ampicillin
av_name("21066-6") # "Ampicillin" (using LOINC)
av_name(6249) # "Ampicillin" (using CID)
av_name("J01CA01") # "Ampicillin" (using ATC)
# spelling from different languages and dyslexia are no problem
av_atc("ceftriaxon")
av_atc("cephtriaxone")
av_atc("cephthriaxone")
av_atc("seephthriaaksone")
av_atc("ACI")
av_group("J05AB01")
av_loinc("abacavir")
av_name("29113-8")
av_name(135398513)
av_name("J05AB01")
}
\seealso{
\link{antivirals}