R is a versatile programming language and environment designed specifically for data analysis and statistical computing, making it an incredible choice for data-driven work.
R has gained significant popularity across the data science, data analysis, data visualization, and statistical communities due to its extensive capabilities and active user community.
In this article, you will learn how to install R and RStudio in your preferred Linux distribution and how to run your first program using RScript or RStudio, with practical examples.
Tutorial Details
Description | R and RStudio |
Difficulty Level | Moderate |
Root or Sudo Privileges | Yes |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | – |
Internet Required | Yes |
How to Install R on Linux
The R programming language enjoys widespread popularity and is seamlessly integrated into leading Linux repositories like Ubuntu, Red Hat, and Fedora by default.
It’s great as it makes it easier for any Linux user to hit one command and damn the R installed in their system. Regrettably, a prevailing shortcoming lies in the likelihood that the R version provided within the repository could potentially be outdated.
To get the latest R version, add an external repository to your system. It might involve extra steps, but it’s worthwhile for the latest R release.
However, I’ll show you both ways to install R: via the official repository and by adding and installing from an external repository.
Installing R on Linux via the Official Repository [Outdated]
Reiterating once more, while this method involves just a single step, it might install an outdated version of R. If you want the latest version, follow the next method.
If you just want to give it a shot, nothing is wrong with choosing one of the following commands based on your Linux system to install R from the official repository:
$ sudo apt install r-base #For Debian or Ubuntu
$ sudo dnf install R #For Red Hat or Fedora
$ sudo pacman -S r #For Arch or Manjaro
$ sudo zypper install R-base R-base-devel #For OpenSUSE
Upon finishing the installation, please run the “R --version
” command (“R
” in uppercase) to confirm the presently installed version.
$ R --version
Output:
Although I currently have R version 4.1.2 installed, it’s worth noting that a more recent release, v4.3.1, is available while I’m writing this article.
Now, you can check out this section to get hands-on experience with practical examples if you are using R for the first time.
Installing R on Linux via an External Repository [Latest]
This method involves utilizing the CRAN (Comprehensive R Archive Network), which may introduce a few additional steps, but they are essential to guaranteeing that you have the latest R version.
To proceed with the installation, please choose the appropriate steps below based on your Linux system.
R
” if it’s already installed on your Linux system.Install R on Ubuntu
$ sudo apt update
$ sudo apt install --no-install-recommends software-properties-common dirmngr
$ 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
$ sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/"
$ sudo apt update
$ sudo apt install r-base
Install R on Debian
$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-key '95C0FAF38DB3CCAD0C080A7BDC78B2DDEABC47B7'
$ sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/debian $(lsb_release -cs)-cran40/"
$ sudo apt update
$ sudo apt install r-base
Install R on RHEL 9
$ sudo subscription-manager repos --enable codeready-builder-for-rhel-9-$(arch)-rpms
$ sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm
$ sudo dnf install R
Install R on Fedora
You can directly execute the following command to install the latest version of R:
$ sudo dnf install R
Install R on CentOS Stream
$ sudo dnf config-manager --set-enabled crb
$ sudo dnf install epel-release epel-next-release
$ sudo dnf install R
Install R on Rocky and AlmaLinux
$ sudo dnf config-manager --set-enabled crb
$ sudo dnf install epel-release
$ sudo dnf install R
Install R on OpenSUSE
$ sudo VERSION=$(grep "^PRETTY_NAME" /etc/os-release | tr " " "_" | sed -e 's/PRETTY_NAME=//' | sed -e 's/"//g')
$ sudo zypper addrepo -f http://download.opensuse.org/repositories/devel\:/languages\:/R\:/patched/$VERSION/ R-base
$ sudo zypper install R-base R-base-devel
Install R on Arch
You don’t have to worry about extra steps. Just execute the following command, and it will install the most recent R version from the official Arch repository.
$ sudo pacman -S r
Verify the R Version
Once the installation is successfully completed, proceed by executing the “R --version
” command (“R
” in uppercase) to confirm the installed R version.
$ R --version
Output:
Let’s now explore the practical applications of R following its successful installation.
How to Use R on Linux
The R can be used in two ways: interactive mode or via the script. Let’s begin with
R in Interactive Mode
To initiate interactive mode for R, just execute the following command in uppercase:
$ R
Output:
If you are familiar with the Python programming language, you can quickly spot the similarities between the R console and Python interpreter mode.
Now, in this R console, you can start using the basic commands to print text or perform basic mathematical operations. For example:
> 66+99
> print ("Hello World!")
> print ("Hello World!", quote=FALSE)
Output:
Alternatively, you can create a scatter plot in R using the custom data or by using the “cars
” built-in dataset that contains information about the speed and stop distances of cars.
> plot(cars)
Output:
To exit the R console, you can use the “q()
” command; upon execution, respond with “y” (yes), “n” (no), or “c” (cancel) to save your session data.
> q()
Output:
Running R Programs via Rscript
This method involves creating a script and running it via the Rscript that was included while installing R. It’s an efficient way to run an R program with multiple lines of code.
Let’s say you want to execute the following sets of commands in one go using the Rscript. For that, simply copy the following code into the file named “basic.r
“.
66+99
print ("Hello World!")
print ("Hello World!", quote=FALSE)
And run this R program via the Rscript.
$ Rscript basic.r
Output:
You can also write a complex R program using multiple variables and execute it using the Rscript. Let’s say you want to calculate the mean of a list of numbers using the following code:
# Create a list of numbers
numbers <- c(10, 15, 20, 25, 30)
# Calculate the mean
total <- sum(numbers)
count <- length(numbers)
mean_value <- total / count
# Print the result
cat("Numbers:", numbers, "\n")
cat("Total:", total, "\n")
cat("Count:", count, "\n")
cat("Mean:", mean_value, "\n")
Simply save the above code in a file named “adv.r
” and run it using the following command:
$ Rscript adv.r
Output:
How to Install and Use RStudio on Linux
RStudio is an integrated development environment (IDE) designed specifically for the R programming language.
It provides a user-friendly interface that can enhance the R programming experience by offering a range of features to assist with coding, data analysis, visualization, and more.
It comes in two variants: the open-source free edition and the desktop pro edition, which are identical except that the professional edition provides support and additional drivers.
As a beginner, you can go for the open-source free edition by downloading the appropriate package based on your Linux system from the official website.
Once the package is downloaded, execute the following command to install some dependencies like libcurl, libssl, and libxml before installing RStudio.
$ sudo apt install libcurl4-openssl-dev libssl-dev libxml2-dev #For Debian or Ubuntu
$ sudo dnf install libcurl-devel openssl-devel libxml2-devel #For Red Hat or Fedora
Then navigate to the downloaded directory and execute one of the following commands based on your Linux system to initiate the installation process:
$ sudo dpkg -i rstudio-*.deb #For Debian or Ubuntu
$ sudo dnf install rstudio-*.rpm #For Red Hat or Fedora
$ sudo zypper in rstudio-*.rpm #For OpenSUSE
Once it’s installed, search for it in the application menu, and the home window of the application will look like the following:
Here, you already have the interactive mode launched in the “Console” tab of the RStudio home window, where you can run R code.
To create a new file, just click on “File” and then select “New File” => “R Script” (or use the shortcut “CTRL+SHIFT+N“):
Copy the following content into it:
# Create a list of numbers
numbers <- c(10, 15, 20, 25, 30)
# Calculate the mean
total <- sum(numbers)
count <- length(numbers)
mean_value <- total / count
# Print the result
cat("Numbers:", numbers, "\n")
cat("Total:", total, "\n")
cat("Count:", count, "\n")
cat("Mean:", mean_value, "\n")
Output:
Press “Ctrl+S” to save the file in the appropriate location with your desired filename.
To run the program, simply click on the “Run” button. You should be able to see the output in the “Console” tab and the environments in the “Environment” tab.
Congratulations on running your first RScript in RStudio! Now I’ll leave the rest of it to you.
Final Word
I hope you find this article useful, and if you have any questions or queries related to this topic, then feel free to tell us via the comment section.
Till then, peace!
Join The Conversation
Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.