R is an open-source widely used programming language utilized in data-related tasks such as statistical computing or graphical methods. R package supports extensively used statistical and scientific catalogs. The R package has several built-in packages useful for data management and solving complex or large data problems.
This guide will provide a complete demonstration to install and setup the R package in Ubuntu 22.04 by utilizing these helping topics:
- Install R Using the Default Repository
- Install R Using CRAN Repository
- Install Packages in R
- Uninstall R From Ubuntu
Method 1: Install R Using the Default Repository
The R package is available in the Ubuntu default apt repository. To install the R package using the default apt package manager, perform the below-mentioned steps.
Step 1: Update the Packages in the Default Repository
Before installing any packages, update the default repository to install the latest version of any package. To update the default repository, run:
The packages in the apt repository are updated to the latest version.
$ sudo apt update
Step 2: Install R Package
To install the R package, run the below command in the terminal:
$ sudo apt install r-base -y
The R package is installed in Ubuntu successfully.
Step 3: Verify R Package is Installed
To verify the installation of the R package in Ubuntu and check its version, run the following command:
$ R --version
The latest R package, “4.1.2” is installed in the system.
Method 2: Install R Using CRAN Repository
In this method, we will explain the steps for installing the R package in Ubuntu 22.04 from Comprehensive R Archive Network (CRAN) repository.
Step 1: Update the Packages in the Default Repository
update the default repository to install the latest version of any package. To update the default repository, run:
$ sudo apt update
The packages in the apt repository are updated to the latest version.
Step 2: Install the Required Packages
Now, install the “software-properties-common” and “dirmngr” packages required for the CRAN repository to manage the external packages and GPG keys:
$ sudo apt install software-properties-common dirmngr
Step 3: Configure GPG keys
Add the R package CRAN key to the trusted user’s keyring. For this, download the CRAN public key using the wget command and add (a option) to the GPG keys by running the below-stated command:
$ 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
The CRAN repository GPG public is added to the trusted users.
Step 4: Add R Repository
After adding the CRAN GPG public key, add the CRAN repository to Ubuntu by executing this command:
$ sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/"
The CRAN repository is added to Ubuntu.
Step 5: Install R Package
To install the R package, run the below command in the terminal:
$ sudo apt install r-base -y
The R package is installed in Ubuntu successfully.
Step 6: Verify R Package is Installed
To verify the installation of the R package in Ubuntu and check its version, run the following command:
$ R --version
The latest R package, “4.1.2” is installed in the system.
How to Set Up R in Ubuntu?
These are the steps to set up R in Ubuntu.
How to Install Packages in R?
Let’s install the “stringr” package from the R shell to manipulate the characters within the string and use it to find the string length. To do this, follow these steps:
Step 1: Open the R Shell
To launch the R shell, run the following command:
$ sudo R
The R shell is launched and shows a prompt to enter the commands.
Step 2: Install stringr Package in the R Shell
To install the “stringr” package, run the following command in the R shell prompt:
> install.packages("stringr")
The package “stringr” is installed in the R shell.
Step 3: Load the Stringr Package
To watch the installed package, we must load the package to the R shell. For this, execute the following command in the R shell prompt:
> library('stringr')
The stringr is loaded to the R shell.
Step 4: Use stringr Package to Check the Length
To calculate the length of every word in the my_string variable, use the below “str_length” built-in function and pass the my_string variable to it:
>my_string <- c("Welcome", "to", "MyLinuxBlog") > str_length(my_string)
It shows the length of every character.
Step 5: Quit the R shell
To exit the R shell in Ubuntu, use this command which will show you a confirmation prompt to save the changes in the R shell:
Note: Enter “n” for no, “y” for yes, or “c” to cancel:
>q()
After entering y it saves the changes and exits the R shell.
Open Package Help in R Shell (Optional)
Moreover, every package available in the R shell has its help available which can be opened by using the help command and passing it the desired package name, as shown below for the stringr package:
>help(stringr)
The output shows the help for the stringr package.
How to Remove R From Ubuntu?
We can remove the R shell in Ubuntu by using the following command:
$ sudo apt remove r-base -y
To remove the package completely, including the package, configuration files, and dependencies, run the below-stated command:
$ sudo apt purge --auto-remove r-base -y
The R package is uninstalled from Ubuntu.
Conclusion
The R package can be installed from the default repository using the command “sudo apt install r-base” or using the CRAN repository. This post has explained the possible methods to install the R package on Ubuntu alongside the steps to set it up.