How to Find Out Which Package Version Is Loaded in R

How to find out which package version is loaded in R?

You can use sessionInfo() to accomplish that.

> sessionInfo()
R version 2.15.0 (2012-03-30)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 LC_PAPER=C LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] graphics grDevices utils datasets stats grid methods base

other attached packages:
[1] ggplot2_0.9.0 reshape2_1.2.1 plyr_1.7.1

loaded via a namespace (and not attached):
[1] colorspace_1.1-1 dichromat_1.2-4 digest_0.5.2 MASS_7.3-18 memoise_0.1 munsell_0.3
[7] proto_0.3-9.2 RColorBrewer_1.0-5 scales_0.2.0 stringr_0.6
>

However, as per comments and the answer below, there are better options

> packageVersion("snow")

[1] ‘0.3.9’

Or:

"Rmpi" %in% loadedNamespaces()

How to find the R version under which an R package was built?

The information you want is in the last column ("Built") of the output of installed.packages(), per https://stat.ethz.ch/R-manual/R-devel/library/utils/html/installed.packages.html.

.libPaths() # get the library location
installed.packages(lib.loc = "C://Revolution//R-Enterprise-7.3//R-3.1.1//library")

Know which (exact) R version a package was built under?

It almost never matters which version built a package. The only thing that usually matters is which version installed it. (Binary packages are images of installed packages, so it matters for them.)

The version that installed a package is stored in the Built: field in the DESCRIPTION file. (Yes, "Built", not "Installed".)
You can see it using code like

read.dcf(system.file("DESCRIPTION", package="base"), fields="Built")
#> Built
#> [1,] "R 3.6.1; ; 2019-07-06 02:01:41 UTC; unix"

Put in your own package name instead of "base".

The exception for the "almost never matters" is on those rare occasions when the package format changes.

R - how to identify which version (package) of a function is active/attached?

You can find out which functions are in conflict (being masked) by using conflicts(detail = TRUE). This returns a named list of packages / functions in conflict in the order of the search() path which is the order in which they will be called.

As an example, we can load dplyr which loads some functions that conflict with base.

library(dplyr)

# Create data.frame of conflicts and clean up.
conf <- conflicts(detail = TRUE)
conf.df <- data.frame(do.call(rbind, Map(cbind, conf, names(conf))))
names(conf.df) <- c("fn", "package")
conf.df$package <- sub("package:", "", conf.df$package)

# Aggregate packages by function - first package is the default when called.
aggregate(package ~ fn, conf.df, toString)

fn package
1 body<- methods, base
2 filter dplyr, stats
3 intersect dplyr, base
4 kronecker methods, base
5 lag dplyr, stats
6 setdiff dplyr, base
7 setequal dplyr, base
8 union dplyr, base

Function to automatically check correct R package version is installed

Checking versions for a specific package

You can access the package version through. An example for the abind package:

packageDescription("abind")$Version
# [1] "1.4-3"

Checking versions of all installed packages

installed.packages()[,c("Package", "Built")] 
# Gives an overview of all installed packages and the versions of those packages.
# Package Built
# abind "abind" "3.2.3"
# BradleyTerry2 "BradleyTerry2" "3.2.3"
# brglm "brglm" "3.2.3"
# car "car" "3.2.4"
# caret "caret" "3.2.4"
# colorspace "colorspace" "3.2.4"
# devtools "devtools" "3.2.5"
# (...)

Checking current version of R

sessionInfo()$R.version$version.string
# [1] "R version 3.2.3 (2015-12-10)"

Is there a file on R projects where I can find out which packages and which versions are installed?

I achieved that using renv

  1. renv::init()
  2. renv::settings$snapshot.type("all") # if you want to list all
  3. renv::snapshot()

Then the renv.lock file will be updated with the state of the project libraries where you can find all libraries listed. Something similar to this:

