1 Goal


The goal of this tutorial is to list all the installed packages and list all the loaded packages. This could be very useful if need to check if a library has been installed.


2 List all the installed packages


# The function library() will show every package and its version installed in our computer
# However it is too much information
# We can create a vector with the name of the packages to check later if a package is in it

my_packages <- library()$results[,1]
head(my_packages, 10)
##  [1] "abind"         "Amelia"        "AnnotationDbi" "antiword"     
##  [5] "arules"        "arulesViz"     "assertthat"    "audio"        
##  [9] "automap"       "backports"
# Now we can check if some package is installed
# We added the tolower function to avoid problems with capital letters
"dplyr" %in% tolower(my_packages)
## [1] TRUE

3 List all loaded packages


# We can list the libraries that are actually loaded doing
(.packages())
## [1] "stats"     "graphics"  "grDevices" "utils"     "datasets"  "methods"  
## [7] "base"
# We can check that it actually works
library(ggplot2)
(.packages())
## [1] "ggplot2"   "stats"     "graphics"  "grDevices" "utils"     "datasets" 
## [7] "methods"   "base"
# Again we could check if a library is loaded
"dplyr" %in% tolower((.packages()))
## [1] FALSE
# And load it if it's not loaded yet
if(! "dplyr" %in% tolower((.packages()))){
  library("dplyr")
  (.packages())
}
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
## [1] "dplyr"     "ggplot2"   "stats"     "graphics"  "grDevices" "utils"    
## [7] "datasets"  "methods"   "base"
# We could use the require function for the same purpose
require("readr")
## Loading required package: readr
(.packages())
##  [1] "readr"     "dplyr"     "ggplot2"   "stats"     "graphics" 
##  [6] "grDevices" "utils"     "datasets"  "methods"   "base"

4 Conclusion


In this tutorial we have learnt how to list all the installed libraries in R as well as how to find if certain library has been loaded.