“R” is a programming language that was designed for statistical computing back in 1993. The best thing is that this language is still being used extensively especially for data analysis purposes during research.
”R” is also a free software environment for statistical computing and graphics. It compiles and runs on a wide variety of LINUX platforms, Windows and MacOS.
Installing “R” on Ubuntu 21.04
For installing “R” on your Ubuntu system, you need to perform the following steps:
Update your Ubuntu :
Before installing the package that allows you to use “R” on Ubuntu 21.04, you have to update your system first with the command shown below:
$ sudo apt update && sudo apt -y upgrade
This command will update all those packages and dependencies that need to be fixed before installing any new package on your system.
Install the “r-base” Package :
Now when our Ubuntu system updated, we can conveniently install the “r-base” package on it with the following command:
$ sudo apt install r-base
Once the installation of the “r-base” package will complete on your Ubuntu system, you will be able to use the mathematical functions of the “R” programming language on your system.
Check the Installed Version of “R” on Ubuntu :
You can also verify if “R” successfully installed on your Ubuntu system or not by checking its version with the following terminal command:
$ R --version
How to make your first R program
Running an R program in R console :
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 program here. Type the following lines:
sampleVariable <- "WE LOVE UNIXCOP" print(sampleVariable)
The first line assigns the string ”WE LOVE UNIXCOP” to the variable named sampleVariable.
The second line prints the contents of the variable on the screen.
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 Unixcop.R :
$ vim Unixcop.R
Now add the following lines to your file:
sampleVariable <- "WE LOVE UNIXCOP!" print(sampleVariable)
Save and quit!
Your R script is now ready and you can excute it.
Run the following command in order to run the script:
$ Rscript Unixcop.R
The output displays the text successfully.
Removing “R” from Ubuntu
If you want to remove the “R” programming language , then you will have to remove all the traces of the “r-base” package from your system i.e. the package that allows you to use the “R” programming language. by executing the following two steps:
(1) Remove the “r-base” Package and its Configuration Files :
First, you have to uninstall the “r-base” package and its configuration files by executing the command shown below:
$ sudo apt-get purge r-base
When the “r-base” package and all of its configuration files willbe successfully removed from your Ubuntu system, the execution of the above-mentioned command will terminate while displaying the following messages on the terminal:
(2) Remove the Unused Packages and Dependencies :
Finally, you should also get rid of all the unused packages and dependencies that you do not need any longer after uninstalling “R”. You can do this by executing the command stated below in your terminal:
$ sudo apt-get autoremove
This command will free up all the additional space that was occupied by the unused packages and dependencies and will finish its execution once all such packages will be successfully removed from your system.
By following today’s tutorial, you will be able to install the “R” programming language on your Ubuntu system instantly.
Also you will be able to create and run a “R” script. However, as soon as you feel like you do not want to work with this language anymore, you can conveniently uninstall it from your system to free up your resources.