How to install Rscript on Ubuntu? [SOLVED]


Ubuntu

Author: Omer Cakmak
Reviewer: Deepak Prasad

R is a programming language and also a software environment for statistical computing and graphics. It is mainly used by statisticians and data miners for developing statistical software and performing data analysis. It is free software supported by the R Foundation and is part of the GNU Bill of Rights.

 

What is an R script?

An R script is simply a text file containing the same commands that you would enter on the command line of R. The file extension of this script is ".R".

In this article, we will prepare an environment for application development with R on Ubuntu. Later, we will talk about running R script with Rscript.

 

How to install R?

The main package for R is called r-base. This package is available in Ubuntu repositories, you can also add the CRAN repository to get a more up-to-date version. We recommend using method 3 to install a specific R version.

 

Method-1: Install From Ubuntu Repository

First update the package repository:

foc@ubuntu22:~$ sudo apt update -y

Then install the r-base package:

foc@ubuntu22:~$ sudo apt install r-base -y

R version in Ubuntu repositories:

foc@ubuntu22:~$ R --version
R version 4.1.2 (2021-11-01) -- "Bird Hippie"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

Installation completed successfully.

 

Method-2: Install From CRAN Repository

CRAN(The Comprehensive R Archive Network)  is a network of ftp and web servers around the world that store identical, up-to-date, versions of code and documentation for R. With this repository you will install a current version.

Update package repositories:

foc@ubuntu22:~$ sudo apt update -y

Install two dependencies:

foc@ubuntu22:~$ sudo apt install --no-install-recommends software-properties-common dirmngr -y

Then download and load the key of the CRAN repository:

foc@ubuntu22:~$ 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
-----BEGIN PGP PUBLIC KEY BLOCK-----

mQENBEy9tcUBCACnWQfqdrcz7tQL/iCeWDYSYPwXpPMUMLE721HfFH7d8ErunPKP
Iwq1v4CrNmMjcainofbu/BfuZESSK1hBAItOk/5VTkzCJlzkrHY9g5v+XlBMPDQC
....
2jiqon7LxHKcTkL+MajTgwrtuH5bI7hL2445uew=
=xbQ3
-----END PGP PUBLIC KEY BLOCK-----

Then add the CRAN repository:

foc@ubuntu22:~$ sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/"
Repository: 'deb https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/'
Description:
Archive for codename: jammy-cran40/ components:
More info: https://cloud.r-project.org/bin/linux/ubuntu
Adding repository.
Press [ENTER] to continue or Ctrl-c to cancel

Adding deb entry to /etc/apt/sources.list.d/archive_uri-https_cloud_r-project_org_bin_linux_ubuntu-jammy.list
Adding disabled deb-src entry to /etc/apt/sources.list.d/archive_uri-https_cloud_r-project_org_bin_linux_ubuntu-jammy.list
Hit:1 http://tr.archive.ubuntu.com/ubuntu jammy InRelease
Hit:2 https://packages.cloud.google.com/apt cloud-sdk InRelease
Get:3 https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/ InRelease [3,626 B]
Hit:4 http://tr.archive.ubuntu.com/ubuntu jammy-updates InRelease
Hit:5 http://tr.archive.ubuntu.com/ubuntu jammy-backports InRelease
Get:6 https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/ Packages [23.6 kB]
Hit:7 http://tr.archive.ubuntu.com/ubuntu jammy-security InRelease
Fetched 27.2 kB in 1s (19.7 kB/s)
Reading package lists... Done

Now install the r-base package from this repository:

foc@ubuntu22:~$ sudo apt install --no-install-recommends r-base -y

R version installed from CRAN repository:

foc@ubuntu22:~$ R --version
R version 4.2.2 Patched (2022-11-10 r83330) -- "Innocent and Trusting"
Copyright (C) 2022 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

 

Method-3: Install Specific R version

You may need a specific version of R for a specific reason. In this step, we will determine a version and install that version.  Update the packages list:

