The goal of this tutorial is to load several libraries in one single action. This could be very useful for example if we can define a common list of libraries for a project. We could even load the list of libraries from a source file.
# Imagine that we need to load a long list of libraries
library(dplyr)
library(arules)
library(caret)
library(corrplot)
library(VIM)
library(e1071)
library(readr)
# To perform the following exercise we need to unload the libraries
# This is performed using detach
detach("package:dplyr", unload = TRUE, force = TRUE)
detach("package:arules", unload = TRUE, force = TRUE)
detach("package:caret", unload = TRUE, force = TRUE)
detach("package:corrplot", unload = TRUE, force = TRUE)
detach("package:VIM", unload = TRUE, force = TRUE)
detach("package:e1071", unload = TRUE, force = TRUE)
detach("package:readr", unload = TRUE, force = TRUE)
# We check how many libraries are loaded
# Notice only dependencies are still loaded
(.packages())
## [1] "data.table" "grid" "colorspace" "ggplot2" "lattice"
## [6] "Matrix" "stats" "graphics" "grDevices" "utils"
## [11] "datasets" "methods" "base"
# We can create a list of the packages we want to load
my_packages <- c("dplyr", "arules", "caret", "corrplot", "VIM", "e1071", "readr" )
# And then load them all at once doing
lapply(my_packages, library, character.only = TRUE)
# We can check now that the libraries are loaded again
(.packages())
## [1] "readr" "e1071" "VIM" "corrplot" "caret"
## [6] "arules" "dplyr" "data.table" "grid" "colorspace"
## [11] "ggplot2" "lattice" "Matrix" "stats" "graphics"
## [16] "grDevices" "utils" "datasets" "methods" "base"
In this tutorial we have learnt how to load several libraries at once using a character vector. We have learnt as well how to detach packages.