What are the Packages in R?

R Packages

R packages are the collection of R functions, sample data, and compile codes. In the R environment, these packages are stored under a directory called " library ." During installation, R installs a set of packages. We can add packages later when they are needed for some specific purpose. Only the default packages will be available when we start the R console. Other packages which are already installed will be loaded explicitly to be used by the R program.

There is the following list of commands to be used to check, verify, and use the R packages.

R Packages

Check Available R Packages

To check the available R Packages, we have to find the library location in which R packages are contained. R provides libPaths() function to find the library locations.

libPaths()

When the above code executes, it produces the following project, which may vary depending on the local settings of our PCs & Laptops.

[1] “C:/Users/ajeet/OneDrive/Documents/R/win-library/3.6”
[2] “C:/Program Files/R/R-3.6.1/library”

Getting the list of all the packages installed

R provides library() function, which allows us to get the list of all the installed packages.

library()

When we execute the above function, it produces the following result, which may vary depending on the local settings of our PCs or laptops.

Packages in library ‘C:/Program Files/R/R-3.6.1/library’:

R Packages

Like library() function, R provides search() function to get all packages currently loaded in the R environment.

search()

When we execute the above code, it will produce the following result, which may vary depending on the local settings of our PCs and laptops:

[1] ".GlobalEnv"         "package:stats" "package:graphics" 
[4] "package:grDevices"  "package:utils" "package:datasets"
[7] "package:methods"    "Autoloads"     "package:base"

Install a New Package

In R, there are two techniques to add new R packages. The first technique is installing package directly from the CRAN directory, and the second one is to install it manually after downloading the package to our local system.

Install directly from CRAN

The following command is used to get the packages directly from CRAN webpage and install the package in the R environment. We may be prompted to choose the nearest mirror. Choose the one appropriate to our location.

install.packages(“Package Name”)

The syntax of installing XML package is as follows:

install.packages(“XML”)

Output

Install package manually

To install a package manually, we first have to download it from https://cran.r-project.org/web/packages/available_packages_by_name.html. The required package will be saved as a .zip file in a suitable location in the local system.

R Packages

Once the downloading has finished, we will use the following command:

install.packages(file_name_with_path, repos = NULL, type = “source”)

Install the package named “XML”

install.packages(“C:\Users\ajeet\OneDrive\Desktop\graphics\xml2_1.2.2.zip”, repos = NULL, type = “source”)

Load Package to Library

We cannot use the package in our code until it will not be loaded into the current R environment. We also need to load a package which is already installed previously but not available in the current environment.

There is the following command to load a package:

library(“package Name”, lib.loc = “path to library”)

Command to load the XML package

install.packages(“C:\Users\ajeet\OneDrive\Desktop\graphics\xml2_1.2.2.zip”, repos = NULL, type = “source”)