R is an open-source programming language that can be used to develop statistical software and perform data analysis. R is a GNU project and very similar to the S language. It is mainly used by data analysts, researchers, statisticians and marketers to retrieve, analyze, visualize and present the data. R also offers many packages for specific areas of study, making it easy to find applications in different fields. R can be run on various operating systems including Linux, Windows and MacOS.

In this tutorial, we will show you how to install and use R programming language on Ubuntu 18.04.

Step 1 – Install R Programming Language

By default, R is not available in the Ubuntu 18.04 default repository, so you will need to add CRAN repositories in order to install the latest version of R on your system.

First, install the required packages by running the following command:

apt-get update -y
apt-get install apt-transport-https software-properties-common -y

Next, download and add the GPG key with the following command:

apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9

You should get the following output:

Executing: /tmp/apt-key-gpghome.mOaFpZLCLj/gpg.1.sh --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
gpg: key 51716619E084DAB9: public key "Michael Rutter <[email protected]>" imported
gpg: Total number processed: 1
gpg:               imported: 1

Next, add the CRAN repository with the following command:

add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran35/'

Once the repository has been added, update the repository and install R with the following command:

apt-get update -y
apt-get install r-base -y

After installing R, you can verify the R version with the following command:

R --version

You should see the following output:

R version 3.6.3 (2020-02-29) -- "Holding the Windsock"
Copyright (C) 2020 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 the terms of the
GNU General Public License versions 2 or 3.
For more information about these matters see
https://www.gnu.org/licenses/.

Step 2 – Use R Programming Language

You can open the R console as root with the following command:

sudo -i R

You should get the following output:

R version 3.6.3 (2020-02-29) -- "Holding the Windsock"
Copyright (C) 2020 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.

>

Next, check the default packages installed with R using the following command:

> getOption("defaultPackages")

You should see the following output:

[1] "datasets"  "utils"     "grDevices" "graphics"  "stats"     "methods"

You can also list all available packages with the following command:

> (.packages(all.available=TRUE))

You should get the following output:

 [1] "backports"  "base64url"  "filelock"   "R6"         "txtq"     
 [6] "base"       "boot"       "class"      "cluster"    "codetools"
[11] "compiler"   "datasets"   "foreign"    "graphics"   "grDevices"
[16] "grid"       "KernSmooth" "lattice"    "MASS"       "Matrix"   
[21] "methods"    "mgcv"       "nlme"       "nnet"       "parallel" 
[26] "rpart"      "spatial"    "splines"    "stats"      "stats4"   
[31] "survival"   "tcltk"      "tools"      "utils"     
>

You can also use the library() command to list all available packages with a short description:

> library()

Output:

Packages in library ‘/usr/lib/R/library’:

base                    The R Base Package
boot                    Bootstrap Functions (Originally by Angelo Canty
                        for S)
class                   Functions for Classification
cluster                 "Finding Groups in Data": Cluster Analysis
                        Extended Rousseeuw et al.
codetools               Code Analysis Tools for R
compiler                The R Compiler Package
datasets                The R Datasets Package
foreign                 Read Data Stored by 'Minitab', 'S', 'SAS',
                       'SPSS', 'Stata', 'Systat', 'Weka', 'dBase', ...
graphics                The R Graphics Package
grDevices               The R Graphics Devices and Support for Colours
                        and Fonts
grid                    The Grid Graphics Package
KernSmooth              Functions for Kernel Smoothing Supporting Wand
                        & Jones (1995)
lattice                 Trellis Graphics for R
MASS                    Support Functions and Datasets for Venables and
                        Ripley's MASS
Matrix                  Sparse and Dense Matrix Classes and Methods
methods                 Formal Methods and Classes
mgcv                    Mixed GAM Computation Vehicle with Automatic
                        Smoothness Estimation
nlme                    Linear and Nonlinear Mixed Effects Models
nnet                    Feed-Forward Neural Networks and Multinomial

You can exit from the R console with the following command:

> quit()

Step 3 – Install R Packages from the Console

You can also install R packages within the R console.  First, log in to the R console as a root with the following command:

sudo -i R

Next, install the txtq package using the following command:

> install.packages("txtq")

You should get the following output:

Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
trying URL 'https://cloud.r-project.org/src/contrib/txtq_0.2.0.tar.gz'
Content type 'application/x-gzip' length 10536 bytes (10 KB)
==================================================
downloaded 10 KB

* installing *source* package ‘txtq’ ...
** package ‘txtq’ successfully unpacked and MD5 sums checked
** using staged installation
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (txtq)

The downloaded source packages are in
            ‘/tmp/RtmpHfzZs1/downloaded_packages’
>

You can also remove the installed package with the following command:

> remove.packages("txtq")

Step 4 – Install R Packages from Linux CLI

You can also install the R packages using the command-line interface.

NOTE: If you are still on the R CLI, press CTRL D and select no when prompted to save the workspace.

First, download the txtq package from the CRAN website with the following command:

wget -q https://cran.r-project.org/src/contrib/txtq_0.2.0.tar.gz

Once downloaded, you can install it with the following command:

sudo R CMD INSTALL txtq_0.2.0.tar.gz

Once installed, you should see the following output:

* installing to library ‘/usr/local/lib/R/site-library’
* installing *source* package ‘txtq’ ...
** package ‘txtq’ successfully unpacked and MD5 sums checked
** using staged installation
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (txtq)

Conclusion

That’s it for now. I hope you now have enough understanding of how to install and use R on the Ubuntu 18.04 server. You can now use it to build a statistical software using R programming language on your VPS from Atlantic.Net.