Saturday, January 18, 2025
Google search engine
HomeGuest BlogsInstall R and Rstudio on Rocky Linux 9 / AlmaLinux 9

Install R and Rstudio on Rocky Linux 9 / AlmaLinux 9

.tdi_3.td-a-rec{text-align:center}.tdi_3 .td-element-style{z-index:-1}.tdi_3.td-a-rec-img{text-align:left}.tdi_3.td-a-rec-img img{margin:0 auto 0 0}@media(max-width:767px){.tdi_3.td-a-rec-img{text-align:center}}

R is a programming language as well as a software environment for graphical representation of data and statistical computing supported by the R Foundation. This integrated suite can be used for data calculation, manipulation, and graphical display.

It has the following components:

  • An effective data handling and storage facility
  • A set of operators for calculations on arrays, in particular, matrices
  • Graphical facilities for data analysis and display either on-screen or on hardcopy.
  • A large, coherent, integrated collection of intermediate tools for data analysis

R was developed by Ross Ihaka and Robert Gentleman at the University of Auckland. The name R was derived from the first letter of the two R authors. Currently, it is widely used by developers to develop statistical and data analysis software. It is referred to as an environment rather than a programming language to characterize it as a fully planned and coherent system. It offers an environment within which statistical techniques are implemented. R can also be extended through packages provided by the R distribution and via the CRAN family.

.tdi_2.td-a-rec{text-align:center}.tdi_2 .td-element-style{z-index:-1}.tdi_2.td-a-rec-img{text-align:left}.tdi_2.td-a-rec-img img{margin:0 auto 0 0}@media(max-width:767px){.tdi_2.td-a-rec-img{text-align:center}}

R is preferred due to the following:

  • It is free and open-source
  • It is available for Windows, Mac, and Linux.
  • Publication-quality graphs.
  • It exceeds SAS and Stata in terms of the availability of advanced statistical methods and algorithms.

The file types associated with R include:

  • *.rmd: An R Markdown file
  • *.r: An R script
  • *.rnw: An R Sweave file.
  • *.rds: A file containing a single R object. It can be created using saveRDS() and loaded with readRDS()
  • *.rdata: contains one or more R objects/workspaces. It can be created using save() and loaded with load().

Full-time R developers, required an integrated development environment(IDE). The RStudio acts as the integrated development environment for R. It makes development efficient and productive by providing the below features:

  • Compatibility with Windows, Linux, and macOS
  • Console, syntax-highlighting editor that supports direct code execution.
  • Integrated support for Git and Subversion
  • Integrated R help and documentation
  • Extensive package development tools
  • An interactive debugger that helps troubleshoot and diagnose errors
  • Interactive graphics with Shiny and ggvis

RStudio is available in two editions i.e the open-source which is free for use and the commercial edition which is payable with advanced features.

In this guide, we will chew over how to install R and Rstudio on Rocky Linux 9 / AlmaLinux 9.

Prepare Your System

Before we begin, there are a number of packages required. Begin by updating the available system packages:

sudo dnf update

Once the update is complete, enable the EPEL and POwerTools(CRB) on Rocky Linux 9 / AlmaLinux 9.

sudo dnf install epel-release
sudo dnf config-manager --set-enabled crb

1. Install R Rocky Linux 9 / AlmaLinux 9

R can now be installed from, the Rocky Linux 9 / AlmaLinux 9 YUM repositories using the command:

sudo dnf install R

Dependency Tree:

....
Transaction Summary
================================================================================
Install  544 Packages

Total download size: 540 M
Installed size: 1.4 G
Is this ok [y/N]: y

Once the installation is complete, check the installed version.

$ R --version
R version 4.1.3 (2022-03-10) -- "One Push-Up"
Copyright (C) 2022 The R Foundation for Statistical Computing
Platform: x86_64-redhat-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under the terms of the
GNU General Public License versions 2 or 3.
For more information about these matters see
https://www.gnu.org/licenses/.

#2. Getting Started With R

Once installed, R can be used from the console. To launch the R console, execute the command:

sudo -i R 

Sample Output:

R version 4.1.3 (2022-03-10) -- "One Push-Up"
Copyright (C) 2022 The R Foundation for Statistical Computing
Platform: x86_64-redhat-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> 

Now you can execute the desired commands in the R console. For example, installing the stringrpackage, use the command:

install.packages("stringr") 

Select your CRAN and proceed:

35: France (Marseille) [https]
36: France (Paris 1) [https]
37: Germany (Erlangen) [https]
38: Germany (Leipzig) [https]
39: Germany (Göttingen) [https]
40: Germany (Münster) [https]
......
Selection: 37
....
trying URL 'https://ftp.fau.de/cran/src/contrib/stringi_1.7.8.tar.gz'
Content type 'application/x-gzip' length 8032287 bytes (7.7 MB)
==================================================
downloaded 7.7 MB

trying URL 'https://ftp.fau.de/cran/src/contrib/stringr_1.4.0.tar.gz'
Content type 'application/x-gzip' length 135777 bytes (132 KB)
==================================================
downloaded 132 KB
....
* DONE (stringr)
Making 'packages.html' ... done

The downloaded source packages are in
	‘/tmp/Rtmp5yLZgV/downloaded_packages’
Updating HTML index of packages in '.Library'
Making 'packages.html' ... done

Once the installation is complete, we can use the installed package. First, load the library:

library(stringr) 

Create a simple character vector with the name test

