Last active
July 5, 2018 11:42
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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