4 Packages and libraries
4.1 Packages
Packages are organised collections of R functions, data, and compiled code. Packages have useful functions to perform specific tasks. Some of the useful packages for agricultural research are agricolae
, grapesAgri1
etc.
In the R environment, packages are kept in a directory called “library”. R installs a group of packages by default during installation. Only the default packages are initially available once the R console has been launched. The R application that is getting to use other installed packages needs to be explicitly loaded from package library in order to use them.
4.2 Repositories
A repository is where packages are located so that they can be installed from it. Organizations and developers each have their own local repository, which is often online and open to all. The most well-liked R package repositories include: CRAN, BIOCONDUCTOR, GITHUB.
4.2.1 CRAN
Comprehensive R Archive Network(CRAN) is the official repository, It is an international network of ftp and web servers run and managed by the R community. It is coordinated by the R community, and before a package can be published in CRAN, it must pass a number of tests to guarantee that it complies with CRAN rules.
4.3 Base packages
Base packages or standard pacakges come pre-installed with R installations and are regarded as being a part of the R source code. The fundamental R functions are found in base packages, which make it possible to perform common statistical and graphical operations on datasets. Examples of Base packages include stats
, graphics
, grDevices
, utils
, datasets
, methods
and base
. The functions available in these Base packages can be used for basic operations on the data set. Some of the functions we have discussed earlier in Section3, like mean()
,min
, log10
,as.data.frame
etc.
R is distributed with fifteen “base packages”: base
, compiler
, datasets
, grDevices
, graphics
, grid
, methods
, parallel
, splines
, stats
, stats4
, tcltk
, tools
, translations
, and utils
.
In addition, there are fifteen “recommended packages” from CRAN which are included with binary distributions of R: KernSmooth
, MASS
, Matrix
, boot
, class
, cluster
, codetools
, foreign
, lattice
, mgcv
, nlme
, nnet
, rpart
, spatial
, and survival
.
4.4 Other packages
Any user could develop a package and make it available through any of the repositories. There are thousands of helpful R packages.
4.5 Installing R Packages
There are numerous ways to install R Packages, which are listed below. You need an active internet connectivity for installing packages.
4.5.1 From CRAN
Installing Packages From CRAN: For installing Package from CRAN
we need the name of the package and use the following command:
install.packages("package name")
Simply express package name as a character vector in the first argument of the install.packages()
function to install more than one package at once:
install.packages(c("grapesAgri1", "MASS"))
4.5.2 From Bioconductor
In order to install a package in Bioconductor, run the following script first:
source("https://bioconductor.org/biocLite.R")
This will install some basic functions which are needed to install bioconductor packages, such as the biocLite()
function. To install the core packages of Bioconductor just type it without further arguments.
biocLite()
Type the names of the desired packages in a character vector format if we just need a few of them from this repository.
Example:
biocLite(c("GenomicFeatures", "AnnotationDbi"))
4.5.3 From github
To install a R package from github, start by installing the devtools package. The best way to do this is from CRAN.
install.packages("devtools")
Install the package of interest from GitHub using the following code, where you need to remember to list both the author and the name of the package. In this example, we are installing the grapesAgri1
package created by pratheesh3780.
devtools::install_github("pratheesh3780/grapesAgri1")
4.6 Load packages
Once you successfully install an R package; you need to load the package for use in the session. library()
function can be used to load package.
Example
4.6.1 Load Multiple Packages at Once
We need to first specify a vector of package names:
my_packages <- c("dplyr", "mice", "stringr") # vector of package names
lapply(my_packages, require, character.only = TRUE) # Load multiple packages
After running the previous R code, all packages in the vector my_packages are loaded to R.
4.7 Check, Update and Remove
To check what packages are installed on your computer, type this command:
To update all the packages, type this command:
To update a specific package, type this command:install.packages("package name")
install.packages("grapesAgri1")
4.8 Installation using R studio UI
The packages can be installed using Rstudio User interface. Step by step installation of package grapesAgri1
is shown below. All other packages can be installed in similar way
- Step 1: Click on the install button as shown below
- Step 2: Type the name of the package in the window that appears
- Step 3: Select the package name and click install
- Step 4: Load the package name by clicking on the check box.