Fastest Way to Install & Load Libraries in R | by Catherine Williams | Towards Data Science

After working collaboratively with a classmate, it became apparent that I needed a new way of loading libraries from what I was taught in school. Not everyone has the same libraries installed and this can run into errors. I wanted the code to run seamlessly for everyone. Additionally, it is painful to have to write the same functions over and over again to install and load different libraries. My classmate and I worked to find a simple way to do this. I then used a package called tictoc to measure the speed of different methods.
Note: The testing was done with a clean global environment. All of the packages have already been installed and loaded as well so that testing can stay consistent (the packages will just re-load).
First, the tictoc package needs to be installed and loaded in order to do the analysis. I will also define a variable for the list of packages to be loaded.
install.packages("tictoc")library(tictoc)packages <- c("tidyverse", "dplyr", "stringr", "zoo", "ROCR", "caret", "class", "gmodels", "randomForest")
Method 1 — Load and install each library separately:
I commented out install.packages because it will reinstall whether the package exists or not. This is not the most efficient method since you will either reinstall everything or get an error if install.packages is omitted and the package does not exist in the current installation.
Total time: 0.11 sec
tic("basic")#install.packages(packages)library(tidyverse)library(dplyr)library(stringr)library(zoo)library(ROCR)library(caret)library(class)library(gmodels)library(randomForest)toc(log = TRUE)log.txt <- tic.log(format = TRUE)
Method 2 — Use a for loop
Total time: 0.13 sec
tic("for loop")libraries <- function(packages){  for(package in packages){    #checks if package is installed    if(!require(package, character.only = TRUE)){      #If package does not exist, then it will install      install.packages(package, dependencies = TRUE)      #Loads package      library(package, character.only = TRUE)    }  }}libraries(packages)toc(log = TRUE)log.txt <- tic.log(format = TRUE)
Method 3 — Use apply method
Total time: 0.07 sec
tic("apply")ipak <- function(pkg){  new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]  if (length(new.pkg))    install.packages(new.pkg, dependencies = TRUE)  sapply(pkg, require, character.only = TRUE)}ipak(packages)toc(log = TRUE)log.txt <- tic.log(format = TRUE)
Method 4 — Use the install.load package
For the purposes of this post, I calculated the time with the assumption that the user already has install.load installed, since this would only have to happen once. The code to install is just there for reproducibility purposes.
Total time: 0.12 sec
tic("install.load")if (!require("install.load")) install.packages("install.load")library(install.load)install_load(packages)toc(log = TRUE)log.txt <- tic.log(format = TRUE)
Method 5 — Use the pacman package
This was also calculated with the assumption that the user already has pacman installed.
Total time: 0.06 sec
tic("pacman")if (!require("pacman")) install.packages("pacman")#pacman will not accept a character vector so the same packages are repeatedpacman::p_load("tidyverse", "dplyr", "stringr", "zoo", "ROCR", "caret", "class", "gmodels", "randomForest")toc(log = TRUE)log.txt <- tic.log(format = TRUE)
Final Verdict
The fastest way to install and/or load many packages is to use the pacman package. However, if you did not want to install an extra package, using the apply method will be the best. It is only minutely slower.
It is important to also mention that with the “basic” test, if the user is unsure of the packages they currently have installed, this method can become drastically slower for the reasons mentioned above.