557

I am in a process of figuring out how to use my university cluster. It has 2 versions of R installed. System wide R 2.11 (Debian 6.0) and R 2.14.2 in non-standard location.

I am trying to use MPI together with snow. The code I am trying to run is the following

library(snow)
library(Rmpi)
cl <- makeMPIcluster(mpi.universe.size()-1)
stopCluster(cl)
mpi.quit()

It works without the problems on R 2.11. (I launch the script with mpirun -H localhost,n1,n2,n3,n4 -n 1 R --slave -f code.R). Now when I try to do it with R 2.14.2, I get the following message:

Error: This is R 2.11.1, package 'snow' needs >= 2.12.1
In addition: Warning message:

So it seems that R loads the package snow version compiled for R 2.11. I've installed snow under R 2.14 into my home folder and I added the following lines to my code:

.libPaths("/soft/R/lib/R/library")
.libPaths("~/R/x86_64-pc-linux-gnu-library/2.11")
print(.libPaths())
print(sessionInfo())
print(version)

And the output before the error confirms that I am indeed running R 2.14.2 and my R packages folder is first in search path. But I still get the error.

So my question is how do I determine which version of package is loaded in R? I can see with installed.packages all the packages which are installed, so maybe there is some function which lists similar information for loaded packages?

epo3
  • 2,991
  • 2
  • 33
  • 60
mpiktas
  • 11,258
  • 7
  • 44
  • 57
  • 2
    did you find a good solution for this issue? In my experience and as the R help indicates, both sessionInfo and packageVersion return the _the current version installed at the location the package was loaded from: it can be wrong if another process has been changing packages during the session._ – RockScience Nov 09 '15 at 04:02

13 Answers13

720

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()
fdetsch
  • 5,239
  • 3
  • 30
  • 58
Justin
  • 42,475
  • 9
  • 93
  • 111
  • 1
    Thanks. My mistake was to output sessionInfo before the package loading. In the end it turned out that the correct version of package was loaded, but R still complained about the old version. Installed my own local version of R and everything worked like a charm. – mpiktas Jun 21 '12 at 06:54
  • 74
    TMI! `packageVersion()` is much better in most cases – Louis Maddox May 02 '15 at 17:42
  • 1
    I would not advise to use `sessionInfo`. See the Note of ?sessionInfo: _"The information on ‘loaded’ packages and namespaces is the current version installed at the location the package was loaded from: it can be wrong if another process has been changing packages during the session."_ So: if you want to know wether the package is loaded or not, better use `"Rmpi" %in% loadedNamespaces()` ; if you want to know which version is installed in a specific location, better use `packageVersion(lib.loc = ...)` – RockScience Nov 06 '15 at 06:41
  • See Gábor's answer below for an answer that returns the version of a **currently loaded package** (which may be different from the on-disk version): https://stackoverflow.com/a/37369263/946850 – krlmlr May 20 '19 at 08:50
355

You can use utils::packageVersion to see what version of a package is installed:

> packageVersion("snow")
[1] ‘0.3.9’

Note that

A package will not be ‘found’ unless it has a DESCRIPTION file which contains a valid Version field. Different warnings are given when no package directory is found and when there is a suitable directory but no valid DESCRIPTION file.

Although it sounds like you want to see what version of R you are running, in which case @Justin's sessionInfo suggestion is the way to go.

Salim B
  • 2,409
  • 21
  • 32
GSee
  • 48,880
  • 13
  • 125
  • 145
42

Technically speaking, all of the answers at this time are wrong. packageVersion does not return the version of the loaded package. It goes to the disk, and fetches the package version from there.

This will not make a difference in most cases, but sometimes it does. As far as I can tell, the only way to get the version of a loaded package is the rather hackish:

asNamespace(pkg)$`.__NAMESPACE__.`$spec[["version"]]

where pkg is the package name.

EDIT: I am not sure when this function was added, but you can also use getNamespaceVersion, this is cleaner:

getNamespaceVersion(pkg)
Gabor Csardi
  • 10,705
  • 1
  • 36
  • 53
  • 1
    `\`:::\`(pkg, .__NAMESPACE__.)$spec[["version"]]` is a synonym of the `asNamespace()` method of getting the package version. – seasmith Dec 24 '16 at 05:49
  • 3
    This answer is so important. `packageVersion()` only shows you the top result in `installed.packages()` but if you have multiple version of the same package, and you load one specifically, it won't give you the right answer. – calico_ Jun 14 '17 at 23:54
  • 1
    Per https://hughjonesd.shinyapps.io/rcheology/ it's been available since available as early as R 1.7.0. – krlmlr May 20 '19 at 08:49
  • 1
    It seems that `packageVersion` does return the version of the loaded package by default. The docs for the `lib.loc` argument read: "The default value of `NULL` corresponds to all libraries currently known. If the default is used, the _loaded packages_ and namespaces are searched before the libraries." Emphasis mine. – dpprdan Dec 15 '20 at 19:48
  • 1
    @dpprdan this is new and was not the case when I wrote my answer. – Gabor Csardi Aug 19 '22 at 14:18
  • Another thing to note is that `getNamespaceVersion()` returns a character, not a `package_version()` object. – t-kalinowski Aug 28 '22 at 14:59
