Skip to content

Commit

Permalink
use standalone-pkg script
Browse files Browse the repository at this point in the history
  • Loading branch information
Yunuuuu committed Feb 25, 2025
1 parent bab5fde commit be51bfd
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Collate:
'ggplot-utils.R'
'import-standalone-assert.R'
'import-standalone-obj-type.R'
'import-standalone-pkg.R'
'import-standalone-purrr.R'
'import-standalone-tibble.R'
'layer-order.R'
Expand Down
55 changes: 55 additions & 0 deletions R/import-standalone-pkg.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Standalone file: do not edit by hand
# Source: </~https://github.com/Yunuuuu/standalone/blob/main/R/standalone-pkg.R>
# ----------------------------------------------------------------------
#
# ---
# repo: Yunuuuu/standalone
# file: standalone-pkg.R
# last-updated: 2025-02-26
# license: https://unlicense.org
# imports: [utils]
# ---

# This file contains several helper functions for package checking and
# installation.

# ## Changelog
# 2025-02-26:
# - Add `is_installed`
# - Add `install_pkgs`
# - Add `pkg_nm`
# - Add `pkg_namespace`
#
# nocov start

is_installed <- local({
cache <- new.env(parent = emptyenv())
function(pkg, version = NULL) {
id <- if (is.null(version)) pkg else paste(pkg, version, sep = ":")
out <- cache[[id]]
if (is.null(out)) {
if (is.null(version)) {
out <- requireNamespace(pkg, quietly = TRUE)
} else {
out <- requireNamespace(pkg, quietly = TRUE) &&
utils::packageVersion(pkg) >= version
}
assign(id, out, envir = cache, inherits = FALSE)
}
out
}
})

install_pkgs <- function(pkgs) {
if (is_installed("pak")) {
utils::getFromNamespace("pkg_install", "pak")(pkgs, ask = FALSE)
} else {
utils::install.packages(pkgs)
}
}

pkg_nm <- function() utils::packageName(environment())

pkg_namespace <- function() topenv(environment())

# nocov end
3 changes: 0 additions & 3 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,6 @@ override_call <- function(call = NULL) {
)
}

#' @importFrom utils packageName
pkg_nm <- function() packageName(topenv(environment()))

# library(data.table)
# library(vctrs)
# `%nest_unique%` <- function(x, y) {
Expand Down

0 comments on commit be51bfd

Please sign in to comment.