diff --git a/R/standalone-check_pkg_installed.R b/R/standalone-check_pkg_installed.R index c1132e6..571303b 100644 --- a/R/standalone-check_pkg_installed.R +++ b/R/standalone-check_pkg_installed.R @@ -28,18 +28,27 @@ #' - `get_pkg_dependencies()` returns a tibble with all #' dependencies of a specific package. #' -#' - `get_min_version_required()` will return, if any, the minimum version -#' of `pkg` required by `reference_pkg`. +#' - `get_min_version_required()` will return, if any, the minimum version of `pkg` required by `ref`. #' #' @param pkg (`character`)\cr #' vector of package names to check. #' @param call (`environment`)\cr #' frame for error messaging. Default is [get_cli_abort_call()]. -#' @param reference_pkg (`string`)\cr +#' @param ref (`string`)\cr #' name of the package the function will search for a minimum required version from. #' @param lib.loc (`path`)\cr #' location of `R` library trees to search through, see [utils::packageDescription()]. #' +#' @details +#' The `ref` argument (`pkg` in `get_pkg_dependencies`) uses `packageName()` as a default, which returns the package in +#' which the current environment or function is run from. The current environment is determined via `parent.frame()`. +#' +#' If, for example, `get_min_version_required("dplyr", ref = packageName())` is run within a `cards` function, and this +#' function is then called within a function of the `cardx` package, the minimum version returned by the +#' `get_min_version_required` call will return the version required by the `cards` package. If run within a test file, +#' `packageName()` returns the package of the current test. Within Roxygen `@examplesIf` calls, `packageName()` will +#' returns the package of the current example. +#' #' @return `is_pkg_installed()` and `check_pkg_installed()` returns a logical or error, #' `get_min_version_required()` returns a data frame with the minimum version required, #' `get_pkg_dependencies()` returns a tibble. @@ -61,11 +70,13 @@ NULL #' @keywords internal #' @noRd check_pkg_installed <- function(pkg, - reference_pkg = "cards", + ref = packageName(), call = get_cli_abort_call()) { + if (!is.character(ref) && !is.null(ref)) cli::cli_abort("{.arg ref} must be a string.") + # get min version data ------------------------------------------------------- df_pkg_min_version <- - get_min_version_required(pkg = pkg, reference_pkg = reference_pkg) + get_min_version_required(pkg = pkg, ref = ref) # prompt user to install package --------------------------------------------- rlang::check_installed( @@ -82,10 +93,12 @@ check_pkg_installed <- function(pkg, #' @keywords internal #' @noRd is_pkg_installed <- function(pkg, - reference_pkg = "cards") { + ref = packageName()) { + if (!is.character(ref) && !is.null(ref)) cli::cli_abort("{.arg ref} must be a string.") + # get min version data ------------------------------------------------------- df_pkg_min_version <- - get_min_version_required(pkg = pkg, reference_pkg = reference_pkg) + get_min_version_required(pkg = pkg, ref = ref) # check installation TRUE/FALSE ---------------------------------------------- rlang::is_installed( @@ -99,13 +112,19 @@ is_pkg_installed <- function(pkg, #' @inheritParams check_pkg_installed #' @keywords internal +#' +#' @param pkg (`string`)\cr +#' name of the package the function will search for dependencies from. +#' #' @noRd -get_pkg_dependencies <- function(reference_pkg = "cards", lib.loc = NULL) { - if (rlang::is_empty(reference_pkg)) { +get_pkg_dependencies <- function(pkg = packageName(), lib.loc = NULL) { + if (!is.character(pkg) && !is.null(pkg)) cli::cli_abort("{.arg pkg} must be a string.") + + if (rlang::is_empty(pkg)) { return(.empty_pkg_deps_df()) } - description <- utils::packageDescription(reference_pkg, lib.loc = lib.loc) |> suppressWarnings() + description <- utils::packageDescription(pkg, lib.loc = lib.loc) |> suppressWarnings() if (identical(description, NA)) { return(.empty_pkg_deps_df()) } @@ -157,9 +176,11 @@ get_pkg_dependencies <- function(reference_pkg = "cards", lib.loc = NULL) { #' @inheritParams check_pkg_installed #' @keywords internal #' @noRd -get_min_version_required <- function(pkg, reference_pkg = "cards", lib.loc = NULL) { +get_min_version_required <- function(pkg, ref = packageName(), lib.loc = NULL) { + if (!is.character(ref) && !is.null(ref)) cli::cli_abort("{.arg ref} must be a string.") + # if no package reference, return a df with just the pkg names - if (rlang::is_empty(reference_pkg)) { + if (rlang::is_empty(ref)) { return( .empty_pkg_deps_df() |> dplyr::full_join( @@ -172,7 +193,7 @@ get_min_version_required <- function(pkg, reference_pkg = "cards", lib.loc = NUL # get the package_ref deps and subset on requested pkgs, also supplement df with pkgs # that may not be proper deps of the reference package (these pkgs don't have min versions) res <- - get_pkg_dependencies(reference_pkg, lib.loc = lib.loc) |> + get_pkg_dependencies(ref, lib.loc = lib.loc) |> dplyr::filter(.data$pkg %in% .env$pkg) |> dplyr::full_join( dplyr::tibble(pkg = pkg), diff --git a/R/standalone-stringr.R b/R/standalone-stringr.R index ca4123c..d99501d 100644 --- a/R/standalone-stringr.R +++ b/R/standalone-stringr.R @@ -25,8 +25,8 @@ str_squish <- function(string, fixed = FALSE, perl = !fixed) { return(string) } -str_remove <- function (string, pattern, fixed = FALSE, perl = !fixed) { - sub (x = string, pattern = pattern, replacement = "", fixed = fixed, perl = perl) +str_remove <- function(string, pattern, fixed = FALSE, perl = !fixed) { + sub(x = string, pattern = pattern, replacement = "", fixed = fixed, perl = perl) } str_remove_all <- function(string, pattern, fixed = FALSE, perl = !fixed) { @@ -53,7 +53,7 @@ str_replace <- function(string, pattern, replacement, fixed = FALSE, perl = !fix sub(x = string, pattern = pattern, replacement = replacement, fixed = fixed, perl = perl) } -str_replace_all <- function (string, pattern, replacement, fixed = FALSE, perl = !fixed){ +str_replace_all <- function(string, pattern, replacement, fixed = FALSE, perl = !fixed) { gsub(x = string, pattern = pattern, replacement = replacement, fixed = fixed, perl = perl) } @@ -84,7 +84,7 @@ word <- function(string, start, end = start, sep = " ", fixed = TRUE, perl = !fi } } -str_sub <- function(string, start = 1L, end = -1L){ +str_sub <- function(string, start = 1L, end = -1L) { str_length <- nchar(string) # Adjust start and end indices for negative values @@ -98,11 +98,11 @@ str_sub <- function(string, start = 1L, end = -1L){ substr(x = string, start = start, stop = end) } -str_sub_all <- function(string, start = 1L, end = -1L){ +str_sub_all <- function(string, start = 1L, end = -1L) { lapply(string, function(x) substr(x, start = start, stop = end)) } -str_pad <- function(string, width, side = c("left", "right", "both"), pad = " ", use_width = TRUE){ +str_pad <- function(string, width, side = c("left", "right", "both"), pad = " ", use_width = TRUE) { side <- match.arg(side, c("left", "right", "both")) if (side == "both") { @@ -127,7 +127,7 @@ str_split <- function(string, pattern, n = Inf, fixed = FALSE, perl = !fixed) { parts <- strsplit(string, split = pattern, fixed = fixed, perl = perl) lapply(parts, function(x) { if (length(x) > n) { - x <- c(x[1:(n-1)], paste(x[n:length(x)], collapse = pattern)) + x <- c(x[1:(n - 1)], paste(x[n:length(x)], collapse = pattern)) } return(x) }) diff --git a/tests/testthat/test-standalone-check_pkg_installed.R b/tests/testthat/test-standalone-check_pkg_installed.R index 57d1432..48614b3 100644 --- a/tests/testthat/test-standalone-check_pkg_installed.R +++ b/tests/testthat/test-standalone-check_pkg_installed.R @@ -22,7 +22,7 @@ test_that("check_pkg_installed() works", { ) ) expect_equal( - get_min_version_required("brms", reference_pkg = NULL), + get_min_version_required("brms", ref = NULL), dplyr::tibble( reference_pkg = NA_character_, reference_pkg_version = NA_character_, dependency_type = NA_character_, pkg = "brms", version = NA_character_, @@ -30,7 +30,7 @@ test_that("check_pkg_installed() works", { ) ) expect_equal( - get_min_version_required("dplyr", reference_pkg = NULL), + get_min_version_required("dplyr", ref = NULL), dplyr::tibble( reference_pkg = NA_character_, reference_pkg_version = NA_character_, dependency_type = NA_character_, pkg = "dplyr", version = NA_character_,