R

Load Packages in R

“Creating packages is a good way to make your work/code/program organize and share it with others so they can reuse it. Code, datasets, documentation of those functions which are used inside the package, and some tests are contained by a package. We can download and install the packages from repositories. A repository is a location or platform to store packages so anyone can use them after installation. Organizations and developers each have their local repository, which is usually online and open to the public. CRAN, Bioconductor, and GitHub are some of the most prominent R package repositories.”

What are Packages in R Programming Language in Ubuntu 20.04?

It is a collection of data samples, R functions, and compile scripts. These packages are located in the directory in the R environment, which is known as the library. A set of packages is installed by R during the process of installation. Packages can be added later if they are required for a specific functionality or purpose. When we start the R console, the default packages will be available only for use. To use packages that have been already installed in our system, we have to load them explicitly.

How to Load and use Packages in R Programming Language in Ubuntu 20.04?

You must first install and then load a package into your R library before you can use it. The library is a directory in which the current R session’s packages are loaded. The packages need to be loaded into the library for each session.

Following are some examples in which we will see how to install the packages in R so they can be loaded for use in the R programming language

Example 1:  Installing R Packages

There are different ways to install the packages in R. We will discuss only a few of them in this example.

Installation From CRAN

CRAN is an acronym for Comprehensive R Archive Network. It’s the R community’s official repository, which is updated regularly. To be accepted into CRAN, a package must pass a series of tests to ensure that it complies with CRAN’s rules and policies. To install a package from CRAN, you should know the name of that package, so we can use it in the following command.

The compiler will start the installation process by pressing enter after the above command. Multiple packages can be installed at a time. For this, we will write the names of packages as a character vector.

Installing From Bioconductor

The standard way of installing a package from Bioconductor is passing the following URL inside the source function.

This will install the function that is required to install Bioconductor packages. After installing the required function, you can just write the name of Bioconductor packages or the character vector of their names inside the viocLite() function.

Installing From Github

The R package devtools can be utilized to install a package from GitHub. It will help the users by providing the necessary functions to simplify common tasks in R.

The subsequent instruction can be utilized to install Github packages. You may give a path in place of “package_path” to install the required package.

Example 2:  Loading R Packages

After you are done with the installation of packages in the R environment, to make the packages useful, we must load them in R for the current session.

The library() command can be used to load the R language package, as seen below:

You can write the required package name inside the library function if we need to load a package that was previously installed but isn’t available in the environment which we are using. The following command is used to load a package.

Here lib() is a function that is used to give the path of the library in which the installed R packages are located/stored. To load a file in the current session, we will utilize the instruction below.

In the install.packages() we will specify the path of our package where our package is located. If we just need to access a few functions or data within a package on an occasional basis, we can use the following command.

In the above example, we will use the name of the package from which we want to use a specific function, and we will write the name of the function in the place of “function_name( ).”

R also allows you to unload a package from the current session by using detach() command.

Example 3:  Managing R Packages

In R, you may use some commands to assist you in managing the packages you use and install. We can also see what packages are available, as well as what packages are installed and loaded in our environment. We can check for available updates and make sure they’re up to date. The subsequent command demonstrates a list of the R packages loaded in the current session by using search().

To check which packages are installed in our system, we will use installed.packages() command.

It will provide a matrix with a row for each package and columns for information about the packages like library path, versions, dependencies, license details, and so on.

Installed packages can be upgraded by using the update.packages() command.

If we don’t write the package name in the command, it will check all the packages in our system to update them.

We can also delete a package from our system by using remove.packages() command.

You will write the name of the package in the above command to delete it from your system.

Conclusion

In this article, we tried to teach you the basic concept of R packages and how you can use them in the R programming language in Ubuntu 20.04. Now, you should be capable of installing packages and loading them on your system. We implemented a few examples in this tutorial to explain to you how to install packages from different types of repositories, how you can load them in the current session, how to detach them, how to check the installed packages on your system,  how to update them, and how to remove the packages from your system which are not required anymore.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.