{
"R": {
"Version": "4.0.3",
"Repositories": [
{
"Name": "CRAN",
"URL": "https://cran.rstudio.com"
}
]
},
"Packages": {
"KernSmooth": {
"Package": "KernSmooth",
"Version": "2.23-17",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "bbff70c8c0357b5b88238c83f680fcd3"
},
"MASS": {
"Package": "MASS",
"Version": "7.3-53",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "d1bc1c8e9c0ace57ec9ffea01021d45f"
},
"Matrix": {
"Package": "Matrix",
"Version": "1.2-18",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "08588806cba69f04797dab50627428ed"
},
"R6": {
"Package": "R6",
"Version": "2.5.0",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "b203113193e70978a696b2809525649d"
},
"Rcpp": {
"Package": "Rcpp",
"Version": "1.0.5",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "125dc7a0ed375eb68c0ce533b48d291f"
},
"assertthat": {
"Package": "assertthat",
"Version": "0.2.1",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "50c838a310445e954bc13f26f26a6ecf"
},
"boot": {
"Package": "boot",
"Version": "1.3-25",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "bd51734a754b6c2baf28b2d1ebc11e91"
},
"class": {
"Package": "class",
"Version": "7.3-17",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "9267f5dab59a4ef44229858a142bded1"
},
"cli": {
"Package": "cli",
"Version": "2.2.0",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "3ef298932294b775fa0a3eeaa3a645b0"
},
"cluster": {
"Package": "cluster",
"Version": "2.1.0",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "db63a44aab5aadcb6bf2f129751d129a"
},
"codetools": {
"Package": "codetools",
"Version": "0.2-16",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "89cf4b8207269ccf82fbeb6473fd662b"
},
"crayon": {
"Package": "crayon",
"Version": "1.3.4",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "0d57bc8e27b7ba9e45dba825ebc0de6b"
},
"digest": {
"Package": "digest",
"Version": "0.6.27",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "a0cbe758a531d054b537d16dff4d58a1"
},
"dplyr": {
"Package": "dplyr",
"Version": "1.0.2",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "d0509913b27ea898189ee664b6030dc2"
},
"ellipsis": {
"Package": "ellipsis",
"Version": "0.3.1",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "fd2844b3a43ae2d27e70ece2df1b4e2a"
},
"fansi": {
"Package": "fansi",
"Version": "0.4.1",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "7fce217eaaf8016e72065e85c73027b5"
},
"foreign": {
"Package": "foreign",
"Version": "0.8-80",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "ae1b1e15cc6ccb2bc61c0ac33e86d35f"
},
"generics": {
"Package": "generics",
"Version": "0.1.0",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "4d243a9c10b00589889fe32314ffd902"
},
"glue": {
"Package": "glue",
"Version": "1.4.2",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "6efd734b14c6471cfe443345f3e35e29"
},
"lattice": {
"Package": "lattice",
"Version": "0.20-41",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "fbd9285028b0263d76d18c95ae51a53d"
},
"lifecycle": {
"Package": "lifecycle",
"Version": "0.2.0",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "361811f31f71f8a617a9a68bf63f1f42"
},
"magrittr": {
"Package": "magrittr",
"Version": "2.0.1",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "41287f1ac7d28a92f0a286ed507928d3"
},
"mgcv": {
"Package": "mgcv",
"Version": "1.8-33",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "eb7b6439bc6d812eed2cddba5edc6be3"
},
"nlme": {
"Package": "nlme",
"Version": "3.1-149",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "7c24ab3a1e3afe50388eb2d893aab255"
},
"nnet": {
"Package": "nnet",
"Version": "7.3-14",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "0d87e50e11394a7151a28873637d799a"
},
"pillar": {
"Package": "pillar",
"Version": "1.4.7",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "3b3dd89b2ee115a8b54e93a34cd546b4"
},
"pkgconfig": {
"Package": "pkgconfig",
"Version": "2.0.3",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "01f28d4278f15c76cddbea05899c5d6f"
},
"plyr": {
"Package": "plyr",
"Version": "1.8.6",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "ec0e5ab4e5f851f6ef32cd1d1984957f"
},
"purrr": {
"Package": "purrr",
"Version": "0.3.4",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "97def703420c8ab10d8f0e6c72101e02"
},
"remotes": {
"Package": "remotes",
"Version": "2.2.0",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "430a0908aee75b1fcba0e62857cab0ce"
},
"renv": {
"Package": "renv",
"Version": "0.12.3-8",
"Source": "GitHub",
"RemoteType": "github",
"RemoteHost": "api.github.com",
"RemoteRepo": "renv",
"RemoteUsername": "rstudio",
"RemoteRef": "HEAD",
"RemoteSha": "080234134766804fb1dbbd777dff77dffff4b2e6",
"Hash": "cf80821fca4292cf8af159b24d422536"
},
"rlang": {
"Package": "rlang",
"Version": "0.4.9",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "9d7aba7bed9a79e2403b4777428a2b12"
},
"rpart": {
"Package": "rpart",
"Version": "4.1-15",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "9787c1fcb680e655d062e7611cadf78e"
},
"spatial": {
"Package": "spatial",
"Version": "7.3-12",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "58a02ce0150652b96c044bc67a0df2e5"
},
"survival": {
"Package": "survival",
"Version": "3.2-7",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "39c4ac6d22dad33db0ee37b40810ea12"
},
"tibble": {
"Package": "tibble",
"Version": "3.0.4",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "71dffd8544691c520dd8e41ed2d7e070"
},
"tidyselect": {
"Package": "tidyselect",
"Version": "1.1.0",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "6ea435c354e8448819627cf686f66e0a"
},
"utf8": {
"Package": "utf8",
"Version": "1.1.4",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "4a5081acfb7b81a572e4384a7aaf2af1"
},
"vctrs": {
"Package": "vctrs",
"Version": "0.3.5",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "d25c5bea636cf892edbfd64fc3d20c20"
}
}
}

r check if package version is greater than x.y.z

