blog posts

Basic programming syntax training R

For ease of use, learn R programming by writing “Hello World!” (Hello, world!) Let’s start. Depending on your needs; You can write your own program; Write the program in the R command or use the R script for this purpose.

syntax training R, Let’s look at both.

$ R

This will launch R interpreter and you will get to prompt> where you can type your program as follows:

> myString <- “Hello, World!”

> print (myString)

[1] “Hello, World!”

Here, the first expression defines a string variable; Where we assign the phrase “Hello World” to the string variable and then the print () statement is used to print the value stored in the myString variable.

R script file

Usually you will do your programming by writing your programs in script files and then with the help of interpreter R which calls Rscript; Run those scripts in the prompt command. So; Let’s do the following; Let’s start by writing the following code in the text file that test.R calls:

# My first program in R Programming

myString <- “Hello, World!”

print (myString)

Save the above code to a test.R file and run it on the Linux command line as shown below. Even if you use Windows and other systems; The syntax you use; It’s the same.

$ Rscript test.R

When we run the above program; The following result is obtained:

[1] “Hello, World!”

Comments

Comments are the same as the help text in your R program while your actual program is running; These comments are ignored by the program interpreter. Single line comment like below; Using # at the beginning of the phrase; It is written:

# My first program in R Programming

R programming does not support multi-line comments, but you can use a trick that looks something like this:

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)

[1] “Hello, World!”

Although the above comments are executed by the R program interpreter; But they do not interfere with your actual program. You should put these types of comments either in one quote or in two quotes.