Various R functions, sample data, and compile codes are grouped to create R Packages, that are stored under a directory called “library”, in the R environment. Some sets of packages are installed with R installation, while other packages can be added as and when required. The packages installed with R installation are the default packages that are obtainable whenever the R console starts. The packages installed later on require explicit loading. They can then be utilised as required in programming with R or R codes.
Functions to check Available R Packages:
- libPaths(): It is important to find the library locations to look for the available R Packages. This is where libPaths() function is utilised.
Functions to get the list of all the installed packages:
- library(): The list of all the installed packages can be made available using the library() function in R.
Packages in library ‘C:/Program Files/R/R-3.6.1/library’:
search() : The packages presently installed in the R environment can also be viewed using the search() function in R.
Installing a New Package in R:
New packages can be installed in R using either of the below two techniques:
- Installing packages directly from the CRAN directory.
- Installing packages manually after downloading the package to the local system.
Installing R packages directly from the CRAN directory:
The packages can be installed directly from CRAN webpage to the R environment, using the below command.
install.packages(“Package Name”)
Example: For installing XML package:
install.packages(“XML”)
Note:: If prompted to select the nearest mirror, select the mirror which is appropriate to your location.
Output:
Installing packages manually after downloading the package to the local system:
To install the R packages manually, follow the below steps:
- Go to the link https://cran.r-project.org/web/packages/available_packages_by_name.html.
- Download the desired package.
- The package will be downloaded as a .zip file.
- Locate the file in your local system.
Use the below command, after downloading the package:
install.packages(file_name_with_path, repos = NULL, type = “source”)
Example: For installing the package named “XML”:
install.packages(“C:\Users\example\OneDrive\Desktop\graphics\xml2_1.2.2.zip”, repos = NULL, type = “source”)
Loading Packages to the Library:
Before using the installed packages in your code, you need to load them into the current R environment. For this purpose, you can make use of the below command:
library(“package Name”, lib.loc = “path to library”)