From 7c945b80819f8452aa64c57dc85f43b8f9d81536 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 1 May 2026 06:19:17 +0000 Subject: [PATCH] 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 --- R/interpretive_rules.R | 15 ++++++++++++--- R/zzz.R | 11 ----------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/R/interpretive_rules.R b/R/interpretive_rules.R index a91e222a2..7e57bb5a6 100755 --- a/R/interpretive_rules.R +++ b/R/interpretive_rules.R @@ -619,9 +619,18 @@ interpretive_rules <- function(x, } else if (!is.null(list(...)$eucast_rules_df)) { # deprecated parameter name kept for backward compatibility interpretive_rules_df_total <- list(...)$eucast_rules_df - } else { - # otherwise internal data file, created in data-raw/_pre_commit_checks.R + } else if (exists("INTERPRETIVE_RULES_DF", envir = asNamespace("AMR"), inherits = FALSE)) { + # internal data file, created in data-raw/_pre_commit_checks.R interpretive_rules_df_total <- INTERPRETIVE_RULES_DF + } else { + # transitional fallback: sysdata.rda predates the rename from EUCAST_RULES_DF + # re-run data-raw/_pre_commit_checks.R to regenerate sysdata.rda + interpretive_rules_df_total <- EUCAST_RULES_DF + interpretive_rules_df_total$rule.provider <- "EUCAST" + interpretive_rules_df_total <- interpretive_rules_df_total[ + , c("rule.provider", setdiff(colnames(interpretive_rules_df_total), "rule.provider")), + drop = FALSE + ] } ## filter on guideline provider and user-set guideline versions ---- @@ -661,7 +670,7 @@ interpretive_rules <- function(x, # sometimes, the screenings are missing but the names are actually available # we only hints on remaining rows in `interpretive_rules_df` screening_abx <- as.character(AMR::antimicrobials$ab[which(AMR::antimicrobials$ab %like% "-S$")]) - screening_abx <- screening_abx[screening_abx %in% unique(unlist(strsplit(INTERPRETIVE_RULES_DF$and_these_antibiotics[!is.na(INTERPRETIVE_RULES_DF$and_these_antibiotics)], ", *")))] + screening_abx <- screening_abx[screening_abx %in% unique(unlist(strsplit(interpretive_rules_df_total$and_these_antibiotics[!is.na(interpretive_rules_df_total$and_these_antibiotics)], ", *")))] if (isTRUE(info)) { cat("\n") } diff --git a/R/zzz.R b/R/zzz.R index 5417b2ea9..39eca912f 100755 --- a/R/zzz.R +++ b/R/zzz.R @@ -112,17 +112,6 @@ AMR_env$cross_icon <- if (isTRUE(base::l10n_info()$`UTF-8`)) "\u00d7" else "x" AMR_env$AB_lookup <- cbind(AMR::antimicrobials, AB_LOOKUP) AMR_env$AV_lookup <- cbind(AMR::antivirals, AV_LOOKUP) - - # Transitional: create INTERPRETIVE_RULES_DF alias if sysdata.rda predates the rename from EUCAST_RULES_DF. - # Remove this block once data-raw/_pre_commit_checks.R has been re-run. - ns <- asNamespace("AMR") - if (!exists("INTERPRETIVE_RULES_DF", envir = ns, inherits = FALSE) && - exists("EUCAST_RULES_DF", envir = ns, inherits = FALSE)) { - df <- get("EUCAST_RULES_DF", envir = ns) - df$rule.provider <- "EUCAST" - df <- df[, c("rule.provider", setdiff(colnames(df), "rule.provider")), drop = FALSE] - assignInNamespace("INTERPRETIVE_RULES_DF", df, ns = "AMR") - } } .onAttach <- function(libname, pkgname) {