Skip to content

Instantly share code, notes, and snippets.

@wch
Created September 16, 2019 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wch/fd82214c8b4b3eca4b5003043f6bf001 to your computer and use it in GitHub Desktop.
Save wch/fd82214c8b4b3eca4b5003043f6bf001 to your computer and use it in GitHub Desktop.
Script for finding R version dependency
# First, need to install crandb
# devtools::install_github('r-hub/crandb')
# Memoize crandb::package because we end up making a lot of repetitive calls.
package <- memoise::memoise(crandb::package)
# Get a list of built-in packages. We don't need to check the version
# information for these packages.
builtin_pkgs <- memoise::memoise(function() {
pkgs <- installed.packages()
pkgs <- as.data.frame(pkgs, stringsAsFactors = FALSE)
rownames(pkgs) <- NULL
builtin_pkgs <- pkgs$Package[!is.na(pkgs$Priority)]
builtin_pkgs <- c(builtin_pkgs, "R")
builtin_pkgs
})
# Find immediate dependencies (Imports, Depends) for a package
dependencies <- function(pkg) {
deps <- c(names(package(pkg)$Imports),
names(package(pkg)$Depends))
deps <- setdiff(deps, builtin_pkgs())
deps
}
# Find recursive dependencies (Imports, Depends) for a package
all_dependencies <- function(pkg) {
deps <- dependencies(pkg)
child_deps <- lapply(deps, all_dependencies)
unique(c(deps, unlist(child_deps)))
}
# Find the R version required by a package
r_version <- function(pkg) {
ver <- package(pkg)$Depends$R
if (is.null(ver)) ver <- NA_character_
ver
}
# Find R version for all (recursive) dependencies of a package
find_r_version_deps <- function(pkg) {
all_deps <- all_dependencies("shiny")
data.frame(
package = all_deps,
r_ver = vapply(all_deps, r_version, character(1)),
row.names = NULL
)
}
find_r_version_deps("shiny")
#> package r_ver
#> 1 httpuv >= 2.15.1
#> 2 mime <NA>
#> 3 jsonlite <NA>
#> 4 xtable >= 2.10.0
#> 5 digest >= 3.1.0
#> 6 htmltools >= 2.14.1
#> 7 R6 >= 3.0
#> 8 sourcetools >= 3.0.2
#> 9 later <NA>
#> 10 promises <NA>
#> 11 crayon <NA>
#> 12 rlang >= 3.2.0
#> 13 Rcpp >= 3.0.0
#> 14 magrittr <NA>
# ============================
# Carson's method
library(desc)
library(magrittr)
pkgs <- tools::package_dependencies("shiny", recursive = TRUE, which = "Imports")
dplyr::bind_rows(lapply(pkgs$shiny, function(pkg) {
desc(package = pkg)$get_deps() %>%
filter(type == "Depends", package == "R")
}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment