R Language Hello World Program

Posted Under: Programming, R, Tutorials on Apr 13, 2020
To get started first we need to install R Programming language on windows. Or if you prefer R on Linux. Then we will write our first "Hello, world!" Program. As needed, you can program at the R language command prompt, or you can write programs using R language script files. If you have configured the R language environment, then you only need to open the command prompt and type R
$ R
This will start the R language interpreter and you will get a prompt> where you can start typing your program, as follows.

> myString <- "Hello, World!"
> print ( myString)
[1] "Hello, World!"
Here, the first statement first defines a string variable myString and assigns "Hello, World!" To it, and the second sentence uses the print () statement to print the content of the variable myString.

R Script Files

Typically, you will perform programming by writing programs in script files, and then use the R interpreter (called Rscript) at the command prompt to execute these scripts. So let's start writing the following code in a text file named test.R
# My first program in R Programming
myString <- "Hello, World!"

print ( myString)
Save the above code in the test.R file and execute it at the Linux command prompt as shown below. Even if you are using Windows or other systems, the syntax will remain the same.
$ Rscript test.R 
When we run the above program, it produces the following result.
[1] "Hello, World!"

Comments in R

Comments can help you interpret scripts in R language programs, and they will be ignored by the interpreter when the program is actually executed. A single comment is written using # at the beginning of the statement, as shown below
# My first program in R Programming
R language does not support multi-line comments, but you can use a little trick, as follows
if(FALSE) {
   "This is a demo for multi-line comments and it should be put inside either a single
      OR double quote"

myString <- "Hello, World!"
print ( myString)
Although the above comments will be executed by the R interpreter, they will not interfere with your actual program. But you must add single or double quotes to the content.
© Tutorial Jinni