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:


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:

Get all packages currently loaded in the R environment


Install a New Package

We can install packages to R in two different ways:

  • Installing directly from the CRAN repository
  • Install packages manually
  1. 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:

For example:


package ‘XML’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in


  1.   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.


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:

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:


Pin It on Pinterest

Share This