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

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?

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()

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.

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)

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.

You can try something like this:
package_version(R.version)
getRversion()

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

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"

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"

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)]

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)

Use the following code to obtain the version of R packages installed in the system:
installed.packages(fields = c ("Package", "Version"))

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"

Simply use help(package="my_package") and look at the version shown.
This assumes there are no other package versions in the same .libPaths.

Related

R error when installing packages (summarytools)

edit: all screenshots are displayed
I'm teaching stats in Rstudio and we're using the summarytools package. A few of my students are getting different error messages, with the same overall result that the package is not properly installed.
Alex's error seems to download, but not sure if it is installing and the library function cannot find the summarytools package.
Aroun's error is happening mid-install, something about failing to install or load a supporting package 'stringr'.
Zoey's error says something about a non-zero exit status.
While I can run stats in R, I'm a complete newb at troubleshooting package errors. Since I'm not directly experiencing these errors, it is double-difficult to troubleshoot. Any help is appreciated.
Best,
Shawn
It is difficult to judge from these errors, what actually happens. Still, my conjecture is broken or incomplete R installations.
When using R on Windows, please double-check that you installed
1) a single version of R
2) Rtools giving compilers to R
Without Rtools, R will only function in a very limited sense as it cannot compile packages and depends on binary packages that might have been compiled with different versions of R on different platforms.
If these two ideas don't solve the problem and as you are teaching, please think about using a single VM or Docker image (my approach in teaching) such that you and all your students have exactly the same software platform. With this approach, you can use a stable Linux, where R is integrated and well-tested.
Overview
I think - because without each of your student's sessionInfo() output I am only able to make recommendations based off their error messages - the following will help:
Alex: install.packages( pkgs = c("digest", "rapportools") ).
Zoey & Aroun: install.packages( pkgs = "stringr" ).
Afterwards, have all three run install.packages( pkgs = "summarytools" ).
If all else fails, you can have all three run the following commands to install the package from GitHub: install.packages("devtools") followed by devtools::install_github("dcomtois/summarytools").
Methodology
I installed summarytools with the following command install.packages( pkgs = "summarytools" ). All of your students did the same, which led me to print out my session information using sessionInfo():
R version 3.4.3 (2017-11-30)
Platform: x86_64-redhat-linux-gnu (64-bit)
Running under: Red Hat Enterprise Linux
Matrix products: default
BLAS/LAPACK: /usr/lib64/R/lib/libRblas.so
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8
[4] LC_COLLATE=en_US.UTF-8 LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] summarytools_0.8.2
loaded via a namespace (and not attached):
[1] Rcpp_0.12.16 matrixStats_0.53.1 codetools_0.2-15 digest_0.6.14 bitops_1.0-6
[6] plyr_1.8.4 magrittr_1.5 stringi_1.1.7 pryr_0.1.4 rapportools_1.0
[11] tools_3.4.3 stringr_1.3.0 pander_0.6.1 RCurl_1.95-4.10 rsconnect_0.8.8
[16] compiler_3.4.3 htmltools_0.3.6
Based on the error messages from each of your students, the installation of summarytools seems to be failing due to dependency packages (i.e. rapportools, digest, and stringr) not downloading with their download of summarytools.
This is an updated answer
For Alex's problem, I suspect the apostrophe in the path ("Alex's PC") might be the culprit.
For Zoey's and Aroun's, it's not clear, maybe some permission issues...?
Possible solutions (for all three cases)
1 - Preferably, try installing through Github:
install.packages('devtools') # if not already installed
library(devtools)
install_github("dcomtois/summarytools")
2 - If solution 1 fails or is not possible for whatever reason, install the latest binaries instead of the most recent (source) version.
install.packages('summarytools', type = 'binary')
3- If all that fails, I'd suggest trying this prior to installation as a last resort. Normally this shouldn't be necessary, but it can't hurt. After completion, try regular install or one of the two previous solutions.
install.packages(c('htmltools', 'matrixStats', 'pander', 'pryr',
'rapportools', 'RCurl', 'Hmisc', 'rstudioapi',
'rmarkdown', 'stringr'))
install.packages('knitr', dependencies = TRUE)
update.packages(ask = FALSE, repos = 'https://cran.rstudio.org')

