Lets 'Start with Why' ....'Why a concept of Package exists in R ' -

R is built on the ethos of 'Sharing & Building Together ' which essentially means -
a global community of Experts & Enthusiasts are constantly adding / sharing new features / capabilities of R.

These new enhancements get shared with the R community through 'Packages' being made available at a Central Official Repository.

Availability of these additional Packages increases the Power of R by improving upon the existing base R functionalities.

So , What is a Package in R then ...

In R , the fundamental unit of Shareable code is called a Package .
A Package bundles together Code , Data , Documentation & easy to share with others.

The Description or Documentation of a Package can be retrieved in R using following commands -

R Command to show the Package Description 
> packageDescription("Mosaic") 

R Command to show the Package Documentation
> help( package = 'mosaic')

And Where is the Central Official Repository of Publicly shared Package in R ...

CRAN ( Comprehensive R Archive Network ) is the official repository , it is a network of ftp & web servers being maintained by the R community around the world.
This repository is maintained & coordinated by R Foundation & for a package to be published here , it needs to pass several stringent tests demonstrating that the Package is following CRAN policies.

How many R Packages are available as on Nov 2018
> nrow(available.packages())  # Total Number of R Packages available as on Nov 2018
[1] 13310

Now , lets discuss the final aspect of this article - How to Install & Load packages -

a) Before we Install a Package , lets check whether a Package is already installed or not -

Below 2 commands ( either of them ) will list down all Installed Packages in your R Environment

> installed.packages()[,1]


> library()

b) How to Install Package which is not already installed ..

Package Installation is a process by which a Package available in Central Repository ( Like CRAN ) gets downloaded from Web Central Repository and added to your R Local Library ( on your system ) .
Hence , to install packages , it is prerequisite that you have internet connectivity.

Below R command will Install 'dplyr' package along with all the dependent Packages.
> install.packages('dplyr' , dependencies = T)

c) We have Installed a Package , but how to Load the Package to the memory and have the objects in Package made available to R Environment instance ..

The Packages can be loaded to memory by 2 ways -

> library(dplyr)


> require(dplyr)

After loading the Packages using above methods , you can now access all Objects in the library directly .

d) How to check the list of Packages Loaded into Memory 

> search()
 [1] ".GlobalEnv"         "package:mosaic"     "package:Matrix"     "package:mosaicData"
 [5] "package:ggformula"  "package:ggstance"   "package:ggplot2"    "package:lattice"   
 [9] "package:titanic"    "package:bindrcpp"   "package:dplyr"      "tools:rstudio"     
[13] "package:stats"      "package:graphics"   "package:grDevices"  "package:utils"     
[17] "package:datasets"   "package:methods"    "Autoloads"          "package:base" 

e) How to unload a Package from the memory ( so as to make space in memory by removing the unused Package )

> detach("package:mosaic" , unload=T)

f) How to remove an Installed Package which is no more needed 

> remove.packages('mosaic')
Removing package from ‘D:/Users/prmohant/Documents/R/R-3.5.1/library’
(as ‘lib’ is unspecified)

g) How to find the list of Installed Packages which are older than the latest version available at Web Central Repository

> old.packages()

h) How to update all the old packages installed on your system

> update.packages()

Other R commands / functions related to package handling

1) Get the System Folder Path where R Packages are stored on your System

> .libPaths()

Thank You & Happy Learning !!

@AUTHOR : Admin

Tags:Eco, Water, Air, Environment

Comments (0)

    No Comments Found
Leave a Comment