Home »
R Language
Hello World program in R programming language
R language Hello World program: In this tutorial, we are going to learn how to make your first program in R programming language?
Submitted by Bhavya Sri Khandrika, on March 18, 2020
Writing first program in R programming
We all know how exciting it will be while working on the console of any programming language for the first time. Adding fun to the knowledge it gives the best results to the user. But before stepping into the coding world an individual needs to know about the basic syntaxes of various aspects that are encompassed in the R language.
We all know that the programmer is the one who solved the real-time problems with his coding technology. But in case he or she fails to understand and frame the code then that would be a mess. Thus, let us start our journey in the R language with the basic hello program.
Beforehand the user needs to open the R command prompt to start the coding part with it's help.
R Command Prompt:
If the R language environment is setup using the installation techniques then the step is to open the command prompt with the help of the following code:
$ R
The above command in the command prompt of the R language will launch the interpreter of R and thereby the programmer can find the symbol >. The > symbol notifies the user to start typing the program according to the need of the user.
Since our main interest is to write the hello world program then let us witness the code which is used to print the hello statement in the R language.
The following code helps us print the string "hello world":
> myString <- "Hello, World!"
> print (myString)
Explanation:
The first statement written in the above-quoted code illustrates a string variable named as myString. Here myString will contain the string "Hello, World!". Once the input is mentioned then it is the type to print the given input string on to the screen. For this purpose, we use a function print() which is predefined in the R language. Thus, the print() function stands responsible for printing the string stored in the myString which is the string variable.
Thus, the final result obtained is the following:
Output
"Hello, World!"
Rscript program
Now let us write the program using the R script. In the conventional method of writing the R programs, one needs to observe that whatever program the user wishes to write he or she will need to write that particular program in the form of script files. Once the program is written using the script files then later he or she can execute it in the command prompt with the help of the R interpreter. The R interpreter that is employed here is called with the name Rscript. Now let us write a text file that includes our hello world program in it. The following code is written in the text file and it is named as text.R
# My first program in R Programming
myString <- "Hello, World!"
print (myString)
Once the above code is saved in the text file with an extension text.R then it is time to run it using the command prompt. One must include the following code to run the code.
Running code:
$ Rscript test.R
However, the code remains the same on all various platforms like Linux, Windows, etc.
Output
"Hello, World!"