R is an interpreted programming language. It also allows you to carry out modular programming with the help of functions. It is widely used to analyze statistical information as well as graphical representation.
R allows you to integrate with programming procedures written in C, C++, Python, .Net, etc. Today, R is widely used in the field of data science by data analysts, researchers, statisticians, etc. It is used to retrieve data from datasets, clean it, analyze and visualize it, and present it in the most suitable way.
Run R Programs
You can run R programs in two different ways:
- Installing R in your local machine
- Using an online environment
Install R in Your Local Machine
Before installing R on your computer, you first need to determine the operating system that you are using. R has binaries for all the major operating systems including Windows, MacOS, and Linux.
In this tutorial, you will learn how to install R in a Windows machine. You can follow the steps below:
- Visit https://cloud.r-project.org/ and download the right binary for your operating system. If you want to download the latest version of R in Windows, you can visit https://cloud.r-project.org/bin/windows/base/R-4.1.1-win.exe.
- Once you have finished the download, open the executable file to start the installation. This will start an installation wizard for you.
- Here, select the path where you want to install the R tool. We recommend you to use the default path provided.
- Next, select all the components that you want to install and click the next button.
- The next wizard will ask you to either select a default setup or customize the setup. We recommend you to select the default setup and click on next.
- Wait for a while until the installation concludes. Once done, click on finish.
Voila! You have now successfully installed R on your local computer.
How to Install RStudio?
If you want to work with R in your local machine, installing R is not enough. R does not come with a GUI-based platform. Most users install a separate IDE which allows them to interact with R. It gives them additional functionality such as help, preview, etc.
The most popular IDE for the R programming language is RStudio. You can follow these steps to install RStudio on your Windows machine.
- Visit https://www.rstudio.com/products/rstudio/download/#download to download the free version of RStudio for any platform you want.
- Once the download is completed, you need to open the executable file to start the installation process.
- An installation wizard will appear on the screen. Click on the next button.
- On the next prompt, it will ask you to select the start menu folder for shortcut creation. Click on the install button. Once the installation is completed, click on Finish.
You have now successfully installed RStudio in your local machine.
R Online Compilers
Another way to run R programs is to simply use an online environment. You don't have to go through the hassles of installing R and RStudio in this case. There are lots of competitive R compilers that you can find in a single Google search.
The most commonly used online R compilers are:
You can use any of these R compilers as per your convenience.
Running Your First R Program
Now that you have installed R and RStudio successfully, let's try to create your first R program. We will try to create a simple Hello World
program.
A Hello World program is a simple program that simply prints a "Hello World!"
message on the screen. It's generally used to introduce a new language to learners.
Consider the program below.
message <-"Hello World!"
print(message)
Output
[1] "Hello World!"
Here, we have created a simple variable called message. We have initialized this variable with a simple message string called "Hello World!"
. On execution, this program prints the message stored inside the variable.
Every output in R is preceded by a number (say n) in square brackets. This number means that the displayed value is the nth element printed.