R packages consists of a collection of R functions, data sets and compiled code which adds value to the existing R-functionalities. They are stored in the 'library' directory in the R-environment and developed by the community. For Example, "dplyr" is one of the commonly used packages in R which adds further functionalities with respect to working with dataframes.
There already exists some default packages in the local directory 'library' on your machine when you install R. We can see all the default packages by using code : 'library()'. If we want to add a new package, we can use three ways to do it:
This recipe demonstrates the installation and loading of a package via devtools package. devtools package gives us the option to choose and install packages from different repositories like Bioconductor, Bitbucket, CRAN, local file, GitHub, URL, SVN etc.
We use the command " install.packages("package_name") " to install from CRAN respository. Below shows the code for installation of "devtools".
install.packages("devtools")
After running the command, you might recieve some messages which is based on the OS, dependencies installed and the status of the package.
We can use various functions of devtools package to install packages from different repositories like:
Syntax: devtools::install_cran("package_name")
Below is the example of installing package from CRAN repository using devtools package
devtools::install_cran("MASS")
We use the function "library()" to load the package. It is essential to load the package before we can use it in your code.
library(MASS)