25

To check the version of R execute : R --version

Or after you are in the R shell print the contents of version$version.string

EDIT

To check the version of installed packages do the following.

After loading the library, you can execute sessionInfo ()

But to know the list of all installed packages:

packinfo <- installed.packages(fields = c("Package", "Version"))
packinfo[,c("Package", "Version")]

OR to extract a specific library version, once you have extracted the information using the installed.package function as above just use the name of the package in the first dimension of the matrix.

packinfo["RANN",c("Package", "Version")]
packinfo["graphics",c("Package", "Version")]

The above will print the versions of the RANN library and the graphics library.

zx8754
  • 52,746
  • 12
  • 114
  • 209
phoxis
  • 60,131
  • 14
  • 81
  • 117
21

You can try something like this:

  1. package_version(R.version)

  2. getRversion()

user1317221_G
  • 15,087
  • 3
  • 52
  • 78
Sathish
  • 12,453
  • 3
  • 41
  • 59
15

GUI solution:

If you are using RStudio then you can check the package version in the Packages pane.

enter image description here

epo3
  • 2,991
  • 2
  • 33
  • 60
8

Use the R method packageDescription to get the installed package description and for version just use $Version as:

packageDescription("AppliedPredictiveModeling")$Version
[1] "1.1-6"
krishna Prasad
  • 3,541
  • 1
  • 34
  • 44
  • 1
    There's a convenience function `utils::packageVersion()` that directly parses the `Version` field, see https://stackoverflow.com/a/11103291/7196903 – Salim B Sep 06 '21 at 16:58
5

Based on the previous answers, here is a simple alternative way of printing the R-version, followed by the name and version of each package loaded in the namespace. It works in the Jupyter notebook, where I had troubles running sessionInfo() and R --version.

print(paste("R", getRversion()))
print("-------------")
for (package_name in sort(loadedNamespaces())) {
    print(paste(package_name, packageVersion(package_name)))
}

Out:

[1] "R 3.2.2"
[1] "-------------"
[1] "AnnotationDbi 1.32.2"
[1] "Biobase 2.30.0"
[1] "BiocGenerics 0.16.1"
[1] "BiocParallel 1.4.3"
[1] "DBI 0.3.1"
[1] "DESeq2 1.10.0"
[1] "Formula 1.2.1"
[1] "GenomeInfoDb 1.6.1"
[1] "GenomicRanges 1.22.3"
[1] "Hmisc 3.17.0"
[1] "IRanges 2.4.6"
[1] "IRdisplay 0.3"
[1] "IRkernel 0.5"
joelostblom
  • 43,590
  • 17
  • 150
  • 159
5

Old question, but not among the answers is my favorite command to get a quick and short overview of all loaded packages:

(.packages())

To see which version is installed of all loaded packages, just use the above command to subset installed.packages().

installed.packages()[(.packages()),3]

By changing the column number (3 for package version) you can get any other information stored in installed.packages() in an easy-to-read matrix. Below is an example for version number and dependency:

installed.packages()[(.packages()),c(3,5)]
TimTeaFan
  • 17,549
  • 4
  • 18
  • 39
2

Search() can give a more simplified list of the attached packages in a session (i.e., without the detailed info given by sessionInfo())

search {base}- R Documentation
Description: Gives a list of attached packages. Search()

search()
#[1] ".GlobalEnv"        "package:Rfacebook" "package:httpuv"   
#"package:rjson"    
#[5] "package:httr"      "package:bindrcpp"  "package:forcats"   # 
#"package:stringr"  
#[9] "package:dplyr"     "package:purrr"     "package:readr"     
#"package:tidyr"    
#[13] "package:tibble"    "package:ggplot2"   "package:tidyverse" 
#"tools:rstudio"    
#[17] "package:stats"     "package:graphics"  "package:grDevices" 
#"package:utils"    
#[21] "package:datasets"  "package:methods"   "Autoloads"         
#"package:base"
  • 1
    Yes, but sessionInfo gives the version number too. In my case the latter is usually more important. – mpiktas Jan 30 '18 at 05:08
2

To add on @GSee's answer, note that the returned value of utils::packageVersion() is not a character and that you can perfectly use it to write conditions:

packageVersion("dplyr")
#> [1] '1.0.7'
packageVersion("dplyr")>1
#> [1] TRUE
packageVersion("dplyr")>'1.0'
#> [1] TRUE
packageVersion("dplyr")>'1.1'
#> [1] FALSE

Created on 2021-08-23 by the reprex package (v2.0.0)

Dan Chaltiel
  • 7,811
  • 5
  • 47
  • 92
1

Use the following code to obtain the version of R packages installed in the system:

installed.packages(fields = c ("Package", "Version"))
josliber
  • 43,891
  • 12
  • 98
  • 133
Anjana
  • 11
  • 1
1

Simply use help(package="my_package") and look at the version shown.

This assumes there are no other package versions in the same .libPaths.

NelsonGon
  • 13,015
  • 7
  • 27
  • 57