R syntax

R programming is a very simple programming language used for data analysis. R is very easy because R syntax is very simple. The way we define the R code is quite simple. The basics program for all languages is “Hello World”. In this tutorial, we will try to understand the R syntax using the “Hello World” program. We can write R programming code in two ways either using a command prompt or using an R script file.

1. Using command prompt for R syntax

For writing R code in the command prompt we need to install the R environment setup in our system first. To start the R command just type R in the windows command prompt. When we type R and press enter, it launches an interpreter where we can code the R program.

R syntax
R interpreter

“Hello, World!” Program

The “Hello World!” program in R programming can be written as:

R syntax
R syntax

The above code defines a string variable where we assign the “hello world!” string in the first statement. In the next statement, we used the print() command for printing the value of the variable string

2. Script file for R syntax

There are two ways to open a script file

Using command prompt

Another way of writing R programs is using R script files. We can write an R program and execute these scripts using a command prompt with the help R interpreter called Rscript. We first write the following code and make a text file, after this save the file using .R extension as:

Demo.R

string <-"Hello World!"  
print(string)  

The process of executing this file is the same in windows and other operating systems as mentioned below:

R syntax
Script file command

After pressing enter it will display the following output:

R syntax
“Hello World” output

Using R Studio

We can run an R script in RStudio by going to File > New File > R script in the menu bar. RStudio will then open a fresh script above your console pane.

R Studio
R Studio