R Error in validObject(.Object) when running as script, but not in console

The following code works fine in the R console (R 3.3.0):
m = system.file("external/pores_1.mtx", package = "Matrix")
x = Matrix::readMM(m)
I can put it in a script and it runs fine in R console as:
source("test.R")
However, when I execute it as Rscript --vanilla test.R or Rscript test.R, I get an error:
Error in validObject(.Object) :
invalid class “dgTMatrix” object: Not a valid 'Mnumeric' class object
Calls: <Anonymous> -> new -> initialize -> initialize -> validObject
Execution halted
I don't know if this is related to that specific function. I am guessing this has something to do with how Rscript works, but I used it with various other libraries and functions previously and have never seen anything like this. Any idea what is happening?
I can confirm ... I'm getting the exact same error when running a script containing a call to glmnet(). I was able to trace it back to the Matrix package, on which glmnet depends.
I back-rev'd my R version from v3.3.3 -> v3.3.2 and the error disappeared. I then checked the sessionInfo() between the two and discovered that the version of the Matrix package differed ... it is 1.2-8 (in v3.3.3) and 1.2-7.1 (in v3.3.2). To confirm, I simply replaced the "OK" version of Matrix (7.1) with the "broken" version and the error returned.
I can also confirm that explicitly loading the methods package via library(methods) resolves the error (somehow?), which explains the differing behavior between the console call and the Rscript call from the command line.
So, it appears we have 2 work-arounds:
library(methods)
back rev your version of Matrix to 1.2-7.1.
Neither is super satisfactory ... I'd just like to know what's going on with Matrix 1.2-8. Maybe it'll be bug-fixed in the next version.
If you're interested, here is my sessionInfo():
R version 3.3.3 (2017-03-06)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.2 LTS
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] 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
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets base
other attached packages:
[1] glmnet_2.0-5 foreach_1.4.3 Matrix_1.2-8
loaded via a namespace (and not attached):
[1] codetools_0.2-15 grid_3.3.3 iterators_1.0.8 methods_3.3.3
[5] lattice_0.20-35
Oh dear.
I'm pretty (not 100% !) sure that this problem should not apply in newer version of R and Matrix.
Still I would claim this is not a Matrix bug in a proper sense, rather either an 'Rscript' vs 'R' bug -- as #Stu Field mentions already; Rscript does not by default attach the methods package to the search() path, but regular R does.
OTOH, R CMD check Matrix nowadays should try to run Matrix without the methods package being in the search path, and hence the problem should not be present from Matrix 1.2-9 and newer, i.e., by default from R 3.4.0 and newer or then update the 'Matrix' package if you have an older version of R.
Again: Can you please confirm that the problem is gone with R 3.4.0 (which has Matrix 1.2-9 "along with it") ?
Here is a more useful example script, I called Rscript-tst.R.
Run as
Rscript --vanilla Rscript-tst.R > Rscript-tst.Rout 2>&1
or (as myself with many R versions installed) something like
`R-3.4.0 RHOME`/bin/Rscript --vanilla Rscript-tst.R > Rscript-tst.Rout_R340 2>&1
Rscript-tst.R :
options(echo = TRUE)# << even with "Rscript" or --slave ...
(m <- system.file("external/pores_1.mtx", package = "Matrix"))
packageDescription("Matrix")
## This *load*s the Matrix package but does not attach it to search()
str(Matrix::readMM)
sessionInfo()
x <- Matrix::readMM(m)
## used to fail because 'methods' was not "there" (in Rscript only)

Error in library(functional) : there is no package called ‘functional’ - While running MR using rmr2

