We provide effective and economically affordable training courses for R and Python, Click here for more details and course registration !

Like in many other programming languages, packages in R are just collections of built-in functions and datasets are combined with R installation in a well-defined format. Library is directories where packages, either come with basic R installation or being manually installed, are stored on computer. In this post, we provide several important and useful functions associated with R package management.

  1. .libPaths()

.libPaths() will show library locations for packages (with R base installation in second line, and packages installed manually appears in the first line)

> .libPaths()
[1] "C:/Users/Wilso/AppData/Local/R/win-library/4.3"
[2] "C:/Program Files/R/R-4.3.2/library" 

2. install.packages() to install a package

Example: to install package ‘lavaan’ (lavaan is a package for working with latent variable model analysis)

> install.packages("lavaan")
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:

https://cran.rstudio.com/bin/windows/Rtools/
Installing package into ‘C:/Users/Wilso/AppData/Local/R/win-library/4.2’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/lavaan_0.6-16.zip'
Content type 'application/zip' length 3522149 bytes (3.4 MB)
downloaded 3.4 MB

package ‘lavaan’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
C:\Users\Wilso\AppData\Local\Temp\Rtmp4iVdAQ\downloaded_packages

3. library(“nameofPackage”) to load a package into R working session.

example: to load package ‘lavaan’ into R working session.

> library(lavaan)
This is lavaan 0.6-16
lavaan is FREE software! Please report any bugs.

4. update.packages(“nameofPackage”) to update a package to the latest version

example: to update package ‘lavaan’

> update.packages("lavaan")
> 

5. installed.packages() to show list of installed packages as well as their deailed information on this machine

> installed.packages()
             Package        LibPath                                         
lavaan       "lavaan"       "C:/Users/Wilso/AppData/Local/R/win-library/4.3"
mnormt       "mnormt"       "C:/Users/Wilso/AppData/Local/R/win-library/4.3"
numDeriv     "numDeriv"     "C:/Users/Wilso/AppData/Local/R/win-library/4.3"
pbivnorm     "pbivnorm"     "C:/Users/Wilso/AppData/Local/R/win-library/4.3"
quadprog     "quadprog"     "C:/Users/Wilso/AppData/Local/R/win-library/4.3"
base         "base"         "C:/Program Files/R/R-4.3.2/library"            
boot         "boot"         "C:/Program Files/R/R-4.3.2/library"            
class        "class"        "C:/Program Files/R/R-4.3.2/library"            
cluster      "cluster"      "C:/Program Files/R/R-4.3.2/library"            
codetools    "codetools"    "C:/Program Files/R/R-4.3.2/library"            
compiler     "compiler"     "C:/Program Files/R/R-4.3.2/library"            
datasets     "datasets"     "C:/Program Files/R/R-4.3.2/library"            
foreign      "foreign"      "C:/Program Files/R/R-4.3.2/library"            
graphics     "graphics"     "C:/Program Files/R/R-4.3.2/library"            
grDevices    "grDevices"    "C:/Program Files/R/R-4.3.2/library"            
grid         "grid"         "C:/Program Files/R/R-4.3.2/library"            
KernSmooth   "KernSmooth"   "C:/Program Files/R/R-4.3.2/library"            
lattice      "lattice"      "C:/Program Files/R/R-4.3.2/library"            
MASS         "MASS"         "C:/Program Files/R/R-4.3.2/library"            
Matrix       "Matrix"       "C:/Program Files/R/R-4.3.2/library"            
methods      "methods"      "C:/Program Files/R/R-4.3.2/library"            
mgcv         "mgcv"         "C:/Program Files/R/R-4.3.2/library"            
nlme         "nlme"         "C:/Program Files/R/R-4.3.2/library"            
nnet         "nnet"         "C:/Program Files/R/R-4.3.2/library" 

6. help(package=”package_name”) to show short description of a particular package, e.g the main working purpose of the package, and most useful and important functions the package have, and some simple example snippets.

example: to show description of package ‘lavaan’

> help(package="lavaan")
>
Documentation for package ‘lavaan’ version 0.6-16
DESCRIPTION file.
Help Pages
A B C D E F G H I L M N P R S U V W

-- A --
anova	LRT test
anova-method	Class For Representing A (Fitted) Latent Variable Model
-- B --
bootstrapLavaan	Bootstrapping a Lavaan Model
bootstrapLRT	Bootstrapping a Lavaan Model
-- C --
cfa	Fit Confirmatory Factor Analysis Models
cfaList	Fit List of Latent Variable Models
char2num	Utility Functions For Covariance Matrices
coef-method	Class For Representing A (Fitted) Latent Variable Model
coef-method	Class For Representing A List of (Fitted) Latent Variable Models
cor2cov	Utility Functions For Covariance Matrices
-- D --
Demo.growth	Demo dataset for a illustrating a linear growth model.
Demo.twolevel	Demo dataset for a illustrating a multilevel CFA.
-- E --
efa	Exploratory Factor Analysis
efaList	Summarizing EFA Fits
estfun.lavaan	Extract Empirical Estimating Functions

7. data(package=”packageName”) to show the ready-made datasets in a package

example: to show datasets in package ‘lavaan’

> data(package="lavaan")
>
Data sets in package ‘lavaan’:

Demo.growth            Demo dataset for a illustrating a linear
                       growth model.
Demo.twolevel          Demo dataset for a illustrating a multilevel
                       CFA.
FacialBurns            Dataset for illustrating the
                       InformativeTesting function.
HolzingerSwineford1939
                       Holzinger and Swineford Dataset (9 Variables)
PoliticalDemocracy     Industrialization And Political Democracy
                       Dataset

You can also watch our youtube video to get more illuminative understanding of the package management functions in R programming.