While utils::compareVersion() is fine, I would say that using packageVersion() with comparison operators (as indicated by @G5W in comments) is simpler:

packageVersion("data.table")
[1] ‘1.10.0’
> packageVersion("data.table")>"1.9.8"
[1] TRUE
> packageVersion("data.table")>"1.10.01"
[1] FALSE
> packageVersion("data.table")=="1.10.0"
[1] TRUE

This is illustrated in the examples for ?packageVersion; the ability to use comparison operators in this way is explicitly documented in ?package_version:

Functions numeric_version, package_version and
R_system_version create a representation from such strings (if
suitable) which allows for coercion and testing, combination,
comparison, summaries (min/max), inclusion in data frames,
subscripting, and printing. The classes can hold a vector of such
representations.

How to find how a package was loaded in R?

I'm not an R-internals or R-Core guy, so some of this is speculation and understanding on my part.

If a package imports functions from another package, either one-by-one or the whole other-package, then those functions should not be inserted into your search path. For example, dplyr is a fairly complex package that imports from several other packages and optionally re-exports some of them. For instance, from its NAMESPACE:

importFrom(R6,R6Class)

but if you library(dplyr) and then type in R6Class, it reports Error: object 'R6Class' not found. However, it is visible to dplyr functions:

> R6Class
Error: object 'R6Class' not found
> debug(dplyr::mutate)
> mutate(mtcars, cyl = 5)
debugging in: mutate(mtcars, cyl = 5)
debug: {
UseMethod("mutate")
}
Browse[2]> R6Class
function (classname = NULL, public = list(), private = NULL,
active = NULL, inherit = NULL, lock_objects = TRUE, class = TRUE,
portable = TRUE, lock_class = FALSE, cloneable = TRUE, parent_env = parent.frame(),
lock)
{
...

This works now because the search path within dplyr::mutate is from dplyr's perspective, not the user's perspective.

Combine with this my doubt (though not certainty) that those packages would call library(data.table), importing the package into your search path.

More than likely, there is a package imported by one of the packages you just listed (a second-generation dependency-import, I guess) that is improperly referencing wday by itself, and it just starts working when some higher package is properly loaded, bringing that function into its effective search path.

I suggest two ways to find what is going wrong:

  1. When you see an error, run traceback() and look at the stack of function calls; it'll take some sleuthing, but find where that wday is being called, find that function in the packages (both exported and internal functions!), and go from there.

  2. If all packages are being thorough in announcing packages they import (via Depends:, Imports:, or perhaps even a mis-used Suggests:), then you can hunt down where nested dependencies go with something like this.

    pkgs <- trimws(unlist(strsplit(gsub("library\\(([^)]*)\\)", "\\1", "library(qmao);library(chron);library(tseries);library(iterators);library(erer);
    library(corpcor); library(zoo); library(xts); library(quantmod);
    library(TTR); library(graphics); library(ggplot2); library(gsee);
    library(tseries); library(quantstrat); library(plyr); library(caTools);
    library(zoo); library(chron); library(gtools); library(microbenchmark);
    library(benchmark); library(rbenchmark); library(utils); library(Rcpp);
    library(RcppXts); library(RcppArmadillo); library(gtools); library(rcppbugs);
    library(RcppClassic); library(RcppStreams); library(inline); library(RcppEigen);
    library(RcppParallel); library(RcppProgress); library(doParallel); library(parallel);
    library(foreach); library(doMC); library(doSNOW); library(fGarch); library(FitAR);
    library(fUnitRoots); library(dplyr);"), ";")))
    # just so I can search locally on mine without all of those packages
    inst_pkgs <- installed.packages()
    pkgs <- intersect(pkgs, inst_pkgs[,1])

    # inexact but "good enough" for now
    possibles <- Filter(function(a) any(grepl("data.table|lubridate", a)),
    sapply(pkgs, function(p) unlist(packageDescription(p)[c("Depends","Imports","Suggests")])))
    names(possibles)
    # [1] "dplyr"

    You can find more info by looking at the full details of that package:

    possibles[[1]]["Suggests"]
    # Suggests
    # "bit64 (>= 0.9.7), callr (>= 3.1.1), covr (>= 3.0.1), DBI (>=\n0.7.14), dbplyr (>= 1.2.0), dtplyr (>= 0.0.2), ggplot2 (>=\n2.2.1), hms (>= 0.4.1), knitr (>= 1.19), Lahman (>= 3.0-1),\nlubridate (>= 1.7.4), MASS, mgcv (>= 1.8.23), microbenchmark\n(>= 1.4.4), nycflights13 (>= 0.2.2), rmarkdown (>= 1.8), RMySQL\n(>= 0.10.13), RPostgreSQL (>= 0.6.2), RSQLite (>= 2.0),\ntestthat (>= 2.0.0), withr (>= 2.1.1), broom (>= 0.5.1), purrr\n(>= 0.3.0), readr (>= 1.3.1), crayon (>= 1.3.4)"


Related Topics



Leave a reply



Submit