Skip to content

Instantly share code, notes, and snippets.

@wpetry
Last active July 5, 2018 11:42
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 wpetry/e60978044b804d8d90296cfdb1ce7384 to your computer and use it in GitHub Desktop.
Save wpetry/e60978044b804d8d90296cfdb1ce7384 to your computer and use it in GitHub Desktop.
function that reports the names and version numbers of packages in an R workspace
#' Retrieve attached or loaded package names and their version numbers
#'
#' @param L an object of class 'sessionInfo'. Default is to retrieve the current
#' workspace session information.
#' @param n character specifying whether to use attached packages ('otherPkgs') or
#' packages only loaded via a namespace ('loadedOnly'). Base packages are always
#' omitted.
#'
#' @return a data frame with two columns: the package name and the package version number.
#' @export
#'
#' @examples
#' get_pkg_versions()
#' get_pkg_versions()
get_pkg_versions <- function(L = sessionInfo(), n = c("basePkgs", "otherPkgs", "loadedOnly")) {
n <- match.arg(n)
if (n == "basePkgs") {
data.frame(name = L[[n]], version = with(R.version, paste(major, minor, sep = ".")))
} else {
vers <- unname(sapply(L[[n]], function(x) x[["Version"]]))
pkg <- unname(sapply(L[[n]], function(x) x[["Package"]]))
data.frame(name = pkg, version = vers)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment