R Packages Management - gizotso/R GitHub Wiki
Packages
-
search()
: list of attached packages
library(help = splines)
: documentation on package 'splines'
library(stats)
: load package stats
detach("package:devtools", unload=TRUE)
! unload a package
When you install a package, and both the global environment (that is, your workspace or current R session), and the installed package, have a function with the same name, R will ?mask? the function and use the function in the highest-level environment.
Load a Package
library(tools)
# attach(tools)
# if package is not in the Lib Search path
library("zoo", lib.loc="C:/Tools/R/RLibs")
require(splines) # package
detach(package:splines)
# or also
library(splines)
pkg <- "package:splines"
detach(pkg, character.only = TRUE)
library("stats4") # loads the package "stats4"
library(h=stats4) # gives help for all functions
data(package=”stats4”) # gives the list of all available datasets
detach("package:pretty") #detache a package from the current environment
R Repositories
Repositories are listed under the config file %R_HOME%/etc/repositories Rgui() : choose repositories (CRAN + CRAN extra by default)
setRepositories()
- Check the selected repo :
getOption("repos")
- R repos
R CRAN : The Comprehensive R Archive Network
This is the main repository for R
CRAN mirror can be selected in Rgui : "Choose Cran Mirror"
chooseCRANmirror()
chooseCRANmirror(graphics=FALSE)
Or it can be set in cmd line or at startup
# set a CRAN mirror at startup
# https://www.r-bloggers.com/permanently-setting-the-cran-repository/
local({r <- getOption("repos")
r["CRAN"] <- "https://cloud.r-project.org"
options(repos=r)
})
Package Management
This is the most epic stuff in R as package are actively developed and updated by a large community. Main issues
- Some packages are abandoned and removed from the CRAN repo, thus even if running the old R version that was working fine with the package you will not be able to install it easily (std commands)
- new R versions : when upgrading to a major R version (R 3.4, 3.5, 3.6, ...) packages should be installed again and need to be compiled against the correct R core (3.4).
- In some cases we can also need to stick to a specific version because of constraints (production server, cloud service: Power BI service still running 3.4.4 as of 2020-03-14, ...) but when installing a old R version
Installed Packages (R libraries)
# list installed packages
installed.packages()
# listing of all available packages
dir(.libPaths())
# listing attached packages
search()
ip <- installed.packages(fields = c ("Package", "Version"))
ip[, c("Package", "Version")]
ip["graphics", c("Package", "Version")]
ip = installed.packages()[,1-6]
ip = as.data.frame(installed.packages()[,c(1,3:6)])
# Default : base and recommanded packages
ip_R_default <- ip[!is.na(ip$Priority), 1:3, drop=FALSE]
ip = as.data.frame(installed.packages())
# colnames(ip) to see all available columns
# package, LibPath, Version, Priority, Depends, Imports, ...
ip = as.data.frame(installed.packages()[,c(1,3:4)])
rownames(ip) <- NULL
# packages that comes with R
ip_default <- ip[!is.na(ip$Priority),1:3,drop=FALSE]
# ignoring packages that comes with R (priority = base/recommended/...)
ip <- ip[is.na(ip$Priority),1:2,drop=FALSE]
print(ip, row.names=FALSE)
# open a window with package listing by library (all packages available)
library()
library(lib.loc = .Library)
# Packages in library 'C:/Tools/R/R-3.2.5/library':
# ...
Package Version and Description (on installed packages)
packageVersion("tools")
packageDescription("stats")
packageDescription("stats", fields = c("Package", "Version"))
packageDescription("stats", fields = "Version")
packageDescription("stats", fields = "Version", drop = FALSE)
Checking Dependencies
library(tools)
# check dependencies are installed
package_dependencies('Hmisc')
ip<-installed.packages()
ip["latticeExtra", c("Package", "Version", "Depends", "Imports")]
# deprecated function in devtools
library(devtools)
package_deps("devtools")
Available Packages and its attributes (version, depends, ...) (CRAN mirror)
packageStatus()
# available packages : package, Version, Priority, Depends, Imports, ...
ap <- as.data.frame(available.packages())
ap["devtools",c("Version","Depends","Imports","Suggests")]
ap["Hmisc",]$Depends
my_package_list=c("devtools", "Hmisc", "reshape2")
my_package_list_depends = ap[my_package_list, c("Version","Depends","Imports","Suggests")]
ap <- available.packages()
ap["ggplot2","Depends"]
# check available packages in list
ap = available.packages()
ap = ap[ap[, "Package"] %in% nip, c(1:5)]
Installing Packages
Note : Depending on install date and order, dependencies installed will not be the same for a given package. Install can be made from Rstudio or from Rgui (menu/cmds). Ih having issues in RStudio, check if it works in RGui.
Install in a specific R library (usefull to keep separate default packages from additionnal and also when not having root access / Run as admin).
Default packages are under .Library
-> $R_HOME/library
-
set R_LIBS package to point to the desired location. This will be default location where packages will be installed.
-
add this library to search path :
.libPaths(Sys.getenv('R_LIBS'))
. This can be done at R startup (.Renviron or .Rprofile in home dir). -
R_LIBS_USER, R_LIBS_SITE (.Library.site)
-
https://support.rstudio.com/hc/en-us/articles/219949047-Installing-older-versions-of-packages
-
https://stackoverflow.com/questions/1474081/how-do-i-install-an-r-package-from-source
-
Package Manager : PacMan https://cran.r-project.org/web/packages/pacman/vignettes/Introduction_to_pacman.html
Install packages
The normal method to install packages is to use the install.packages()
function. This will either lookup for an existing binary or download an archive (source) and build the package.
CRAN only provides binaries for one version of R prior to the current one
install.packages("ggplot2")
# install under a given lib
install.packages("ggplot2", lib=Sys.getenv('R_LIBS'))
library(ggplot2, lib.loc="/data/Rpackages/")
# install from a specific CRAN mirror
install.packages('RMySQL', repos='http://cran.us.r-project.org')
# dependency option
install.packages('knitr', dependencies = TRUE)
install.packages(c("rj", "r.gd"), repos=http://download.walware.de/rj-1.1)
install.packages("rattle", repos="http://rattle.togaware.com", type="source")
# Windows users may also run the following scripts to install required libraries
source("http://www.math.csi.cuny.edu/pmg/installpmg.R")
Install packages from source
install.packages(path_to_file, repos = NULL, type="source")
install.packages("RJSONIO", repos = "http://www.omegahat.org/R", type="source")
install.packages("C:/Tools/R/RLibs-3.2.5/latticist_0.9-44.tar.gz", repos = NULL, type = 'source')
install.packages("D:/PortableApps/R/R-3.5.3-LibsSrc/latticeExtra_0.6-28.tar.gz", repos = NULL, type = 'source')
install.packages("http://cran.r-project.org/src/contrib/Archive/RNetLogo/RNetLogo_0.9-6.tar.gz", repo=NULL, type="source")
install.packages(http://cran.r-project.org/src/contrib/Archive/ggplot2/ggplot2_0.9.1.tar.gz, repos=NULL, type="source")
Behind the scene, install from source File
Package is downloaded in a Temp folder and a R CMD
is run to build the package from the source
download.packages()
dir_temp <- tempdir()
dir.create(dir_temp)
pkg <- download.packages("car", dir_temp, type = "source")
wget http://cran.r-project.org/src/contrib/Archive/ggplot2/ggplot2_0.9.1.tar.gz
R CMD INSTALL ggplot2_0.9.1.tar.gz
R CMD INSTALL /usr/local/R-pkg/package_1.0-2.tar.gz
R CMD INSTALL RJSONIO_0.2-3.tar.gz
R CMD INSTALL --build RJSONIO_0.2-3.tar.gz
R CMD INSTALL --library="C:/Tools/R/Lib" gWidgetsRGtk2_0.0-84.tar.gz
Using Binaries
As an alternative, binaries can be downloaded from the CRAN but will probably require latest R Version. Just need to download and unzip the library archive under the R library. :warning: windows binaries are maintained only for most recent R versions (e.g. on 2020-03, we find binaries from R3.2 up to R4.0)
- https://cran.r-project.org/bin/windows/contrib/
- https://cran.r-project.org/bin/windows/contrib/3.5/ ** https://cran.r-project.org/bin/windows/contrib/3.4/esquisse_0.1.7.zip
Since March 2016, Windows and macOS binaries of CRAN packages for old versions of R (released more than 5 years ago) are made available from a central CRAN archive server instead of the CRAN mirrors
- CRAN FAQ : How can I get CRAN package binaries for outdated versions of R?
- https://cran-archive.r-project.org/bin/windows/contrib/
- https://cran.microsoft.com/snapshot/2018-04-01/bin/windows/contrib/3.4/
Custom package install with devtools
- https://rdrr.io/cran/installr/man/install.packages.zip.html
- https://rdrr.io/cran/devtools/man/install_deps.html
- https://rdrr.io/cran/installr/man/install.packages.zip.html
require(devtools)
install_version("ggplot2", version = "0.9.1", repos = "http://cran.us.r-project.org")
# https://support.rstudio.com/hc/en-us/articles/219949047-Installing-older-versions-of-packages
library(devtools)
install.packages.zip("https://cran.r-project.org/bin/windows/contrib/r-release/devtools_1.1.zip")
library(devtools)
install_url('https://cran.r-project.org/src/contrib/Archive/latticist/latticist_0.9-44.tar.gz')
install_github("twitter/AnomalyDetection")
install_github("klutometis/roxygen")
install_github("wch/ggplot2")
setwd("C:/Tools/R/RLibs-3.2.5")
install_local("latticist_0.9-44.tar.gz")
Install dependencies with devtools
setwd("C:/Tools/R/RLibs-3.2.5")
library(devtools)
install_deps(pkg = "latticist")
install_deps(".")
install_deps("/path/to/package", dependencies="logical")
Microsoft Reproducibility
Microsoft R Open offers predictability by default. During installation, the CRAN repository is configured to point to a specific CRAN repository snapshot. For Microsoft R Open 3.5.3, this fixed CRAN repository snapshot was taken Apr 15, 2019
- https://mran.microsoft.com/documents/rro/reproducibility
- https://mran.microsoft.com/package/checkpoint
- https://mran.microsoft.com/snapshot/
- R Open Release History
- Prev R Releases for Windows
Microsoft R Open
R Version | R Release | R Open | Snapshot | CRAN Repo link |
---|---|---|---|---|
3.6.3 | 20200229 | https://cran.microsoft.com/snapshot/2020-04-01/ | ||
3.5.3 | 20190311 | 201905 | 2019-04-15 | https://cran.microsoft.com/snapshot/2019-04-15/ |
3.5.2 | 201812 | 201904 | 2019-02-01 | |
3.4.4 | 20180315 | 201804 | 2018-04-01 | https://cran.microsoft.com/snapshot/2018-04-01/ |
3.3.3 | 201604 | 201703 | 2017-03-15 | https://cran.microsoft.com/snapshot/2017-03-15/ |
R 3.6.0 (April, 2019), R 3.5.0 (April, 2018), R 3.4.0 (April, 2017)
install.packages( "devtools", repos = "https://mran.microsoft.com/snapshot/2018-06-01/")
Installer function utility wrappers
Install if package is not installed
install_package <- function(x){
if (!require(x,character.only = TRUE))
{
install.packages(x,dep=TRUE)
if(!require(x,character.only = TRUE)) stop("Package not found")
}
}
Is installed
p %in% installed.packages()
Using require()
p = "Hmisc"
if (!require(p, character.only = TRUE)) {print(paste(p, "needs install"))}
if (!require(p, character.only=T, quietly=T)) {
install.packages(p)
library(p, character.only=T)
}
Upgrade R Packages (not recommended)
update.packages()
: updates all the packages installed with the newest version available in the repositories
update.packages(c("ggplot2", "reshape2", "dplyr"))`
update.packages(checkBuilt=TRUE, ask=FALSE)
Remove a Package
remove.packages()
: remove a package
uninstall.packages()
: uninstall
remove.packages()
remove.packages("survival", lib=Sys.getenv('R_LIBS'))
uninstall.packages(c("reshape", "plyr"))
Create your own packages
- http://r-pkgs.had.co.nz/package.html
- https://cran.r-project.org/doc/contrib/Lemon-kickstart/kr_pack.html
Upgrade R
InstallR package https://cran.r-project.org/web/packages/installr/index.html
# installing/loading the package:
if(!require(installr)) {
install.packages("installr")
require(installr)
} #load / install+load installr
updateR() # updates R
install.packages("installr")
library(installr)
updateR()
Then upgrade moved packages
update.packages(checkBuilt=TRUE)
- http://stackoverflow.com/questions/1401904/painless-way-to-install-a-new-version-of-r-on-windows
- https://cran.r-project.org/bin/windows/base/rw-FAQ.html#What_0027s-the-best-way-to-upgrade_003f
- https://cran.r-project.org/contrib/extra/batchfiles/
My Packages
my_package_list=c("devtools", "Hmisc", "reshape2", "dplyr", "gplots", "ggplot2", "ggExtra", "playwith", "gridExtra", "prettyR", "psych", "lessR", "tables", "xtable", "xlsx", "corrplot", "psy", "Epi", "MASS", "foreign", "XLConnect", "RCurl", "htmlTable", "knitr", "rmarkdown", "shiny", "shinyjs", "twitteR", "wordcloud")
## devtools :dependencies stringi, magrittr, BH, mime, R6, stringr, brew, xml2, httr, curl, memoise, whisker, evaluate, digest, rstudioapi, jsonlite, roxygen2, rversions, git2r
## Hmisc : dependencies Formula, latticeExtra, acepack, gridExtra
## latticist : not anymore in R 3.2
## playwith : ## dependency cairoDevice, gWidgetsRGtk2, gWidgets, gridBase
## lessR : ## also installing the dependencies minqa, nloptr, RcppEigen, lme4, SparseM, MatrixModels, pbkrtest, quantreg, car, leaps, MBESS, sas7bdat, readxl, triangle
## rattle : ## RGtk2
## htmlTable : ## dependencies formatR, highr, markdown, yaml, knitr
## shiny : ## crayon, XML, httpuv, htmltools, Cairo, testthat
## Rmcdr :## dependencies zoo, sandwich, e1071, RcmdrMisc, tcltk2, abind