foc@ubuntu22:~$ sudo apt update -y

Add the 4.2.0 version number as environment:

foc@ubuntu22:~$ export R_VERSION=4.2.0

Pull the ".deb" package with cul:

foc@ubuntu22:~$ curl -O https://cdn.rstudio.com/r/ubuntu-2204/pkgs/r-${R_VERSION}_1_amd64.deb
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--  6 60.2M    6 3947k    0     0  2436k      0  0:00:25  0:00:01  0:00:24  9 60.2M    9 5961k    0     0  2287k      0  0:00:26  0:00:02  0:00:24 12 60.2M   12 7776k    0     ...
15 60.2M   15 9738k    0     0  2113k      0  0:00:29  0:00:04  0:00:25 18 60.2M   18 11.3M    0     0  2055k      0  0:00:30  0:00:05  0:00:25
...
100 60.2M  100 60.2M    0     0  1913k      0  0:00:32  0:00:32 --:--:-- 1877k

Install the Debian package:

foc@ubuntu22:~$ sudo apt install ./r-4.2.0_1_amd64.deb

Third party software is installed under the /opt directory. You can access R under opt:

foc@ubuntu22:~$ /opt/R/4.2.0/bin/R --version
R version 4.2.0 (2022-04-22) -- "Vigorous Calisthenics"
Copyright (C) 2022 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

Link the executables under /usr/local/bin:

foc@ubuntu22:~$ sudo ln -s /opt/R/${R_VERSION}/bin/R /usr/local/bin/R
foc@ubuntu22:~$ sudo ln -s /opt/R/${R_VERSION}/bin/Rscript /usr/local/bin/Rscript

You can now access R from anywhere in the terminal:

foc@ubuntu22:~$ R

R version 4.2.0 (2022-04-22) -- "Vigorous Calisthenics"
Copyright (C) 2022 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
...

 

Using R Programming Language (Example)

To use R, simply type R in the terminal and press enter:

foc@ubuntu22:~$ R

R version 4.1.2 (2021-11-01) -- "Bird Hippie"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: x86_64-pc-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.

> print('This article is about R')
[1] "This article is about R"
>
> quit()
Save workspace image? [y/n/c]:

To exit the R console, you must type quit() and press enter.

 

An Example R Script

Create a file with the ".R" extension:

foc@ubuntu22:~$ nano example.R

Write the following script content to this file:

#Example R script
x = 42
y = 25
z = x + y   # addition
w = y/x     # division

#Print
"z"
z
"w"
w

Finally run the example.R script:

foc@ubuntu22:~$ Rscript example.R
[1] "z"
[1] 67
[1] "w"
[1] 0.5952381

The result of the operations to be performed in the script is printed on the screen. In this way, you can code hundreds of lines of code more regularly and get faster results.

 

Summary

You can use the "--help" parameter to get local information about R:

foc@ubuntu22:~$ R --help

Usage: R [options] [< infile] [> outfile]
   or: R CMD command [arguments]

Start R, a system for statistical computation and graphics, with the
specified options, or invoke an R tool via the 'R CMD' interface.

Options:
  -h, --help            Print short help message and exit
  --version             Print version info and exit
  --encoding=ENC        Specify encoding to be used for stdin
  --encoding ENC
  RHOME			Print path to R home directory and exit
  --save                Do save workspace at the end of the session
  --no-save             Don't save it

We installed with 3 different methods. The choice is yours. Our recommendation is to use R by installing packages from the operating system's repositories if there is no reason.

 

References

cran.r-project.org - Install R
docs.rstudio.com - Install R 

 

Omer Cakmak

Omer Cakmak

He is highly skilled at managing Debian, Ubuntu, CentOS, Oracle Linux, and Red Hat servers. Proficient in bash scripting, Ansible, and AWX central server management, he handles server operations on OpenStack, KVM, Proxmox, and VMware. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment