7 years ago I started wondering how to save time by automating routine procedures such as; setting up working/output/input directories, loading source files, setting up graph layouts and loading R packages when opening R/R studio. A couple of colleagues also had similar requests at the time. In this short tutorial I will focus on the latter. How to easily install/load R packages in R.
Like other programming languages, basic R contains a number of set functions that are installed when R is installed. Except for these base functions numerous others exist but they have be loaded together with their corresponding R package. An R package can be considered as a combination of functions and datasets created by R users. R is an open source statistical programming language and every R user can create functions, include them in an R package and upload them to CRAN.
To start using an R package, the package itself has to be installed in your computer. This can be done by install.packages(“Package_name”).
Basic 1st way to install/load R packages
This was my first try to install and load packages based on my undergraduate studies knowledge. This was could be very convenient when only a few packages have to be installed in an R session or when only one package has to be installed and loaded i.e. when a specific part of your code is initiated.
# Install packages install.packages("Rfast") install.packages("MASS") install.packages("mvtnorm") install.packages("mice") install.packages("lme4") install.packages("xtable") install.packages("lattice") install.packages("gridBase") install.packages("mvtnorm") install.packages("RfagridExtrast") install.packages("grid") install.packages("latticeExtra") install.packages("tmvtnorm") install.packages("fitdistrplus") # Load packages load(Rfast) load(MASS) load(mvtnorm) load(mice) load(lme4) load(xtable) load(lattice) load(gridBase) load(mvtnorm) load(RfagridExtrast) load(grid) load(latticeExtra) load(tmvtnorm) load(fitdistrplus)
Most flexible 2nd way to install/load R packages
This could be the most flexible solution to install/load R packages. All packages are stored in a vector called i.e. “packages.list”. Then, a routine checks which packages are not yet installed and create a filtered version of the “packages.list” vector.
# Save all packages names packages.list <- c("Rfast","MASS","mvtnorm","mice","lme4","xtable","lattice","gridBase","mvtnorm","gridExtra","grid","latticeExtra","tmvtnorm","fitdistrplus") # Install missing packages packages.new <- packages.list[!(packages.list %in% installed.packages()[,"Package"])] install.packages(packages.new);rm(packages.new) # Load all selected packages lapply(packages.list, require, character.only = TRUE); rm(packages.list) # Update all downloaded packages update.packages()
Most prompt 3rd way to install/load R packages
A number of packages currently exist in R that can automated the above procedure, such as the pacman package. By using such packages, as all R packages the user should get familiarized with each command result. For example, the p_load command by has the option to automatically update packages to each latest versions.
# Install pacman package install.packages("pacman"); load(pacman) # Install and load packages p_load(Rfast,MASS,mvtnorm,mice,lme4,xtable,lattice,gridBase,mvtnorm,gridExtra,grid, latticeExtra,tmvtnorm,fitdistrplus) # Unload package p_unload(Rfast,MASS) # Install packages from Github p_install_gh("trinker/pacman")
If you have any suggestions on the guide above send me a message or comment below.