Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

R language package


May 12, 2021 R language tutorial


Table of contents


The package for the R language is a collection of R functions, compiled code, and sample data. T hey are stored in a directory called "library" in the R-language environment. B y default, the R language installs a set of packages during installation. A dditional packages are then added when they are used for specific purposes. W hen we start the R language console, only the default package is available by default. O ther packages that have been installed must be explicitly loaded for use by R-language programs that will be using them.

All available R language packs are listed in the R language package.
The following is a list of commands for checking, validating, and using R-packs.

Check the packages that are available in the R language

Gets the location of the library that contains the R package

.libPaths()

When we execute the code above, it produces the following results. I t may vary depending on your computer's local settings.

[2] "C:/Program Files/R/R-3.2.2/library"

Gets a list of all packages that have been installed

library()

When we execute the code above, it produces the following results. I t may vary depending on your computer's local settings.

Packages in library ‘C:/Program Files/R/R-3.2.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

Gets all the packages that are currently loaded in the R environment

search()

When we execute the above code, it produces the following results. I t will vary depending on your PC's local settings.

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

Install a new package

There are two ways to add a new R package. O ne is installed directly from the CRAN directory, and the other is to download the package to the local system and install it manually.

Install directly from CRAN

The following commands get the package directly from theRAN web page and install the package in the R environment. Y ou may be prompted to select the most recent mirror. C hoose one based on your location.

 install.packages("Package Name")
 
# Install the package named "XML".
 install.packages("XML")

Install the package manually

Go to the link R Packages to download the required package. Save the package as .zip file in the appropriate location on the local system.
You can now install this package in an R environment by running the following commands.

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

# Install the package named "XML"
install.packages("E:/XML_3.98-1.3.zip", repos = NULL, type = "source")

Load the package into the library

Before a package can be used in code, it must be loaded into the current R environment. Y ou also need to load packages that were previously installed but are not available in the current environment.

Load the package with the following command:

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

# Load the package named "XML"
install.packages("E:/XML_3.98-1.3.zip", repos = NULL, type = "source")