Packages In R - How To Install And Use Packages In R

In this article, we shall learn about the packages what it is in R? We shall see how to install and use it in R studio?

Introduction

R is an important programming language used heavily by statisticians. It is also used in machine learning, data science, research, and many more new fields. It is a computing environment where statistical data may be implemented. In this article, we shall discuss and learn about the packages in R.

Package in R

Packages are add-ons that can extend R's functionality and perform specific tasks covering a wide range of statistics. Packages are the collections of the R functions and data sets. The packages in R is developed by R Studio team as well as individuals.

There are several package (around 5k) available in R. Some of them are standard packages that come with R installation package, such as -  tidyverse, shiny, ggplot2, readxl, devtools, plumber, gdata etc.

Installing Packages in R

We can install a package in R using function install.packages() or following the steps of submenu Install Packages... which is the part of Tools menu. We can also install the packages manually after downloading from R Standard and official website site or from other sources available on Google.

Instailing Package By function install.packages()

To know more about install.packages() function, you can type either ?install.packages() or help(install.packages). It will show the documentation of install.packages() in R Studio as shown in below images.
  1. ?install.packages()  
  2. help(install.packages)   
R

Now, suppose we want to install the package gdata, then we need to type the below command in R Studio.
  1. install.packages("gdata")  
Now, it will show a window as shown in the below image. Basically, it shows the basic information about the package which you have given the command to install for. It shows the source from where it has been downloaded and it also gives the information about the size of the packages. After successful installation, it shows the directory where the packages have been installed.

R

Installing Package By R Studio Tools menu

To install packages in R Studio via Tools menu, click on Tools - Packages. It will show a window as shown below. Once you type the package name, it will automatically show the list of packages matching with your keyword. Here, you can provide multiple package names to be installed at a time. This will save your time. Keep in mind that we should install only a package that we need to use.

R 

If you install a package that is already installed in R Studio, then it will give a warning as shown in the below images.

R 

Using Package in R

So far, we have seen what packages are in R and how to install these. Now, we shall learn how to use a pacakge in R Studio.

To use a package in R, we use library() function as below.
  1. #Working with Ggplot2  
  2. movies <- read.csv("Movie-Ratings.csv"#Reading data  
  3. #Installing Package  
  4. install.packages("ggplot2")  
  5. #Calling library function to use ggplot2 package  
  6. library(ggplot2)  
  7. ggplot(data=movies, aes(x=CriticRating, y=AudienceRating))  
  8. #add geometry  
  9. ggplot(data=movies, aes(x=CriticRating, y=AudienceRating)) +  
  10. geom_point()  
After executing the above code in R Studio, we can see the resultant graph as shown below which has been generated by ggplot() function available in ggplot2 package.

R 

Finding Installied Package in R

By executing the below R script, we can get the list of R packages been installed. It gives the result as shown in below image.
  1. installed.packages(lib.loc = NULL, priority = NULL,  
  2. noCache = FALSE, fields = NULL,  
  3. subarch = .Platform$r_arch)  
R 

Summary

In this article, we have learned about packages in R and saw how to install and use packages in R. 

I hope you have learned and enjoyed reading this article. You’re welcome to like, comment, share and leave any type of suggestion regarding this information.