Skip to content

Commit

Permalink
add a function install_deps() to install dependencies specified in DE…
Browse files Browse the repository at this point in the history
…SCRIPTION
  • Loading branch information
yihui committed Jan 6, 2025
1 parent 89c7eea commit 7780b7b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: xfun
Type: Package
Title: Supporting Functions for Packages Maintained by 'Yihui Xie'
Version: 0.49.12
Version: 0.49.13
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre", "cph"), email = "[email protected]", comment = c(ORCID = "0000-0003-0645-5666")),
person("Wush", "Wu", role = "ctb"),
Expand Down
25 changes: 22 additions & 3 deletions R/packages.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ pkg_update = function(...) {
# allow users to specify a custom install.packages() function via the global
# option xfun.install.packages
pkg_install = function(pkgs, install = TRUE, ...) {
if (length(pkgs) == 0) return()
if (length(pkgs) == 0) return(invisible())
# in case the CRAN repo is not set up
repos = getOption('repos')
if (length(repos) == 0 || identical(repos, c(CRAN = '@CRAN@'))) {
opts = options(repos = c(CRAN = 'https://cran.rstudio.com'))
opts = options(repos = c(CRAN = 'https://cloud.r-project.org'))
on.exit(options(opts), add = TRUE)
}
if (length(pkgs) > 1)
Expand All @@ -142,7 +142,7 @@ pkg_install = function(pkgs, install = TRUE, ...) {
}
)
if (identical(install, 'pak')) install = pak::pkg_install
retry(install, pkgs, ..., .pause = 0)
invisible(retry(install, pkgs, ..., .pause = 0))
}

#' Find out broken packages and reinstall them
Expand Down Expand Up @@ -203,6 +203,25 @@ pkg_build = function(dir = '.', opts = NULL) {
pkg
}

# install dependencies given a package DESCRIPTION
install_deps = function(dir = '.') {
d = read.dcf(file.path(dir, 'DESCRIPTION'))
d = d[1, intersect(colnames(d), c('Depends', 'Imports', 'Suggests', 'LinkingTo'))]
d = unlist(strsplit(d, ',\\s*'), use.names = FALSE)
d = gsub('\\(.*\\)', '', d)
d = gsub('^\\s+|\\s+$', '', d)
d = setdiff(d, c('R', base_pkgs()))
if (length(d)) {
db = available.packages()
d2 = NULL
for (p in intersect(d, rownames(db))) d2 = c(d2, tryCatch(
if (packageVersion(p) < db[p, 'Version']) p,
error = function(e) p
))
pkg_install(d2)
}
}

#' An alias of `remotes::install_github()`
#'
#' This alias is to make autocomplete faster via `xfun::install_github`, because
Expand Down

0 comments on commit 7780b7b

Please sign in to comment.