Skip to content

Commit

Permalink
Merge pull request #32 from ddsjoberg/26_shorter_ref_arg_name
Browse files Browse the repository at this point in the history
Shorten `reference_pkg` argument name to `ref`
  • Loading branch information
ddsjoberg authored Sep 13, 2024
2 parents 44f0878 + 1bf6c15 commit 230ce55
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 22 deletions.
47 changes: 34 additions & 13 deletions R/standalone-check_pkg_installed.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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())
}
Expand Down Expand Up @@ -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(
Expand All @@ -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),
Expand Down
14 changes: 7 additions & 7 deletions R/standalone-stringr.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}

Expand Down Expand Up @@ -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
Expand All @@ -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") {
Expand All @@ -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)
})
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-standalone-check_pkg_installed.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ 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_,
compare = NA_character_
)
)
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_,
Expand Down

0 comments on commit 230ce55

Please sign in to comment.