test <- c("Klinsmann", "Installed", "R", "on", "RockyLinux", "9") 

Run a function to print the length of each string in the character vector.

str_length(test) 

Sample Output:

R and Rstudio on Rocky Linux 9 AlmaLinux 9 9

To exit the R console, type:

> q()
Save workspace image? [y/n/c]: y

3. Install RStudio on Rocky Linux 9 / AlmaLinux 9

RStudio is the integrated development environment for R. It makes the whole development process easier. To install it on Rocky Linux 9 / AlmaLinux 9 you require OpenSSL 1.1.0 installed. This OpenSSL version is not provided in the Rocky Linux 9 / AlmaLinux 9 repositories. So we will compile and install it.

Install the required build tools:

sudo yum install -y make gcc perl-core pcre-devel wget zlib-devel

Proceed and download OpenSSL 1.1.0 archive:

wget https://www.openssl.org/source/openssl-1.1.0i.tar.gz

Extract the archive and navigate to the directory:

tar -zxf openssl-1.1.0i.tar.gz
cd openssl-1.1.0i

Make and install OpenSSL 1.1.0

./config
make test
make
sudo make install

The make test might with some errors but that doesn’t affect the process much if both make and make install are successful. Once complete, export the PATH:

export LD_LIBRARY_PATH=/usr/local/lib:/usr/local/lib64
echo "export LD_LIBRARY_PATH=/usr/local/lib:/usr/local/lib64" >> ~/.bashrc
source ~/.bashrc

Check the OpenSSL version:

$ openssl version
OpenSSL 1.1.0i  14 Aug 2018

Now download RStudio, by visiting the RStudio downloads page and obtaining the latest release version.

You can also obtain the file using wget as below:

wget https://download1.rstudio.org/desktop/rhel8/x86_64/rstudio-2022.07.2-576-x86_64.rpm

Now install the package with the command:

sudo yum localinstall ./rstudio-*.rpm

Dependency Tree:

...
Total size: 144 M
Total download size: 954 k
Installed size: 579 M
Is this ok [y/N]: y

4. Using Rstudio on Rocky Linux 9 / AlmaLinux 9

To demonstrate if the installed RStudio is working together with R as desired, launch RStudio from the App Menu.

R and Rstudio on Rocky Linux 9 AlmaLinux 9

It will start with the below window.

R and Rstudio on Rocky Linux 9 AlmaLinux 9 1

This shows that RStudio has connected to the available R installation and you are ready to get started. In this guide, we will create a simple project by navigating to File-> New Project

R and Rstudio on Rocky Linux 9 AlmaLinux 9 2

Select the project type.

R and Rstudio on Rocky Linux 9 AlmaLinux 9 3

Provide the project name and path. You can also check the box to use renv in your project if you need it.

R and Rstudio on Rocky Linux 9 AlmaLinux 9 4

Now your project will be ready. I will also demonstrate a few R Programming examples. These include:

A. Simple Arithmetics with R

For example to perform a simple arithmetic such as division, proceed as shown below.

R and Rstudio on Rocky Linux 9 AlmaLinux 9 5

B. Hello World Program

R can also be used to run a simple Hello Word program. The code with the below syntax can be used, with the print() function:

###To Print "Hello World!"
print("Hello World!")

### To suppress Quotes in the output
print("Hello World!", quote = FALSE)

## If there are more than 1 item, we can concatenate using paste()
print(paste("How","are","you?"))

Sample Output:

R and Rstudio on Rocky Linux 9 AlmaLinux 9 6

C. Take Input From User

We can also run an R code that takes input from the user and print the desired output. Below is an example code that takes the name and age of the user and prints the age with +1 year.

##Take Input
my.name <- readline(prompt="Enter name: ")
my.age <- readline(prompt="Enter age: ")

## convert character into integer
my.age <- as.integer(my.age)

##Print the Output
print(paste("Hello,", my.name, "next year you will be", my.age+1, "years old."))

Sample Output:

R and Rstudio on Rocky Linux 9 AlmaLinux 9 7

D. Check Prime Number

You can also use R to check if a number is a prime number or not. This also takes the input from the user and uses the provided code to check if the number is prime or not.

## take input from the user
num = as.integer(readline(prompt="Enter a number: "))
flag = 0

## prime numbers are greater than 1
if(num > 1) {
# check for factors
flag = 1
for(i in 2:(num-1)) {
if ((num %% i) == 0) {
flag = 0
break
}
}
} 
if(num == 2)    flag = 1
if(flag == 1) {
print(paste(num,"is a prime number"))
} else {
print(paste(num,"is not a prime number"))
}

Sample Output:

R and Rstudio on Rocky Linux 9 AlmaLinux 9 8

The end!

We have triumphantly walked through how to install R and Rstudio on Rocky Linux 9 / AlmaLinux 9. You are now set to use R for the graphical representation of data and statistical computing. I hope this was important.

Related posts:

Best Books To Master R Programming in 2022

Install R and RStudio on Ubuntu

Install R & RStudio on CentOS 8 / RHEL 8 Linux

How To Install R & RStudio on Debian

.tdi_4.td-a-rec{text-align:center}.tdi_4 .td-element-style{z-index:-1}.tdi_4.td-a-rec-img{text-align:left}.tdi_4.td-a-rec-img img{margin:0 auto 0 0}@media(max-width:767px){.tdi_4.td-a-rec-img{text-align:center}}

RELATED ARTICLES

Most Popular

Recent Comments