I am trying to run a simple MR program using rmr2 in a single node Hadoop cluster. Here is the environment for the setup
Ubuntu 12.04 (32 bit)
R (Ubuntu comes with 2.14.1, so updated to 3.0.2)
Installed the latest rmr2 and rhdfs from here and the corresponding dependencies
Hadoop 1.2.1
Now I am trying to run a simple MR program as
Sys.setenv(HADOOP_HOME="/home/training/Installations/hadoop-1.2.1")
Sys.setenv(HADOOP_CMD="/home/training/Installations/hadoop-1.2.1/bin/hadoop")
library(rmr2)
library(rhdfs)
ints = to.dfs(1:100)
calc = mapreduce(input = ints, map = function(k, v) cbind(v, 2*v))
from.dfs(calc)
The mapreduce job fails with the below error message in hadoop-1.2.1/logs/userlogs/job_201310091055_0001/attempt_201310091055_0001_m_000000_0/stderr
Error in library(functional) : there is no package called ‘functional’
Execution halted
java.lang.RuntimeException: PipeMapRed.waitOutputThreads(): subprocess failed with code 1
at org.apache.hadoop.streaming.PipeMapRed.waitOutputThreads(PipeMapRed.java:362)
at org.apache.hadoop.streaming.PipeMapRed.mapRedFinished(PipeMapRed.java:576)
But, the sessionInfo() shows that functional package has been loaded
> sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: i686-pc-linux-gnu (32-bit)
>locale:
[1] LC_CTYPE=en_IN LC_NUMERIC=C LC_TIME=en_IN
[4] LC_COLLATE=en_IN LC_MONETARY=en_IN LC_MESSAGES=en_IN
[7] LC_PAPER=en_IN LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=en_IN LC_IDENTIFICATION=C
>attached base packages:
[1] stats graphics grDevices utils datasets methods base
>other attached packages:
[1] rhdfs_1.0.6 rJava_0.9-4 rmr2_2.3.0 reshape2_1.2.2 plyr_1.8
[6] stringr_0.6.2 **functional_0.4** digest_0.6.3 bitops_1.0-6 RJSONIO_1.0-3
[11] Rcpp_0.10.5
Update : I am able to run a R MR job reading and writing from STDIO without using the rmr2 and the rhdfs libraries as mentioned here. So, for now my guess is that the problem is isolated to rmr2 and the rhdfs packages.
How to get around this problem?
Install the dependencies for rmr2/rhdfs in a system directory instead of a custom directory (~/R/x86_64-pc-linux-gnu-library/3.0). This can be done running R as sudo and then installing the dependencies. Thanks to Antonio for the help in the RHadoop forums.
The most common solution of these kind of problem is re-installation since in sesssionInfo()
you are getting
**functional_0.4**
while when i did sessionInfo() i got
functional_0.4
i guess there is some missing dependencies you might be missing so use from your R console
install.packages("functional",dependencies="TRUE")
to fix any problem due to any other packages .
P.S: Choose cloud-0 mirror from the available ones.
If still that does not help i recommend you use r-base-dev as your R version though i don't have a reason to justify this using http://cran.r-project.org/bin/linux/ubuntu/README
sudo apt-get install r-base-dev
Thanks

R C symbol name "do_is_ordered" not in the DLL for package xts

