• Category
  • >R Programming

Packages in R Programming

  • Lalit Salunkhe
  • Sep 06, 2020
Packages in R Programming title banner

Packages in R programming are one of those fancy things that makes this programming language a go-to tool for data scientists and machine learning engineers. Packages allow the user to manipulate almost any data into a way they want. A user has access for more than 15000 packages in R (this number gets increased every now and then, as users add packages of their own every year) at their fingertips and there is nothing from the field of data science these packages can’t handle in R. Through this article, we will go through the packages, how to load, install, as well as manage them and so much more. If you are new to the field of R Programming, I would recommend you to first go through and get used to this beautiful programming language through the article An Introduction to R Programming.


What are packages in R Programming


In R, we have packages that are a compilation of different functions, procedures, and sample data sets that allow the user to deal with different tasks with ease. These packages are a part of a library. The best thing about R is we can always add a package of our own since it is an open-source language, a lot of users add packages every now and then. In fact, you can add a package of your own to R as well. If you developed a code for doing any task in R which includes different functions of your own and data sets, you can create a package of your own. In other words, we can make a statement that a package in R is a way in which you can manage, organize your work in a structured manner, and if needed, could share with others.


What are Repositories in R Programming?


Well, every package you install must be stored somewhere, isn’t it? The same way your mobile applications are stored in RAM or your computer programs are stored on a C directory etc.. Repositories are the places where all the packages we install in the R environment are stored. Most of the time these repositories are online. However, your organization could have a local repository of its own as well.

There are three widely used online repositories for installing a package in R, mentioned below:

  • CRAN - The full form of CRAN is “Comprehensive R Archive Network”. This is the official repository for R programming which contains thousands of packages that can be installed on your local R environment. You need to make sure that you are connected to a stable internet while accessing the CRAN repository.

  • Bioconductor - The Bioconductor repository is a topic-specific repository and includes all the packages and analysis tools that are associated with the bioinformatics.

  • GithubThis is one of the largest and popular open-source repositories for all the programming languages and is not only associated with R programming. It includes the support towards Python, Java, R, and many more along with packages, extensions, libraries, and what not. 



How to Install a Package in R


To install a package in R, we can use the install.packages() command on R. This function allows us to install a package in R. Make sure the package name is provided as an argument under double-quotes. Well, some of these packages are preinstalled in R and get loaded every time the session starts. However, some of the packages, we need to load every time we start the R session.

You are not aware of the R programming data types much? Please go through the article Data Types in R.

This image shows how to install a package in R using install.packages() function.

Example code for how to install a package in R

If you see the screenshot above, the system shows a lot of lines of code that are associated with the installation process and then a final message that says the package has been installed and the location where it is installed.



How to Remove, Update, and Check Installed Packages


Well, it is as important as installing a package. You know, to manage a one? Because you will always be installing multiple packages in your R environment and what happens when you forget which one you have installed? Which one you have an updated version for? Which one is not of any use and needs to be removed?


To check what are the installed packages in your R environment, you can use the installed.packages() command. This function will return a list of all those packages you already have installed on your R environment as an output. You don’t need to put anything as an argument inside the parentheses for this function to work.

This image shows how the installed.packages() function helps to return a list of installed packages as an output from your R environment.

Example code for how to list down all the installed packages in R

The screenshot above is just a small portion of the list of packages I have installed in my system. Since I do a lot of stuff on R, I usually have a lot of packages installed in my environment. However, your list should be a smaller one compared to mine.


To update the existing packages, you can use the update.packages() function inside the R environment. You can either put a package name as an argument inside this function or it is totally fine to keep it blank. In that case, the system will update all the packages that have the updates available. 

This image shows how the update.packages() function works to update all the outdated functions in R.

Example code how to update the functions in R

See an example code above. In this code, we can see that all of the packages in my system that are outdated are having an update through update.packages() function.


It is again a pretty much straight forward job to remove a package from the R environment. You can always use the remove.packages() function that removes a specified package that you provide as an argument to this function. Make sure you provide the package name under double quotes inside the function as an argument.

The image shows a code that specifies how to uninstall a package from the R environment.

Example code for how to uninstall a package from R

Before moving further, have you read our article on recursive functions? If not, please give a look for this beautiful article here Recursive Functions in R.



How to Load a Package in R Programming


Well, the story doesn’t end as soon as you install a package. To be able to use it and do calculations, you must load it into your library under the R programming environment. That will allow you to use that package for designated tasks. You can use the library() function to load a package into your library. This function takes a package name as an argument without double quotes in it.

This image shows how to use the library() function under R to load a package inside the R environment.

Example code for loading a package inside R

If you see, in this example code we have tried to load the “stats4” package. After loading this package, we can use the functions/procedures as well as the sample datasets (if any present in this package). This package has statistical functions that could perform statistical analysis using S4 class functions.




  • Packages are the blocks that are encapsulated with the formulae, procedures, and sample datasets(sometimes) in R programming. 

  • To install a package inside the R environment, you need to use the install.packages() function. The function requires at least a package name as an argument enclosed in a single/double quote.

  • To update a package, you can use an update.packages() function in R. This function if not provided with a package name as an argument, will update all the packages that are having a new version available.

  • To list down all the packages you have installed on your system, you can use the installed.packages() function. This function doesn’t need any argument under the parentheses. 

  • To remove a package from the R environment, you can use the remove.packages() function. This function takes a packages name as an argument with being enclosed in a single/double quote.

  • To load a package in R, you can use the library() function. The function takes a package name as an argument.


Let us end this article here. If you haven’t read my previous article which beautifully explains how to perform the descriptive statistics in R, you can navigate to the link Descriptive Statistics in R and read it out. Until we come up with something new from the world of R programming, take care! Stay safe! :)

Latest Comments