Building and installing an R package
In R Programming, packages are a collection of functions, data sets and compiled code. Packages are used to collect sets of R functions into a single unit.
The directory where packages are stored is known as ‘library’. R provides many standard sets of packages that are installed during the installation of R.
When we start the R console or RStudio, only the default packages are available by default. Other installed packages must be loaded explicitly.
Get Library Location
To get the library location containing R packages we have to use .libPaths() function:
 "C:/Users/Nikita/Documents/R/win-library/3.5" "C:/Program Files/R/R-3.5.2/library"
List all packages installed
We can load a package which is already existing and installed on your system using the library() function.
When we execute the above command it produces the following output:
Packages in library ‘C:/Program Files/R/R-3.5.2/library’: base The R Base Package boot Bootstrap Functions (Originally by Angelo Canty for S) class Functions for Classification cluster "Finding Groups in Data": Cluster Analysis Extended Rousseeuw et al. codetools Code Analysis Tools for R compiler The R Compiler Package datasets The R Datasets Package foreign Read Data Stored by 'Minitab', 'S', 'SAS', 'SPSS', 'Stata', 'Systat', 'Weka', 'dBase', ... graphics The R Graphics Package grDevices The R Graphics Devices and Support for Colours and Fonts grid The Grid Graphics Package KernSmooth Functions for Kernel Smoothing Supporting Wand & Jones (1995) lattice Trellis Graphics for R MASS Support Functions and Datasets for Venables and Ripley's MASS Matrix Sparse and Dense Matrix Classes and Methods methods Formal Methods and Classes mgcv Mixed GAM Computation Vehicle with Automatic Smoothness Estimation nlme Linear and Nonlinear Mixed Effects Models nnet Feed-Forward Neural Networks and Multinomial Log-Linear Models parallel Support for Parallel computation in R rpart Recursive Partitioning and Regression Trees spatial Functions for Kriging and Point Pattern Analysis splines Regression Spline Functions and Classes stats The R Stats Package stats4 Statistical Functions using S4 Classes survival Survival Analysis tcltk Tcl/Tk Interface tools Tools for Package Development translations The R Translations Package utils The R Utils Package
Get all packages currently loaded in the R environment
 ".GlobalEnv" "tools:rstudio" "package:stats" "package:graphics" "package:grDevices" "package:utils"  "package:datasets" "package:methods" "Autoloads" "package:base"
Install a New Package
We can install packages to R in two different ways:
- Installing directly from the CRAN repository
- Install packages manually
- Install Directly from the CRAN
To install the package directly from the CRAN repository we have to use the following command with the package name:
package ‘XML’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
- Install Package Manually
Go to the above link to download the package needed. And then run the following command to install this package in the R environment.
install.packages(file_name_with_path, repos = NULL, type = "source") # Install the package named "XML" install.packages("D:/XML_3.98-1.19.zip", repos = NULL, type = "source")
Load Package to library
A package must be loaded to the current R environment. To load the package we have to use the following command:
library("package Name", lib.loc = "path to library")
Maintaining Packages in R
You can update the installed packages with the following command:
You can also check what packages need an update with the following command:
If you want to delete any package from R environment then you can delete it using the following command:
remove.packages("Package Name")Reference: https://www.rstudio.com/products/rpackages/ https://www.tutorialspoint.com/r/r_packages.htm