Syntax and Basics for R Programming

This tutorial is designed to give basic insights into R programs and how you can write your first R program in R Studio. Furthermore, you will learn how to save your R program in files and various ways to run the R program and so on. So without wasting time let's get started.

Your first R program in R Studio text editor

If you are a programmer, definitely the first program that strikes your mind is printing the message “ Hello, World!!!”. Why not start with Hello World - You can refer to the R program to print Hello World.

See the below screenshot which shows various ways you can display your message.

R Comments

From our previous tutorial you now know where exactly the output displays. If you are not aware, please feel free to visit our previous tutorial- R STUDIO INSTALLATION FOR WINDOWS. The output of the above program  will be displayed as:

R Comments

R comments

Comments in the R programming language are texts written after the hash(#) to make the code more readable. In the above program, we started our code from the third line with a comment which tells the user from the first sight itself that this is a ‘Hello World Program’. Anything that written after the hash(#) will never get executed as the compiler will only overlook the texts. 

The comments section is not really for the front-end user but the programmers. It helps them to keep track of every section of the program without any interference with the programming structure. 

A little change in the usual syntax of comments will help the user with easy navigation through the code. The change is eclosing the comment in the between # and #### as shown below :


# Comment text  ####
 

You can witness the change in the below example and is really an aid for programmers when writing lengthy code.

R Comments

When you compare the above program with the first, you can see the following:

  • The comments enclosed in # and ####  are listed on the bottom bar.
  • The cursor will move as per the selection and you can observe the position of the cursor. In our example, the cursor is at the position of Method 3- Error.

Note: Since comments are not a programming statement they can

Unlike other programming languages, R does not support multi-line comments. However, in certain situations, you want to comment a bunch of lines at once. Is it possible in R? Yes it is, just do the following instructions;

  • Select the entire section you want to comment
  • From the menu bar choose Code-> Comment/Uncomment lines or use the shortcut key Ctrl +Shift+C.
    R Comments

R Syntax

R Syntax is very simple and anyone can easily understand it in a fraction of a second. Let see 

1.    Syntax for strings

R strings are defined by bounding texts or characters either in double (“……”) quotes or single (‘…….’) quotes.  R is case sensitive so ‘X’ and ‘x’ have different meanings in R.


# Method 1
"Hello, World!!!"
 

The output will be:


> # Method 1
> "Hello, World!!!"
[1] "Hello, World!!!"

In case if we miss the single or double quotes then the output will be a error message like:

> # Method 3 - Error
> Hello, World!!!
Error: unexpected ',' in "Hello,"

2. Syntax for numbers

Defining numbers in R is pretty easy and straightforward as shown. Directly enter the number as you wish and it will be outputted as it is.


# Defining Numbers in R
500
1000
1984
 

The output will be:


> 500
[1] 500
> 1000
[1] 1000
> 1984
[1] 1984

How to run an R Program in R Studio?

R is quite a different language that mostly deals with data science and hence is tricky when it comes to the execution of a program. It is obvious that we can run small and basic programs in R, but the fact is R plays with a huge data set which you will see in your upcoming tutorials. Here in this segment, you will see where the run button is placed in R studio and how to run the R program in RStudio in multiple ways.

To do the execution of codes, R has 3 options in the toolbar.

  1. Run button
  2. Re-Run button which helps to re-run the previous code section
  3. Source button to run the entire document.
How to run an R Program in R Studio?

1.    Running a single line 

To run a single line of code (where currently the cursor resides) you can either use the Run button in the toolbar or use the shortcut: Ctrl + Enter(Windows OS).

2.    Running multiple lines

To run multiple lines of codes, you need to first choose the required lines and then either use the Run button in the toolbar or use the shortcut: Ctrl +Enter (windows OS).

Note: In RStudio, the codes that are executed will be automatically

If you are curious and want to surf more about editing and execution of an R program, please visit How to edit and execute R code in RStudio.

How to save R Script?

How to save R Script?

On observing the above program you can see, by default, the R Script is named Untitled1.R.  You can either keep the name as its or you can change it to a more meaningful one by following the below ways:

  1. Save by clicking on the Save icon on the Script Editor Panel.
    How to save R Script?
  2. Choose File -> Save or the shortcut key Ctrl + S.
    How to save R Script?

In both cases, you will get a window where you can browse your desired location to save the program files and you can input a meaningful name as per your program and then click on the save button will save the code in the new name. See below:

How to save R Script?

Now you can see the R Script name in the File tab on the top of the screen editor changes to Basic_Arithmetic.R

How to save R Script?

Till now you have acquired knowledge on most common commands used in RStudio to function an R program. Last but not least let me remind you of the fact that R is a programming language used to deal with statistical analytics and huge datasets even though R can work with small data. So never underestimate R as a usual programming language in your learning process. Always keep in mind R works with large datasets in a few steps in a matter of time.  For instance, see below example: 

The following code will give you a bar chart as shown below:


x <- c("P", "Q", "R", "S")
y <- c(20, 40, 30, 50)

barplot(y, names.arg = x, col =c("blue","green","yellow","red"))
 

The output will be:

How to save R Script?

In our future tutorial you will grasp more on R programming and will get into more complex problems. So if you are excited go ahead with our upcoming tutorials and extend your knowledge.

To explore more on R examples visit Our R programs.