R is a programming language and environment for statistical computing and graphics. It can be considered as a different implementation of the S language with much of the S code running unaltered in R. R provides a wide variety of statistical (linear and nonlinear modeling, classical statistical tests, time-series analysis, classification, clustering) and graphical techniques. In this article we will explain how to:
- Install R on your Ubuntu using the CRAN repositories.
- Write your first/Hello World R program.
- Write your first R script.
We have run the commands and procedures mentioned in this article on a Ubuntu 20.04 LTS system.
Install R on Ubuntu through the CRAN repositories
We will be using the CRAN repositories in order to install the latest version of R on our Ubuntu. The official Ubuntu apt repository does contain R but it is not always the latest version of the software. We will be using the Ubuntu command line, the Terminal, in order to install R; you can access it through the Ubuntu application launcher search or the Ctrl+Alt+T shortcut. Please follow these steps one-by-one:
Step 1: Add the CRAN gpg key
Run the following command as sudo in order to get the CRAN gpg signing key:
$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
The system might as you the password for sudo as only an authorized user can add/remove and configure software on Ubuntu.
Step 2: Add the CRAN repository for installing R
CRAN stands for Comprehensive R Archive Network. It is a network of FTP and web servers around the world that store identical, up-to-date, versions of code and documentation for R. Run the following command as sudo in order to add the CRAN repository to your Ubuntu:
$ sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/'
Step 3: Update repository index
In order to install the latest available version of software from the Internet repositories, your local repository index needs to be in line with them. After adding the CRAN repository, run the following command as sudo in order to update your local repository index:
$ sudo apt-get update
Step 4: Install R Programming Language
Please run the following command as sudo in order to install R from the newly added CRAN repository:
$ sudo apt-get install r-base
The system might ask you the password for sudo and also provide you with a Y/n option to continue with the installation. Enter Y and then hit Enter; R will then be installed on your system. The process may, however, take some time depending on your Internet speed.
Step 3: Verify installation (optional)
You can verify your R installation and also check the version number by running the following command in your Terminal:
$ R --version
The above output shows that R version 3.5.3 is installed on my system; this is the latest available version of R at the time of writing this article.
Your first R program
Now that you have R installed on your Ubuntu, it is time to write your first R language program. Open the Terminal, type R to launch the R console, and hit Enter.
You will now find yourself in the R prompt.
Let us write a simple Hello World program here. Type the following lines:
sampleVariable <- "Hello World" print(sampleVariable)
The first line assigns the string ‘Hello World” to the variable named sampleVariable.
The second line prints the contents of the variable on the screen.
The program prints “Hello World” on your screen, as you can see in the above output.
Creating and Running an R script
The real power of a programming language comes when you can use it in scripts and then in your more complex projects. Let us now learn how to create and run an R-based script in the Ubuntu command line.
Open the Terminal application and enter the following command in order to open an empty file by the name of sampleScript.R:
$ nano sampleScript.R
Now add the following lines to your file:
sampleVariable <- "This text is printed using an R based script!" print(sampleVariable)
Tip: Instead of typing the lines in your file, you can copy it from here and paste in the Terminal by using the Ctrl+Shift+V shortcut, or by using the Paste option from the right-click menu.
Now, quit the file by hitting Ctrl+X, save the file by entering Y and then hitting Enter.
Your R script is now ready to be executed.
Run the following command in order to run the script:
$ Rscript sampleScript.R
The output displays the text we added for printing in the R script.
If you ever have to uninstall R from your system, run the following command as sudo in your Terminal:
$ sudo apt-get remove r-base
The system will as you for a Y/n option to continue with the removal process. Enter Y and hit Enter after which the R will be uninstalled from your system. For complete removal, along with all the configurations you might have made, you can use the following command:
$ sudo apt-get purge r-base
By following the steps described in this article, you can install and use R on your Ubuntu. Your first R program and R script will serve as a basis for you to move to more complex and productive R programs.