Packages in R

In this tutorial, we are beginning to explore the R programming language to an extended level. We will discuss how to install R packages into our operating systems. You will also learn different packages in R, their purpose, and how to load these packages in R.

In this tutorial, you will learn

  • How to install R packages 
  • How to check packages
  • How to load packages 
  • List of packages
  • How to use the R package 

And so on...

What are R packages?

In the R programming language, the R packages form a fundamental unit of shareable code. An R package is a bundle with a collection of codes, data, documentation, and tests. The best part of the R package is it can be shared easily with others. The users can install these R packages from a centralized repository known as CRAN. The CRAN is the acronym for the Comprehensive R Archive Network. One of the main reasons for the successful use of R in different emerging fields like data science, Machine learning is the variety of available packages in CRAN.

The R packages are stored under a directory called “library” within the R environment. During R installation a set of packages gets installed by default. When the user starts with RStudio these default packages are only available. Some packages other than default packages need to be loaded by the user to the R program for their efficient utilization.

Where do these R packages reside?

The R packages reside in software repositories. A software repository is the storage location where a collection of packages are stored, managed, and maintained. These are accessible online and the public or users or anybody can install R packages from these repositories depending upon their requirements.

Some of the software repositories that provide R packages are

  • CRAN
  • MRAN and RStudio Package Manager
  • Bioconductor
  • R-Forge
  • Github
sw repositories img

Let us consider each repository and its performances. What makes one different from other repositories?

Software Repositories Description
CRAN
  • Comprehensive R Archive Network is an official repository for R packages.
  • An archive of earliest and latest R packages supported by the R foundation.
  • It contains source packages to support the collection of code written by user to perform some specific action.
  • It includes pre-compiled binaries for Windows and macOS.
  • CRAN supports R programmers by providing more than 16,000 R packages.
  • This report is as of November 2020.
  • All types of packages are available .  
MRAN and RStudio Package Manager
  • Microsoft R Application Network (MRAN)
  • Maintained by MICROSOFT
  • It forms a mirror of CRAN.  
Bioconductor
  • It contains R packages that support for the analysis of data.
  • Mostly for dealing with object oriented data handling.
R-Forge
  • It is a central platform.
  • It includes packages for collaborative development of R related projects.  

What is the difference between an R package and a library?

In R, the collection of functions, data, and compiled code form a package. The library is the storage location of these R packages. The R packages are installed in your system by downloading from some software repositories (CRAN, GitHub, etc) and are stored in the library. Once after installation of packages to make available in your code, the package needs to be loaded using the command or function library(package name). (Will discuss this in detail in this tutorial)

difference img

How to install an R package?

In RStudio, R packages are the bundles of commands or libraries to support certain analyses. Installing packages in RStudio is easy and can be done in multiple ways by

  • Installing using functions 
  • Installation using RStudio User Interface.
  • Possible to install multiple packages at a time.

Installation of R package using functions

R packages can be installed manually using the simple function


install.packages()

Where the parentheses enclose the package name that needs to be installed in the double quotation. Suppose need to install the calendar package, the following command is used.


 install.packages("calendR")

The screenshot shows the RStudio console with install.packages() command to install package calendR. You can install any package of your interest.

difference img

How to install multiple packages at a time?

To get rid of repeated writing of the same function again and again in the RStudio console to install different packages at the same time the R supports the below syntax


install.packages(c("ggplot2", "dplyr"))

The function to install a package install.packages() with another c() function which contains the name of multiple packages to be installed at a time. The single-line function allows installing multiple packages at a times

How to install R packages using RStudio User Interface?

Follow the steps to install an R package in RStudio

  1. Take the RStudio interface. Open the packages tab shown in the window.
    screenshot img
  2. From there click on install packages.
    screenshot img
  3. Suppose you want to install a package called modeest which allows using the mode analysis program. Type modeest in packages box. And click install.
    screenshot img
  4. You can see the package is downloaded and installed in RStudio.
    screenshot img
  5. Under the list of packages, you can check the newly installed package.
    screenshot img
  6. Now let us see how to load this modeest package to our program. Consider a simple program to find the list of numbers in vector x. Create a vector x using c() function
    
    x=c(1,2,3,4,4,5)
    
    

    The command that comes with modeest package is known as mfv. The mfv() takes x as an argument inside the braces and finds the mode of the list of numbers 1,2,3,4,5. And returns output value 4.

How to load a package to the R program?

After downloading the packages using either of the methods may be by function in the console or with the help of GUI menu options, the desired R package gets installed. The next step is to use these R packages in the R program. In order to use the R package installed, you need to load the package.

You can load the package by using the library() function. The function encloses the name of the package to be loaded in either quotes or without quotes.


library(calendR)
OR
library(“calendar”)

How to check the installed R package?

After loading you can check the loaded package using help or ? in the R console. This further helps in all documentation related to any particular package name including its description, syntax the package follows, what the parameter or arguments inside each function define, and so on.


?(<name of package>)
Or
 help(<name of package>)

Example


?calendR
help(calendR)   or  help(“calendR”)   

How to uninstall an R package?

To remove a package from the library click on the × (cross) mark on the right side.
It displays the remove package message once placing the mouse on the × (cross). The removed packages will be no longer in the R library.

uninstall img

How to update packages in R?

To modify operations and to improve functionalities sometimes R packages are updated.

It is advisable to update R packages occasionally. You can update R packages from RStudio using the update button in the toolbar of the Packages panel.

screenshot img

The RStudio verifies the R packages need to be updated and displays them in a dialog box as shown below

screenshot img

Install R package from GitHub

Before installing R packages from GitHub you need to check for devtools packages are available or not. You can check that by using the function library(devtools) in the R console. If the devtools are already installed no messages will pop up, if no such package exit displays a message showing

Error in library(devtools) : there is no package called ‘devtools’.

If no devtools package exists use the function install.packages(“devtools”) or tools -> install packages->packages using the interface of RStudio.

screenshot img

After installing devtools using either of the two ways type the install_github function with "account_name/repository_name" as an argument to install the R package from GitHub.

Consider the example


# Installing ggplot2 from GitHub
install_github("tidyverse/ggplot2")