1
0
mirror of https://github.com/msberends/AMR.git synced 2026-05-14 01:50:51 +02:00
Files
AMR/R/zz_deprecated.R
Matthijs Berends 24f24ecaf8 Generalise interpretive rules for multi-guideline support (#268) (#283)
* Generalise interpretive rules for multi-guideline support (#268)

- Rename data-raw/eucast_rules.tsv → interpretive_rules.tsv; add rule.provider
  column (value: "EUCAST") to distinguish future CLSI rows
- Rename EUCAST_RULES_DF → INTERPRETIVE_RULES_DF in _pre_commit_checks.R;
  filter by rule.provider == guideline when applying rules in interpretive_rules()
- Rename custom_eucast_rules() → custom_interpretive_rules() with new S3 class
  "custom_interpretive_rules"; old function becomes a deprecated wrapper in
  zz_deprecated.R; backward-compat S3 dispatch shims added for old class
- Remove stop_if(guideline == "CLSI", ...) so clsi_rules() no longer errors
- Add .onLoad shim in zzz.R to create INTERPRETIVE_RULES_DF from EUCAST_RULES_DF
  for transitional compatibility until sysdata.rda is regenerated

https://claude.ai/code/session_01D46BTsfJSPo3HnLWp3PRkP

* Fix namespace load failure: remove assignInNamespace from .onLoad (#268)

assignInNamespace cannot add NEW bindings to a locked package namespace
(R locks namespace bindings before .onLoad runs). Replace the .onLoad
shim with a runtime fallback inside interpretive_rules(): if
INTERPRETIVE_RULES_DF is absent (pre-regeneration sysdata.rda), derive
it from EUCAST_RULES_DF by adding the rule.provider column. This also
fixes the screening_abx line to reuse the already-resolved
interpretive_rules_df_total instead of a bare INTERPRETIVE_RULES_DF
reference.

https://claude.ai/code/session_01D46BTsfJSPo3HnLWp3PRkP

* fixes

* fixes

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-05-01 18:38:51 +01:00

146 lines
5.8 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 #
# ==================================================================== #
#' Deprecated Functions, Arguments, or Datasets
#'
#' These objects are so-called '[Deprecated]'. **They will be removed in a future version of this package.** Using these will give a warning with the name of the alternative object it has been replaced by (if there is one).
#' @keywords internal
#' @name AMR-deprecated
#' @rdname AMR-deprecated
NULL
.amr_deprecation_warn <- function() {
deprecation_warning(old = "antibiotics", new = "antimicrobials", is_dataset = TRUE)
invisible(NULL)
}
#' @export
`[.deprecated_amr_dataset` <- function(x, ...) {
.amr_deprecation_warn()
NextMethod("[")
}
#' @export
`[[.deprecated_amr_dataset` <- function(x, ...) {
.amr_deprecation_warn()
NextMethod("[[")
}
#' @export
`$.deprecated_amr_dataset` <- function(x, name) {
.amr_deprecation_warn()
NextMethod("$")
}
#' @export
print.deprecated_amr_dataset <- function(x, ...) {
.amr_deprecation_warn()
NextMethod("print")
}
#' @export
as.data.frame.deprecated_amr_dataset <- function(x, ...) {
.amr_deprecation_warn()
NextMethod("as.data.frame")
}
# REMEMBER to search for `deprecation_warning` in the package code to find all instances.
# currently deprecated arguments at least:
# - `antibiotics` in `antibiogram()`
# - `converse_capped_values` in `as.sir()`
#' @rdname AMR-deprecated
#' @export
custom_eucast_rules <- function(...) {
deprecation_warning("custom_eucast_rules", "custom_interpretive_rules", is_function = TRUE)
custom_interpretive_rules(...)
}
#' @rdname AMR-deprecated
#' @export
ab_class <- function(...) {
deprecation_warning("ab_class", "amr_class", is_function = TRUE)
amr_class(...)
}
#' @rdname AMR-deprecated
#' @export
ab_selector <- function(...) {
deprecation_warning("ab_selector", "amr_selector", is_function = TRUE)
amr_selector(...)
}
## Helper function ----
deprecation_warning <- function(old = NULL, new = NULL, fn = NULL, extra_msg = NULL, is_function = FALSE, is_dataset = FALSE, is_argument = FALSE) {
if (is.null(old)) {
warning_(extra_msg)
} else if (message_not_thrown_before("deprecation", old, new, entire_session = TRUE)) {
env <- paste0("deprecated_", old)
if (!env %in% names(AMR_env)) {
AMR_env[[paste0("deprecated_", old)]] <- 1
if (isTRUE(is_function)) {
old <- paste0(old, "()")
if (!is.null(new)) {
new <- paste0(new, "()")
}
type <- "function"
} else if (isTRUE(is_dataset)) {
type <- "dataset"
} else if (isTRUE(is_argument)) {
type <- "argument"
if (is.null(fn)) {
stop("Set 'fn' in deprecation_warning()")
}
} else {
stop("Set either 'is_function', 'is_dataset', or 'is_argument' to TRUE in deprecation_warning()")
}
warning_(
ifelse(is.null(new),
paste0("The `", old, "` ", type, " is deprecated"),
ifelse(type == "dataset",
paste0("The `", old, "` ", type, " has been renamed to `", new, "`"),
ifelse(type == "argument",
paste0("The `", old, "` ", type, " in `", fn, "()` has been replaced with `", new, "`: `", fn, "(", new, " = ...)`"),
paste0("The `", old, "` ", type, " has been replaced with `", new, "`")
)
)
),
ifelse(type == "dataset",
". The old name will be removed in future version, so please update your code.",
ifelse(type == "argument",
". While the old argument still works, it will be removed in a future version, so please update your code.",
" and will be removed in a future version, see {.topic [AMR-deprecated](AMR::AMR-deprecated)}."
)
),
ifelse(!is.null(extra_msg),
paste0(" ", extra_msg),
""
),
"\nThis warning will be shown once per session."
)
}
}
}