From 6837bf6989b9a2953a2609ff220f7b785ef604dc Mon Sep 17 00:00:00 2001 From: Danny Colombara Date: Thu, 15 May 2025 09:50:28 -0700 Subject: [PATCH] abstract check for latest version to a function - implemented for rads.data and requested by Daniel for rads, so adding to apde.chi.tools for consistency --- NAMESPACE | 1 + R/check_version.R | 113 ++++++++++++++++++++++++++++++++++++ R/zzz.R | 42 +------------- man/check_version.Rd | 32 ++++++++++ man/chi_suppress_results.Rd | 2 +- 5 files changed, 150 insertions(+), 40 deletions(-) create mode 100644 R/check_version.R create mode 100644 man/check_version.Rd diff --git a/NAMESPACE b/NAMESPACE index b51d2f4..cf8dd15 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,6 @@ # Generated by roxygen2: do not edit by hand +export(check_version) export(chi_calc) export(chi_chars_ccs) export(chi_chars_injury) diff --git a/R/check_version.R b/R/check_version.R new file mode 100644 index 0000000..ecff974 --- /dev/null +++ b/R/check_version.R @@ -0,0 +1,113 @@ +#' Check if the installed apde.chi.tools package version is the latest available +#' +#' This function checks if the installed version of the apde.chi.tools package is the latest +#' available on GitHub. It compares the local version with the version in the +#' DESCRIPTION file on the main branch of the GitHub repository. +#' +#' @param print_message Logical. Whether to print messages about version status. +#' Default is TRUE. +#' +#' @return Invisibly returns a list with the following components: +#' \item{is_current}{Logical. TRUE if the installed version is the latest, FALSE otherwise.} +#' \item{local_version}{The version of the installed apde.chi.tools package.} +#' \item{remote_version}{The version on GitHub, or NULL if it couldn't be determined.} +#' \item{status}{Character string. The status of the check (e.g., "success", "error").} +#' \item{message}{Character string. Detailed message about the check result.} +#' +#' @examples +#' \dontrun{ +#' # Check if apde.chi.tools is up to date +#' apde.chi.tools::check_version() +#' } +#' +#' @export +check_version <- function(print_message = TRUE) { + + # Initialize return values + result <- list( + is_current = TRUE, + local_version = NULL, + remote_version = NULL, + status = "error", + message = "" + ) + + tryCatch({ + # Check if httr is available + if (!requireNamespace("httr", quietly = TRUE)) { + result$message <- "httr package is needed to check for updates and is not installed." + if (print_message) { + message(result$message) + } + return(invisible(result)) + } + + # Construct URL to raw DESCRIPTION file + url <- "https://raw.githubusercontent.com/PHSKC-APDE/apde.chi.tools/main/DESCRIPTION" + resp <- httr::GET(url) + + if (httr::status_code(resp) == 200) { # 200 means is okay / was successful + desc_text <- httr::content(resp, "text") + + # Parse the DESCRIPTION file + # use Debian Control Format (.dcf) because the DESCRIPTION file in R uses dcf + desc_vals <- tryCatch(read.dcf(textConnection(desc_text)), error = function(e) NULL) + + if (!is.null(desc_vals) && "Version" %in% colnames(desc_vals)) { + remote_version <- package_version(desc_vals[1, "Version"]) + result$remote_version <- remote_version + + # Get local version + local_version <- utils::packageVersion("apde.chi.tools") + result$local_version <- local_version + + # Compare versions + if (remote_version > local_version) { + result$is_current <- FALSE + update_msg <- ifelse(requireNamespace("devtools", quietly = TRUE), + 'To update, run: devtools::install_github("PHSKC-APDE/apde.chi.tools", auth_token = NULL)', + 'To update, install devtools and then run: devtools::install_github("PHSKC-APDE/apde.chi.tools", auth_token = NULL)' + ) + + result$message <- paste0( + "A newer version is available: ", remote_version, "\n", + "Your version: ", local_version, "\n", + update_msg + ) + + if (print_message) { + message("-----------------------------------------------------") + message("\u26A0\ufe0f A newer version of apde.chi.tools is available: ", remote_version) + message("Your version: ", local_version) + message('\U0001f504 ', update_msg) + message("-----------------------------------------------------") + } + } else { + result$message <- paste0("Package apde.chi.tools is up to date (version ", local_version, ").") + if (print_message) { + message(result$message) + } + } + + result$status <- "success" + } else { + result$message <- "Could not parse version information from DESCRIPTION file." + if (print_message) { + message(result$message) + } + } + } else { + result$message <- paste0("Could not retrieve version info from GitHub (status: ", httr::status_code(resp), ")") + if (print_message) { + message(result$message) + } + } + }, error = function(e) { + result$message <- paste0("Version check failed: ", conditionMessage(e)) + if (print_message) { + message(result$message) + } + }) + + invisible(result) +} diff --git a/R/zzz.R b/R/zzz.R index d591e0b..b64116e 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -1,41 +1,5 @@ .onAttach <- function(libname, pkgname) { - tryCatch({ - if (!requireNamespace("httr", quietly = TRUE)) { - packageStartupMessage("apde.chi.tools update check skipped: \n'httr' package is needed to check for updates and is not installed.") - return() - } - - url <- "https://raw.githubusercontent.com/PHSKC-APDE/apde.chi.tools/main/DESCRIPTION" - resp <- httr::GET(url) - - if (httr::status_code(resp) == 200) { # 200 means is okay / was successful - desc_text <- httr::content(resp, "text") - - # use Debian Control Format (.dcf) because the DESCRIPTION file in R uses dcf - desc_vals <- tryCatch(read.dcf(textConnection(desc_text)), error = function(e) NULL) - - remote_version <- NULL - if (!is.null(desc_vals) && "Version" %in% colnames(desc_vals)) { - remote_version <- package_version(desc_vals[1, "Version"]) - } - - local_version <- utils::packageVersion(pkgname) - - if (!is.null(remote_version) && remote_version > local_version) { - packageStartupMessage("-----------------------------------------------------") - packageStartupMessage("\u26A0\ufe0f A newer version of apde.chi.tools is available: ", remote_version) - packageStartupMessage("Your version: ", local_version) - update_msg <- ifelse(requireNamespace("devtools", quietly = TRUE), - '\U0001f504 To update, run: devtools::install_github("PHSKC-APDE/apde.chi.tools", auth_token = NULL)', - '\U0001f504 To update, install devtools and then run: devtools::install_github("PHSKC-APDE/apde.chi.tools", auth_token = NULL)' - ) - packageStartupMessage(update_msg) - packageStartupMessage("-----------------------------------------------------") - } - } else { - packageStartupMessage("Could not retrieve version info from GitHub (status: ", httr::status_code(resp), ")") - } - }, error = function(e) { - packageStartupMessage("Version check skipped: could not connect to GitHub") - }) + if (interactive()) { + check_version() + } } diff --git a/man/check_version.Rd b/man/check_version.Rd new file mode 100644 index 0000000..297b336 --- /dev/null +++ b/man/check_version.Rd @@ -0,0 +1,32 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/check_version.R +\name{check_version} +\alias{check_version} +\title{Check if the installed apde.chi.tools package version is the latest available} +\usage{ +check_version(print_message = TRUE) +} +\arguments{ +\item{print_message}{Logical. Whether to print messages about version status. +Default is TRUE.} +} +\value{ +Invisibly returns a list with the following components: + \item{is_current}{Logical. TRUE if the installed version is the latest, FALSE otherwise.} + \item{local_version}{The version of the installed apde.chi.tools package.} + \item{remote_version}{The version on GitHub, or NULL if it couldn't be determined.} + \item{status}{Character string. The status of the check (e.g., "success", "error").} + \item{message}{Character string. Detailed message about the check result.} +} +\description{ +This function checks if the installed version of the apde.chi.tools package is the latest +available on GitHub. It compares the local version with the version in the +DESCRIPTION file on the main branch of the GitHub repository. +} +\examples{ +\dontrun{ +# Check if apde.chi.tools is up to date +apde.chi.tools::check_version() +} + +} diff --git a/man/chi_suppress_results.Rd b/man/chi_suppress_results.Rd index a6c283e..5d448a1 100644 --- a/man/chi_suppress_results.Rd +++ b/man/chi_suppress_results.Rd @@ -15,7 +15,7 @@ chi_suppress_results( denominator_col = "denominator", rse_col = "rse", columns_to_suppress = c("result", "lower_bound", "upper_bound", "se", "rse", - "numerator", "denominator") + "numerator", "denominator", "comparison_with_kc") ) } \arguments{