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.
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.
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:
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.
When you compare the above program with the first, you can see the following:
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;
R Syntax is very simple and anyone can easily understand it in a fraction of a second. Let see
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!!!"  "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,"
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  500 > 1000  1000 > 1984  1984
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.
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).
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.
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:
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:
Now you can see the R Script name in the File tab on the top of the screen editor changes to Basic_Arithmetic.R.
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:
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.