R Packages

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:

Output:

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

Output:

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:

Output:

package ‘XML’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in

C:\Users\Nikita\AppData\Local\Temp\RtmpCuQHa2\downloaded_packages

  1.   Install Package Manually

https://cran.r-project.org/web/packages/available_packages_by_name.html

Go to the above link to download the package needed. And then run the following command to install this package in the R environment.

Example:

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:

Reference:
https://www.rstudio.com/products/rpackages/
https://www.tutorialspoint.com/r/r_packages.htm