Skip to content

Instantly share code, notes, and snippets.

@mojaveazure
Created September 4, 2020 18:30
Show Gist options
  • Save mojaveazure/7b6f4017f440387704b55344576b3f91 to your computer and use it in GitHub Desktop.
Save mojaveazure/7b6f4017f440387704b55344576b3f91 to your computer and use it in GitHub Desktop.
Minimal R Version
#!/usr/bin/env Rscript
#' Minimal R version
#'
#' Find the lowest accepted R version for a package and all of its dependencies
#'
#' @param package Name of CRAN package
#'
#' @return A \code{\link[base]{numeric_version}} with the lowest possible R
#' version to install \code{package} and all of its dependencies
#'
#' @export
#'
#' @examples
#' MinimalRVersion("Seurat")
#'
MinimalRVersion <- function(package) {
db <- utils::available.packages()
deps <- unlist(
x = tools::package_dependencies(
packages = package,
db = db,
recursive = TRUE
),
use.names = FALSE
)
deps <- c(package, intersect(x = deps, y = rownames(x = db)))
db <- db[deps, c('Depends', 'Imports', 'LinkingTo')]
rversions <- apply(
X = db,
MARGIN = 1,
FUN = function(row) {
row <- trimws(x = unlist(
x = strsplit(x = row, split = ','),
use.names = FALSE
))
row <- grep(pattern = '^R \\(', x = row, value = TRUE)
if (!length(x = row)) {
return(NULL)
}
version <- unlist(x = regmatches(
x = row,
m = regexec(pattern = '\\d+\\.\\d+\\.\\d+', text = row)
))
if (!length(x = version)) {
version <- NULL
}
return(version)
}
)
rversions <- unlist(
x = Filter(f = Negate(f = is.null), x = rversions),
use.names = FALSE
)
rversions <- lapply(
X = rversions,
FUN = numeric_version
)
return(Reduce(f = max, x = rversions))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment