R Packages are collections of R functions, data, and compiled code in a well-defined format. The directory where packages are stored is called the library. R comes with a standard set of packages. Others are available for download and installation. Once installed, they have to be loaded into the session to be used.
R comes with standard (or base) packages, which contain the basic functions and data sets as well as standard statistical and graphical functions that allow R to work easily.
The packages are stored in three sources mainly –
1. CRAN
2. Bioconductor
3. Github
CRAN :
The full form of CRAN is Comprehensive R Archive Network. It is an open source platform to store packages and other documents related to R. It is an open distributed platform , where anyone can store and download packages . It is default source to install packages in R.
Bioconductor:
Bioconductor is a free, open source and open development software project for the analysis and comprehension of genomic data generated by wet lab experiments in molecular biology.
Github:
It is a web based repository to store and collaboration features for development . We can store packages in this repository .
You can install package by following these steps :
1. Go to Package tab in right corner window . Choose Install option from the Package menu .
2. It will open Install Packages window .
3. Add package name under Packages(separate multiple with space or comma) . Add ggplot2 package to install.
4. I am installing package ggplot2 here . Click on Install button to install package.
5. It will show following output in Console window .
You can also install packages by using Script window. We can write this code to install “ggplot2” package.
install.packages(“ggplot2”)
To load “ggplot2” package we can write following code:
library(ggplot2)
When we run this command , it will show this output .
We can also load “ggplot2” package by using another way :
We search ggplot2 in Package window . It will show this window.
For loading ggplot2 package , we tick mark ggplot2 . It will load ggplot2 package .
You can see output here :
If we unselect (un-tick) mark across ggplot2 , it will unload package from R .We can see the output here :
It shows following code in Console window :
detach(“package:ggplot2”,unload=TRUE)
It shows we detach package from R by above code or by just un-tick ggplot2 from package window.
You can also download multiple packages at the same time :
install.packages(c(“ggplot2″,”dplyr”))
Install a package from Bioconductor :
First , we install Bioconductor package by :-
source(“https://bioconductor.org/biocLite.R”)
To install package “limma” from Bioconductor source :
biocLite(“limma”)
Install a package from Github :
First install “devtools” package.
install.packages(“devtools”)
We are installing “survminer” package here :
devtools::install_github(“kassambara/survminer”)
In above code kassambara is User-name of github user . Survminer is package stored in github repository of kassambara(User ).
View loaded R packages :
To view the list of loaded (or attached) packages during an R session, use the function search():
search()
Remove installed packages :
To remove an installed R package , we are using remove.packages() as follow:
remove.packages(“fpc”)
It will remove “fpc” package from R.
Update installed packages :
If you want to update all R packages, type this:
update.packages()
To update specific installed packages “readr “and “ggplot2“, use this:
update.packages(oldPkgs = c(“readr”, “ggplot2”))