DevOps

How to Install R on Ubuntu

Guide on how to install R on Ubuntu

Introduction

R is an open-source programming language and environment for statistical computing and data visualization. R’s extensive collection of libraries and packages supports various statistical techniques and models such as hypothesis testing, regression analysis, time series analysis, and more.

Data scientists and statisticians use R to manipulate, calculate, and graphically display data in the form of statistical plots and graphs.

Learn how to install R on Ubuntu 20.04 or Ubuntu 22.04 and start working with datasets in no time.

Guide on how to install R on Ubuntu

Prerequisites

How to Install R on Ubuntu

There are two ways to install R on Ubuntu:

  • Install R from Ubuntu repositories. The installation process is simple and requires only a few steps. However, since the default Ubuntu libraries are not updated as frequently as the CRAN repositories, there is a possibility of installing an older version of R.
  • Install R from the CRAN repository. CRAN (Comprehensive R Archive Network) is backed by the R Foundation and serves as the primary software repository for R. It hosts a collection of current and past versions of the R distribution, along with documentation and contributed R packages. While the installation process from CRAN may involve additional steps compared to the Ubuntu repositories method, it always includes the latest available R version.

Note: The provided commands can be used for both Ubuntu 20.04 (Focal Fossa) and 22.04 (Jammy Jellyfish).

Method 1: Install R from Ubuntu Repository

To install R from the Ubuntu repository:

1. Open the terminal and update the apt package lists:

sudo apt update
Updating apt packages in Ubuntu for R installation.

2. Install the R base package and its dependencies using the following command:

sudo apt install r-base r-base-dev -y
Installing R from Ubuntu repository.

The r-base-dev package enables users to install new packages using the install.packages() function within the R console.

3. Type R and hit enter to verify that R has been installed:

R

Note: Using the R command without sudo creates a personal library for your user. To install packages available to every user on the system, run the R command as root by typing sudo -i R.

Accessing the R console from Ubuntu.

R has been successfully installed, and you have access to the R console prompt.

Type q() to exit the R console.

Method 2: Install R on Ubuntu from CRAN Repository

To install R from the CRAN repository:

1. Update the apt package lists:

sudo apt update
Update apt packages before installing R from CRAN.

2. Install the software-properties-common and dirmngr helper packages for managing CRAN repositories and certificates:

sudo apt install software-properties-common dirmngr -y
Install CRAN helper packages when installing R on Ubuntu.

3. Download and add the signing key for the CRAN repository:

wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | sudo tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
Download and add CRAN repo key.

4. The CRAN repository key fingerprint is E298A3A825C0D65DFD57CBB651716619E084DAB9. Verify the downloaded key using the following command:

gpg --show-keys /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
The CRAN repo key fingerprint.

5. Add the CRAN repository to your system:

sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/"

When prompted, press Enter to start the download.

Add the CRAN R repo in Ubuntu.

Using $(lsb_release -cs) instructs the system to automatically recognize which Ubuntu version your machine is running.

Note: Alternatively, you can replace $(lsb_release -cs) with your Ubuntu version, for example, jammy for Ubuntu 22.04 or focal for Ubuntu 20.04.

6. Enter the following command to install the base R package and its dependencies from the CRAN repository:

sudo apt install r-base r-base-dev -y
Command to install R from CRAN repository.

The r-base-dev package allows users to install packages from the R console using the install.packages() function.

7. Enter the R command to access the R console:

R

Note: Using the R command without sudo creates a personal library for your user. To install packages that are available to every user on the system, run the R command as a root user by typing sudo -i R.

Installed R version from CRAN repository.

You have successfully installed the latest R version (4.3.0) from the CRAN repository.

Installing R Packages

The R community actively develops and maintains thousands of packages for specific tasks like data visualization, machine learning, statistical modeling, text mining, etc. Some of the most popular R packages include:

  • dplyr. This package provides practical and intuitive functions for summarizing, filtering, and transforming data.
  • tidyr. The tidyr package enables users to reshape and organize messy data into a tidy format.
  • purrr. Statisticians use the purrr functional programming package to improve flexibility and maintain consistency when working with functions and vectors.
  • ggplot2. ggplot2 is a versatile R package for creating and customizing visually appealing plots.
  • data.table. The high-performance data.table package is primarily used for data manipulation in large datasets.
  • stringr. The stringr package provides functions for streamlining string and text manipulation tasks.

Users can install R packages from within the R console by utilizing the install.packages("package_name") function.

For example, to install the ggplot2 package from the CRAN repository, access the R console and enter the following command:

install.packages("ggplot2")

Note: To install a different package, replace ggplot2 with the name of the desired package.

Install the ggplot2 package in R.

If you encounter the 'lib = "/usr/local/lib/R/site-library"' is not writable prompt, type yes to use and create a personal library for your user.

Once the installation process is complete, load the ggplot2 library:

library(ggplot2)
Loading the ggplot2 package in R.

The ggplot2 package comes with preinstalled datasets to allow users to test the features. Use the data() function to view a list of preinstalled datasets:

data()
List of preinstalled ggplot2 datasets.

Press q to exit the dataset list and return to the R console.

In this example, the summary() function is called to review the variables in the Orange dataset:

summary(Orange)

A summary of a dataset in R.

Use the following command to plot the numeric and categorical variables in the Orange dataset:

ggplot(Orange, aes(x=Tree, y=circumference, col=age))+geom_point()
Creating a ggplot2 statistical plot in R.

R generates a visual statistical plot in a new window.

A statistical plot created using ggplot2 in R.

The ggplot2 package is one of many R packages for static data visualization. CRAN hosts thousands of packages designed for a wide range of data-related tasks.

You can explore and discover more packages by browsing the official CRAN package repository.

Note: Learn about differences between R and Python.

Conclusion

You have installed R on Ubuntu and can use its functions and environment to extract valuable insights from complex datasets.

It is common for researchers to use R in tandem with Python libraries like pandas. By combining R and Python, you can take advantage of the vast range of packages and functionalities available in both ecosystems.

Đăng ký liền tay Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !

Add Comment

Click here to post a comment

Đăng ký liền tay
Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !