Skip to content

Instantly share code, notes, and snippets.

@CorradoLanera
Created November 17, 2020 10:50
Show Gist options
  • Save CorradoLanera/c352004658a5a69713d35d00fd5f2fc0 to your computer and use it in GitHub Desktop.
Save CorradoLanera/c352004658a5a69713d35d00fd5f2fc0 to your computer and use it in GitHub Desktop.
Information of all packages possibly installed when `install.packages()` a package
'All installed by.
USAGE:
all_installed_by.R [-d] [-s] [-f=<FILE>] <PKG>...
all_installed_by.R [-d] [-s] [-f=<FILE>] -i
all_installed_by.R [-d] [-s] [-f=<FILE>] -c
all_installed_by.R (-h | --help)
OPTIONS:
-h --help Show this screen.
-d --dependencies Consider dependencies (not recursively) [default: FALSE]
-i --installed Consider all the installed packages.
-c --cran Consider all CRAN packages.
-s --save Would you like to save the results? [default: FALSE]
-f --file=<FILE> Filename for the file to save with the results [default: all_installed.rds]
ARGUMENTS:
PKG package(s) to investigate (separate multiple packages by a space)
DESCRIPTION:
Which (and how many) packages would R will install by `install.packages()` a package (possibly with `dependencies = TRUE`) if you do not have any other package installed?
This script helps you to get an answer. It returns (and possibly saves) a tibble with three columns:
- pkg: name of the package considered.
- installed: (list column of the) character vectors of packages installed by `pkg`.
- n_installed: `length()` of `installed`.
You can pass to the function API a sequence of packages\' names, or you can ask to investigate all your installed CRAN\'s packages, or even the whole CRAN altogether.
When running the script through its APIs, you are in a non-interactive R session (`interactive()` returns `FALSE`). So, if you like to have the progress bar displayed, run `export R_PROGRESSR_ENABLE=TRUE` before calling this script (see https://github.com/HenrikBengtsson/progressr#progress-updates-in-non-interactive-mode-batch-mode).
EXAMPLES:
Rscript all_installed_by.R -disf mine_installed.rds
Rscript all_installed_by.R -dcsf cran_installed.rds
Rscript all_installed_by.R -d tidyverse shiny
Rscript all_installed_by.R tidyverse shiny
' -> doc
suppressPackageStartupMessages(library(docopt))
arguments <- docopt(doc, version = 'All installed 0.1')
suppressPackageStartupMessages(library(tidyverse))
suppressPackageStartupMessages(library(progressr))
handlers(list(handler_progress(
format = ":spin :current/:total [:bar] :percent in :elapsed ETA: :eta",
width = 60,
complete = "+"
)))
cran <- remotes::available_packages()
pkgs <- if (arguments$cran) {
cran[, "Package"]
} else if (arguments$installed) {
installed <- installed.packages()[, "Package"]
installed_cran <- setdiff(installed,
setdiff(installed, cran[, "Package"])
)
if (length(installed) != length(installed_cran)) warning(
"installed packages not in CRAN will be excluded"
)
installed_cran
} else {
required <- arguments$PKG
required_cran <- setdiff(required,
setdiff(required, cran[, "Package"])
)
if (length(required) != length(required_cran)) warning(
"required packages not in CRAN will be excluded"
)
required_cran
} %>%
set_names()
deps_names <- function(pkg,
all_available = remotes::available_packages(),
dependencies = NA
) {
tools::package_dependencies(pkg,
db = all_available,
which = remotes::standardise_dep(dependencies)
)
}
pkgs_installed_by <- function(pkg,
all_available = remotes::available_packages(),
dependencies = NA
) {
deps_first <- deps_names(pkg,
all_available = all_available,
dependencies = dependencies
)[[1]]
deps_deps <- unique(unlist(use.names = FALSE, deps_names(deps_first,
all_available = all_available, dependencies = NA
)))
sort(unique(c(pkg, deps_first, deps_deps)))
}
with_dep <- if (arguments$dependencies) TRUE else NA
message("Use dependencies: ", with_dep)
with_progress(expr = {
p <- progressor(along = pkgs)
out <- map(pkgs, ~{
res <- pkgs_installed_by(.x,
dependencies = with_dep,
all_available = cran
)
p()
res
}) %>%
set_names(pkgs)
})
all_installed <- tibble(
pkg = names(out),
installed = out,
n_installed = map_int(out, length)
) %>%
arrange(desc(n_installed))
if (arguments$s) {
saveRDS(all_installed, file = arguments$file)
}
print(all_installed)
@CorradoLanera
Copy link
Author

2020-11-17

$ Rscript all_installed_by.R -dc
Use dependencies: TRUE
# A tibble: 16,561 x 3
   pkg        installed    n_installed
   <chr>      <named list>       <int>
 1 mlr        <chr [288]>          288
 2 broom      <chr [232]>          232
 3 parameters <chr [219]>          219
 4 insight    <chr [210]>          210
 5 modeltime  <chr [166]>          166
 6 agridat    <chr [165]>          165
 7 fitteR     <chr [163]>          163
 8 pubh       <chr [163]>          163
 9 concurve   <chr [160]>          160
10 butcher    <chr [156]>          156

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment