mirror of
https://github.com/msberends/AMR.git
synced 2026-11-16 00:10:57 +01:00
- New sir_combine() to combine SIR results of multiple antimicrobials into one variable; antibiogram(), wisca() and the count/proportion functions now use the same engine (antibiogram() and wisca() output unchanged) - With multiple antimicrobials, count_*() and proportion_*() now follow the documented combination therapy rules: resistance() is the proportion of failing combinations, count_all() excludes isolates with an unknown combination result, and WT/NWT/NS/NI are handled correctly - where() is now decided at runtime instead of at installation, fixing antibiogram() and wisca() without tidyselect (e.g. CRAN binaries without dplyr) - Imports from data.table and cli moved to .onLoad(); data.table functions are only used while it is loaded; the like() example no longer unloads data.table, which crashed R - Static test that fails on code outside functions depending on suggested packages - New check-without-dplyr workflow: installs AMR with all suggested packages, removes dplyr (and tidyselect), then runs all unit tests and examples against the installed package - g.test() now returns the degrees of freedom as 'parameter', so they are printed - Fix R CMD check NOTEs (hidden .claude folder, lost braces in inherited g.test and ggplot_pca docs)
128 lines
6.7 KiB
R
Executable File
128 lines
6.7 KiB
R
Executable File
# ==================================================================== #
|
|
# TITLE: #
|
|
# AMR: An R Package for Working with Antimicrobial Resistance Data #
|
|
# #
|
|
# SOURCE CODE: #
|
|
# https://github.com/msberends/AMR #
|
|
# #
|
|
# PLEASE CITE THIS SOFTWARE AS: #
|
|
# Berends MS, Luz CF, Friedrich AW, et al. (2022). #
|
|
# AMR: An R Package for Working with Antimicrobial Resistance Data. #
|
|
# Journal of Statistical Software, 104(3), 1-31. #
|
|
# https://doi.org/10.18637/jss.v104.i03 #
|
|
# #
|
|
# Developed at the University of Groningen and the University Medical #
|
|
# Center Groningen in the Netherlands, in collaboration with many #
|
|
# colleagues from around the world, see our website. #
|
|
# #
|
|
# 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://amr-for-r.org #
|
|
# ==================================================================== #
|
|
|
|
test_that("test-misc.R", {
|
|
skip_on_cran()
|
|
|
|
expect_equal(AMR:::percentage(0.25), "25%")
|
|
expect_equal(AMR:::percentage(0.5), "50%")
|
|
expect_equal(AMR:::percentage(0.500, digits = 1), "50.0%")
|
|
expect_equal(AMR:::percentage(0.1234), "12.3%")
|
|
# round up 0.5
|
|
expect_equal(AMR:::percentage(0.0054), "0.5%")
|
|
expect_equal(AMR:::percentage(0.0055), "0.6%")
|
|
|
|
# test functions on all R versions - R < 3.3 did not contain these
|
|
expect_equal(strrep("A", 5), "AAAAA")
|
|
expect_equal(strrep(c("A", "B"), c(5, 2)), c("AAAAA", "BB"))
|
|
expect_equal(trimws(" test "), "test")
|
|
expect_equal(trimws(" test ", "l"), "test ")
|
|
expect_equal(trimws(" test ", "r"), " test")
|
|
expect_equal(AMR:::trimws2(" test "), "test")
|
|
expect_equal(AMR:::trimws2(" test ", "l"), "test ")
|
|
expect_equal(AMR:::trimws2(" test ", "r"), " test")
|
|
|
|
expect_message(AMR:::get_column_abx(example_isolates, soft_dependencies = "FUS"))
|
|
|
|
# we rely on "grouped_tbl" being a class of grouped tibbles, so run a test that checks for this:
|
|
if (AMR:::pkg_is_available("dplyr", min_version = "1.0.0", also_load = TRUE)) {
|
|
expect_true(AMR:::is_null_or_grouped_tbl(example_isolates %>% group_by(ward)))
|
|
}
|
|
|
|
|
|
# test where() ----
|
|
# where() must be AMR's own function, decided at runtime: a copy of tidyselect::where() made at installation fails without tidyselect at runtime
|
|
expect_identical(environment(AMR:::where), asNamespace("AMR"))
|
|
expect_identical(colnames(example_isolates[, AMR:::where(is.sir), drop = FALSE]), colnames(example_isolates)[vapply(FUN.VALUE = logical(1), example_isolates, is.sir)])
|
|
expect_identical(colnames(example_isolates[, c(1:2, AMR:::where(is.mo)), drop = FALSE]), c("date", "patient", "mo"))
|
|
expect_error(AMR:::where("is.sir"))
|
|
# default argument where(is.sir) of antibiogram() works with and without tidyselect
|
|
expect_inherits(antibiogram(example_isolates), "antibiogram")
|
|
if (AMR:::pkg_is_available("dplyr", min_version = "1.0.0", also_load = TRUE)) {
|
|
expect_identical(colnames(example_isolates %>% select(AMR:::where(is.sir))), colnames(example_isolates[, AMR:::where(is.sir), drop = FALSE]))
|
|
expect_identical(colnames(example_isolates %>% select(mo, AMR:::where(is.sir))), c("mo", colnames(example_isolates[, AMR:::where(is.sir), drop = FALSE])))
|
|
}
|
|
|
|
# test get_current_data() ----
|
|
|
|
is_right <- FALSE
|
|
check_df <- function(check_element, return_val = 0) {
|
|
is_right <<- FALSE
|
|
for (env in sys.frames()) {
|
|
# in R < 4.2, reading a promise that is still being evaluated (such as `x` in tibble's `[`) gives an error
|
|
val <- tryCatch(env[[check_element]], error = function(e) NULL)
|
|
if (!is.null(val) && is.data.frame(val)) {
|
|
is_right <<- all(colnames(example_isolates) %in% colnames(val))
|
|
}
|
|
}
|
|
return_val
|
|
}
|
|
|
|
df <- example_isolates[, check_df("x")]
|
|
expect_true(is_right, info = "the environmental data cannot be found for base `x`")
|
|
|
|
# should work on R >=3.6.3 or so
|
|
df <- example_isolates[c(1:3), check_df("x")]
|
|
if (!is_right) {
|
|
# otherwise, this is needed for older versions
|
|
df <- example_isolates[c(1:3), check_df("xx")]
|
|
expect_true(is_right, info = "the environmental data cannot be found for base `x` or `xx`")
|
|
}
|
|
|
|
if (AMR:::pkg_is_available("dplyr", min_version = "1.0.0", also_load = TRUE)) {
|
|
df <- example_isolates %>% select(mo, check_df("data123"))
|
|
expect_false(is_right, info = "just a check if non-sense is not being gathered by get_current_data()")
|
|
|
|
df <- example_isolates %>% select(mo, check_df(".data"))
|
|
expect_true(is_right, info = "the environmental data cannot be found for dplyr/select()")
|
|
|
|
df <- example_isolates %>% select_at(check_df(".tbl"))
|
|
expect_true(is_right, info = "the environmental data cannot be found for dplyr/select_at()")
|
|
}
|
|
|
|
if (AMR:::pkg_is_available("tidymodels", also_load = TRUE)) {
|
|
resistance_recipe <- recipe(mo ~ ., data = example_isolates) %>%
|
|
step_corr(check_df("training")) %>%
|
|
prep()
|
|
expect_true(is_right, info = "the environmental data cannot be found for tidymodels/prep()")
|
|
}
|
|
})
|
|
|
|
test_that("test-cli_to_plain", {
|
|
# plain-text fallback for when cli or rlang is unavailable: must always terminate, also when
|
|
# word wrapping breaks a line within markup, or for expressions that cannot be evaluated
|
|
expect_identical(AMR:::cli_to_plain("a {.code\nx()} b"), "a `x()` b")
|
|
expect_identical(AMR:::cli_to_plain("{.arg x} {not_an_object_in_AMR} {1 + 1}"), "`x` {not_an_object_in_AMR} 2")
|
|
msg <- "Setting {.code parallel = TRUE} requires a non-sequential {.help [{.fun future::plan}](future::plan)} to be active."
|
|
expect_identical(
|
|
gsub("[[:space:]]+", " ", AMR:::cli_to_plain(AMR:::word_wrap(msg, width = 40))),
|
|
"Setting `parallel = TRUE` requires a non-sequential `future::plan()` to be active."
|
|
)
|
|
})
|