Packages in R – Learn to install, load and manage packages

This TechVidvan article is designed to introduce R package basics that every R programmer needs to be aware of before working on it.

While core R is powerful and exciting enough, packages are what makes R truly the must-have tool for data science and machine learning.

In this tutorial, we will explore what packages are in R programming. We will learn how to install them and use them. Finally, we will be showcasing a few useful R packages that make a beginner’s life extremely easy.

What are R Packages?

Packages are collections of functions and datasets that can be installed and used in the R environment. They enhance R’s existing abilities and even provide new ones.

R is an open-source programming language and anyone can make and upload a new package. This means that there are a lot of R packages for almost every use imaginable and more packages are uploaded every day.

The CRAN repository has more than 15000 R packages ready to use.

What is a Repository?

Repositories are places where packages are stored online so that users can download and use them. While anyone can have their own local repository, there are three most popular repositories available online for R packages.

CRAN: CRAN or Comprehensive R Archive Network is the largest repository for R packages. It is maintained by the R foundation. Packages uploaded here need to pass several tests to ensure that CRAN policies are being followed.

Bioconductor: Bioconductor is an open-source repository for bioinformatics packages. Like CRAN, it also has a submission and review process.

Github: Github does not store packages for R alone. It is the largest open-source repository. It has packages, extensions, and libraries for almost all software and languages. Many popular R packages can be found on the Github repository.

Follow TechVidvan on Google & Stay updated with latest technology trends

How to Install an R Package?

To use a package in R, you must first install it and then load it into your library. The library is a directory where packages in the current R session are loaded. You need to load the package into the library for every session.

Don’t have R installed yet? Follow the step by step tutorial to install R.

R already has several packages installed. It loads some of these packages into the library by default while some need to be loaded for every session.

You can use the following command to install a package in R.

install.packages(“<package_name>”)

Here, replace <package_name> with the name of the package that you need to install. Like everything else in R, package names are also case sensitive. For example:

Code:

install.packages("esquisse")

The above command will result in a waterfall of code that details the installation process of the package and all of its dependencies.

install.packages(_esquisse_) - packages in r

How to Load a Package into the Library?

Once you have installed a package, you need to load it into the library to be able to use it. You can do that with the following command.

library(<package_name>)

Here, replace <package_name> with the name of the package you need to load. For example:

library(plyr)

library() - packages in r

Managing Packages

There are several commands in R that can help you with managing the packages you use and install. We can view available packages, view installed and loaded packages. We can check for updates and update them to the latest versions.

You can see a list of the R packages loaded into the current session with the following command.

Code:

search()

Output:

search() - packages in r

You can use the following command to see all the packages installed on the system.

installed.packages()

The installed.packages() command returns a matrix where every package has a row and the columns show details about the packages like their versions, dependencies, library path, license details, etc.. For example:

installed.packages() - packages in r

To update a package, use the following command.

update.packages(<package_name>)

update.packages() - packages in r

You can also leave the parenthesis empty. In this case, R will check all the packages for the latest versions and ask you which ones to update.

Note: We can also install and manage our packages without using the commands. Both Rstudio and RGUI provide options to install and manage packages through the GUI. For example in Rstudio, you can select the tools -> install packages to install new packages and the tools -> check for package updates to update installed packages.

You can remove installed packages using the following command.

remove.packages(“<package_name>”)

Useful R Packages for Beginners

There are many packages available for R programming. So many, that it may be very confusing to decide which ones to use. Here are a few reliable and popular packages that you can use:

  1. Tidyverse – Tidyverse is a collection of packages. It enables the user to clean, process, model and visualize data. The Tidyverse package contains packages like tibble, dplyr, forcats, purrr, readr, ggplot2, stringr, and tidyr.
  2. Rtweet – Twitter is the best available testing ground for sentiment analysis. The rtweet package helps users in this by allowing them to scrape Tweets and organize their content in an easy-to-analyze dataset.
  3. R markdown – The R markdown package is the ultimate report generation package for R. It lets you create rich documents in multiple formats like pdf, HTML, etc. It also allows you to embed R code and its results directly into the reports making them thorough and highly informative with the help of R’s rich and powerful visualizations.
  4. MLR (Machine Learning in R) – MLR is a complete machine learning package for R. It includes all the famous machine learning algorithms used for machine learning projects.
  5. Reticulate – Reticulate is a bridging package. It allows you to use Python alongside with R in the R environment. You can also use various Python libraries within R by using the Reticulate package.
  6. Installr – Installr is a package management package. It helps with keeping track of available updates for all your installed packages. It also allows you to update all your packages with a single command.
  7. Shiny – The Shiny package lets you create interactive web-apps in the R development environment. You can seamlessly embed R code, results and visualization into Shiny apps for highly engaging data-driven stories.
  8. Tensorflow – The Tensorflow package is R’s interface to Tensorflow. It lets you work with the Tensorflow API which is an open-source machine learning library.

Summary

Packages are what make R the flexible, comprehensive and powerful programming language it is. They are the major reason R has stayed relevant to the data science industry for more than two decades.

In this R tutorial, we learned about R packages and the library. We learned how to install packages and how to manage them. We saw how we can update and remove packages as well. Finally, we explored a few useful packages in R that make any R programmer’s life easy.

The R community uploads hundreds of new packages every month. Keep exploring them to find new and exciting features of R and its packages.

Any difficulty while practicing packages in R programming?

Ask our TechVidvan experts.

Keep Practicing!!

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google | Facebook


3 Responses

  1. alps says:

    Thank you again for all the knowledge you distribute,Good post. I was very interested in the article, it’s quite inspiring I should admit. I like visiting you site since I always come across interesting articles like this one.Great Job, I greatly appreciate that.

  2. Unnikrishnan says:

    While many websites imparts knowledge on similar website, I am yet to another one like yours which imparts completing coverage and teaching on any topic you are covering. Great work.

  3. felix muia says:

    your article was so much useful. i think no lecturer can teach me what I have learned from you guys. i wish i could buy you tea someday.

Leave a Reply

Your email address will not be published. Required fields are marked *