From a9612c4eacb567ee2384d8d68ea13f088b98607b Mon Sep 17 00:00:00 2001 From: yassineS <1767457+yassineS@users.noreply.github.com> Date: Thu, 16 Apr 2026 22:44:37 +0930 Subject: [PATCH] refactor: extract duplicated warning/message pattern to .report_messages() Extracts the 7-line warning/message reporting pattern that was duplicated in permute.R, prune.R, and rescale.R into a new .report_messages() helper function in internal.R. Uses the renamed variables from PR #26 (warning.messages, info.messages, param.warnings, param.messages) and uses TRUE/FALSE instead of T/F. Addresses issue #6 from code audit (#23) --- R/internal.R | 27 +++++++++++++++++++++++++++ R/permute.R | 7 +------ R/prune.R | 7 +------ R/rescale.R | 7 +------ 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/R/internal.R b/R/internal.R index 7b24108..5b830c2 100644 --- a/R/internal.R +++ b/R/internal.R @@ -12,6 +12,33 @@ } +#' @title Report accumulated messages and warnings +#' @description Centralized reporting of info/warning messages collected +#' during argument validation. Reads from the calling environment. +#' @noRd +.report_messages <- function() { + .verbose_msg("\n") + + # Report warnings + if (!is.null(warning.messages)) { + warning(warning.messages, immediate. = TRUE, call. = FALSE) + } + if (!is.null(param.warnings)) { + warning(param.warnings, immediate. = TRUE, call. = FALSE) + } + + # Report info messages + if (!is.null(info.messages)) { + .verbose_msg(paste0("\n", info.messages, "\n")) + } + if (!is.null(param.messages)) { + .verbose_msg(paste0("\n", param.messages, "\n")) + } + + .verbose_msg("\n") +} + + #' @title Internal file path checker #' @description An internal function to check for correct file paths for #' the required input files. User can either provide path to folder with all diff --git a/R/permute.R b/R/permute.R index 3e4d2b4..e2f149c 100644 --- a/R/permute.R +++ b/R/permute.R @@ -281,12 +281,7 @@ permute_polylinkr_data <- function(plr_input, permute = TRUE, n_permutations = 5 align = "center", col = "cyan", border_col = "cyan")) # report collated warnings and messages - .verbose_msg("\n") - if (!is.null(warning.messages)) warning(warning.messages, immediate. = T, call. = F) - if (!is.null(info.messages)) .verbose_msg(paste0("\n", info.messages, "\n")) - if (!is.null(param.warnings)) warning(param.warnings, immediate. = T, call. = F) - if (!is.null(param.messages)) .verbose_msg(paste0("\n", param.messages, "\n")) - .verbose_msg("\n") + .report_messages() ##=========================================================================## ## PART 2: Calculate standardised gene scores diff --git a/R/prune.R b/R/prune.R index b1dbeeb..fcbba18 100644 --- a/R/prune.R +++ b/R/prune.R @@ -176,12 +176,7 @@ prune_polylinkr_data <- function(plr_input, n_fdr = 300L, estimate_pi0 = TRUE, align = "center", col = "cyan", border_col = "cyan")) # report collated warnings and messages - .verbose_msg("\n") - if (!is.null(warning.messages)) warning(warning.messages, immediate. = T, call. = F) - if (!is.null(info.messages)) .verbose_msg(paste0("\n", info.messages, "\n")) - if (!is.null(param.warnings)) warning(param.warnings, immediate. = T, call. = F) - if (!is.null(param.messages)) .verbose_msg(paste0("\n", param.messages, "\n")) - .verbose_msg("\n") + .report_messages() ##=========================================================================## ##PART 2: generate FDR sets and empirical prepare objects for pruning step diff --git a/R/rescale.R b/R/rescale.R index 4a53dc8..5286ba4 100644 --- a/R/rescale.R +++ b/R/rescale.R @@ -250,12 +250,7 @@ rescale_polylinkr_data <- function(plr_input, rescale = TRUE, fast = TRUE, ac = align = "center", col = "cyan", border_col = "cyan")) # report collated warnings and messages - .verbose_msg("\n") - if (!is.null(warning.messages)) warning(warning.messages, immediate. = T, call. = F) - if (!is.null(info.messages)) .verbose_msg(paste0("\n", info.messages, "\n")) - if (!is.null(param.warnings)) warning(param.warnings, immediate. = T, call. = F) - if (!is.null(param.messages)) .verbose_msg(paste0("\n", param.messages, "\n")) - .verbose_msg("\n") + .report_messages() ##==========================================================================## ## PART 2: Calculate genomic autocovariance