How to Install R Programming Language on Ubuntu 22.04 LTS

R is an open-source programming language and free software environment for statistical computing and graphical representation created and supported by the R Core Team and the R Foundation. R’s popularity is widely used among statisticians and data miners for software developers’ statistical and data analysis.

In the following tutorial, you will learn how to install R using the CRAN repository and install packages from R’s CRAN repository on Ubuntu 22.04 LTS Jammy Jellyfish using the command line terminal and an example of using CRAN.

Update Ubuntu

First, update your system to ensure all existing packages are up to date.

sudo apt update && sudo apt upgrade -y

Install Required Packages

To complete the installation, you may need to install the following dependencies.

sudo apt install dirmngr gnupg apt-transport-https ubuntu-keyring ca-certificates software-properties-common -y

Import R GPG Key & CRAN Repository

By default, R is present in Ubuntu’s repositories. However, it can get outdated quickly, and it is highly recommended to install R from the CRAN repository and install R base from it.

First, import the GPG required to verify the R package’s authenticity.

wget -O- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | sudo gpg --dearmor | sudo tee /usr/share/keyrings/cran.gpg

Next, import the CRAN repository with the following command.

echo deb [signed-by=/usr/share/keyrings/cran.gpg] https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/ | sudo tee /etc/apt/sources.list.d/cran.list

Once imported, refresh your APT repository list.

sudo apt update

Install R Programming Language

With the dependencies and the newly added CRAN repository installed, you can install the R language by executing the following command in your terminal.

sudo apt install r-base -y

Additionally, you can install the r-base-dev.

 sudo apt install r-base r-base-dev -y

Once the installation has finished, verify if it was successful by checking the build version.

R --version

Example output:

How to Install R Programming Language on Ubuntu 22.04 LTS

Install R Packages from CRAN

Now that R is installed on your system, you can launch the R terminal instance.

In your terminal, use the following command.

sudo -i R

Example out R terminal:

How to Install R Programming Language on Ubuntu 22.04 LTS

R has quite an extensive range of packages that you can install. For the tutorial, the “txtplot” package returns ASCII graphs with “line plot, scatter plot, bar charts, and density plot.” This is installed by using the install.packages(”) command in R’s terminal shell.

install.packages('txtplot')

Example output:

How to Install R Programming Language on Ubuntu 22.04 LTS

With “textplot” now installed, you can run a test by activating the package in the R shell terminal.

library('txtplot')

Next, an example is shown using the example data supplied by R’s “datasets” package containing the speed of cars and the distance required to stop based on data from the 1920s.

txtplot(cars[,1], cars[,2], xlab = 'speed', ylab = 'distance')

From this input command, you will then receive a plot graph.

Example:

How to Install R Programming Language on Ubuntu 22.04 LTS

Additionally, you can use the following command to get help on the packages installed.

help(package name)

Replace “package name” with the package installed. In the tutorial’s case, this was “txtplot”.

Example:

help(txtplot)

To remove a package or “txtplot,” use the following remove command in the R shell terminal.

remove.packages('txtplot')

Use the “q()” command to exit the R shell terminal interface.

q()

You will see the following prompt:

Save workspace image? [y/n/c]: 

Select your option to exit by typing y, n, or c and pressing the “ENTER KEY.”

Comments and Conclusion

One of the best parts about R programming is that it is more of a community-run software, which means that anyone can provide code enhancements and new packages. The consistency in the R community environment is a testament to this approach to developing specific software by sharing and encouraging inputs. This tool is also compatible across platforms, and thereby it runs on many operating systems as well as hardware.

Additionally, you can find more R packages at Available CRAN Packages By Name.



Follow LinuxCapable.com!

Like to get automatic updates? Follow us on one of our social media accounts!