I have R installed on my computer in the office and I have experienced something weird. I was trying to use the xts package but the following is happening:
library(xts)
data(sample_matrix)
sample.xts <- as.xts(sample_matrix, descr='my new xts object')
Fehler in .Call("do_is_ordered", x = x, increasing = as.logical(increasing), :
C Symbolname "do_is_ordered" nicht in der DLL für Paket "xts"
I am sorry for the German text but I did not find the equivalent in English, the statement is saying something like C symbol name "do_is_ordered" is not in the DLL for package "xts".
Here is my session info:
> sessionInfo()
R version 2.13.2 (2011-09-30)
Platform: i386-pc-mingw32/i386 (32-bit)
locale:
[1] LC_COLLATE=German_Austria.1252 LC_CTYPE=German_Austria.1252
[3] LC_MONETARY=German_Austria.1252 LC_NUMERIC=C
[5] LC_TIME=German_Austria.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] PerformanceAnalytics_1.0.3.2 xlsReadWrite_1.5.4
[3] quantmod_0.3-15 TTR_0.20-2
[5] Defaults_1.1-1 xts_0.7-5
[7] zoo_1.6-4
loaded via a namespace (and not attached):
[1] grid_2.13.2 lattice_0.19-33 tools_2.13.2
As I have mentioned before, it is a computer in the office which has restricted access to the internet, all the packages are installed from the local folder and I can not simply reinstall the version of R as I would wish the most. It is weird as other computers in the office dont produce any errors with the same code. I would like to ask you what could produce the error (wrong installation?) resp. how could I fix it without necessity of reinstalling R (which is not as simply as one would expect, especially with the IT service behind my back :-))
I faced the same error and refer to Xts conversion fails on update from xts 0.9.7 to 0.10.0, reinstalled devtools::install_github("joshuaulrich/xts") is work for me.

R Rmetrics packages

I ´ve tried to install some packages from Rmetrics, but it looks I am doing something wrong.
For
https://r-forge.r-project.org/scm/viewvc.php/pkg/fPortfolioBacktest/?logsort=cvs&root=rmetrics&pathrev=4948
I simply used
install.packages("fPortfolioBacktest", repos="http://R-Forge.R-project.org")
but
> install.packages("fPortfolioBacktest", repos="http://R-Forge.R-project.org")
Warning: unable to access index for repository http://R-Forge.R-project.org/bin/windows/contrib/2.12
Warning message:
In getDependencies(pkgs, dependencies, available, lib) :
package ‘fPortfolioBacktest’ is not available
Exactly the same problem is with
https://r-forge.r-project.org/scm/viewvc.php/pkg/Rsocp/?root=rmetrics&pathrev=3690
> sessionInfo()
R version 2.12.0 (2010-10-15)
Platform: i386-pc-mingw32/i386 (32-bit)
locale:
[1] LC_COLLATE=Slovak_Slovakia.1250 LC_CTYPE=Slovak_Slovakia.1250 LC_MONETARY=Slovak_Slovakia.1250 LC_NUMERIC=C
[5] LC_TIME=Slovak_Slovakia.1250
attached base packages:
[1] splines stats graphics grDevices utils datasets methods base
other attached packages:
[1] corpcor_1.5.7 fPortfolio_2130.80 fAssets_2100.78 fCopulae_2110.78 sn_0.4-16
[6] mnormt_1.4-3 robustbase_0.7-0 xlsx_0.3.0 xlsxjars_0.3.0 rJava_0.8-8
[11] ttrTests_1.5 PerformanceAnalytics_1.0.3.2 fTrading_2110.77 fBasics_2110.80 timeSeries_2130.90
[16] timeDate_2130.91 dynlm_0.3-0 car_2.0-8 survival_2.36-3 nnet_7.3-1
[21] MASS_7.3-8 lmtest_0.9-27 tseries_0.10-24 quadprog_1.5-3 quantmod_0.3-15
[26] TTR_0.20-2 xts_0.8-0 zoo_1.6-4 Defaults_1.1-1
loaded via a namespace (and not attached):
[1] grid_2.12.0 lattice_0.19-13 Rglpk_0.3-5 slam_0.1-22 strucchange_1.4-2 tools_2.12.0
Is there a fast way how to install them with all the dependencies? I am using Windows XP
Thanks,
Alex
Thanks for adding the error and your sessionInfo.
As I said in my comment, you're running an old version of R (2.12.0). R-forge only builds binaries for the most recent major revision (2.13.X). You need to either upgrade R or build from source. To build from source on Windows, you will need the Windows toolset.

Resources