R Tutorial – We shall learn basic syntax required to write R Script File and execute R Script Example File with Rscript.
R Script File
It is a file with extension “.R” that contains a program (a set of commands). Rscript is the R Interpreter which helps in the sequential execution of R commands in the script file.
R Script File Basic Syntax with Example
Now we shall see a simple R Script that prints “Hello World!” to the console.
# R program to print Hello World
aString = "Hello World!"
print ( aString)
Following is a detailed explanation for each line in the above script file.
- First line is a comment and a comment in R programming language starts with # (hash) symbol. There are only single line comments supported in R language, though there is a work around for multiple line comments.
- Second line is an assignment of value “Hello World!” to the variable aString. <- is assignment operator.
- In the third line, we are printing the variable aString which is holding the value “Hello World!”.
Run R Script File
R language provides an R Interpreter. It can invoked using the command Rscript in the terminal. To run an R script file in the Terminal command prompt, following is the syntax :
$ Rscript <RScriptFileName>.R
The syntax remains same for Windows/MacOS/Linux/Ubuntu.
Save the content of example script file provided in the above section to file with name HelloWorld.R. Please observe that the extension of the file is “.R”. Open a Terminal from the location of HelloWorld.R and run the following command in the Terminal.
$ Rscript HelloWorld.R
The script file would get executed and result in the following output.
 "Hello World!"
In this R Tutorial, we have learnt basic syntax required to write R Script File and execute R Script Example File with R interpreter, Rscript.