In this tutorial you will learn about the R Hello World Program and its application with practical example.
Creating R Hello World Program
As usual we will start learning R programming by creating “Hello world!” program. The “Hello world!” program is a simplest program that will display some text on the screen. The “Hello world!” program is a simple yet complete program for beginners that illustrates the basic syntax of R programming language. The “Hello world!” program gives you a way to test systems and programming environment.
This tutorial will guide you through writing a basic “Hello World!” program in R. In R Programming, you can do this either by programming it directly in command prompt or you can simply write a program in a file and executing it.
R Command Prompt
Let’s open command prompt and run the following command to start the R Command prompt.
1 |
$ R |
Once you hit the enter, R interpreter will be launched with a prompt symbol as following –
1 |
> |
Here, you can start writing your program. In R Command Prompt, you can print the “Hello World!” in following ways –
Example 1:-
1 2 |
> print("Hello world!") [1] "Hello world!" |
Example 2:-
1 2 3 |
> helloStr <- "Hello world!" > print(helloStr) [1] "Hello world!" |
R Script File
Step 1:- Create a file called “hello_world.R” using a text editor program of your choice. The .R file extension is used to specify Rscript file.
Step 2:- Let’s open the “hello_world.R” file that we created, and put the following line of code in it and save it.
1 2 |
> helloStr <- "Hello world!" > print(helloStr) |
The print() is a function that tells the R interpreter to display or output the content inside the parentheses.
Step 3:- Now, open terminal or command prompt and switch to the folder where our “hello_world.R” file is saved. Run the program by typing the following command –
1 |
Rscript hello_world.R |
Once the program is executed it will print “Hello World!” as below –
Output:-
1 |
[1] "Hello world!" |
Note :- Before executing R Script in the command prompt, PATH has to be added to environment variables in advanced system properties.