R Script File

R Script file is a file with extension “.R” that contains a program (a set of commands). Rscript is an R Interpreter which helps in the execution of R commands present in the script file.

In this tutorial, we will learn basic syntax required to write R Script File and execute R Script File with Rscript command in Terminal / Console.

R Script File – Example

The following is a simple R Script file named “Example.R” that has instructions to print “Hello World!” to the console. Use R Studio or any text editor to create a file named Example.R in your PC / Mac.


# R program to print Hello World
aString = "Hello World!"
print ( aString)

Following is a detailed explanation for each line in the above script file.

  1. 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.
  2. Second line is an assignment of value “Hello World!” to the variable aString.
  3. In the third line, we are printing the value of variable aString to the standard console output.

Run R Script File

R language provides an R Interpreter. It can be invoked using the command Rscript in the terminal. To run an R script file Example.R in the Terminal command prompt, use the following syntax.

$ Rscript Example.R

The syntax remains same for Windows/MacOS/Linux/Ubuntu.

Open a Terminal from the location of Example.R and run the following command in the Terminal.

$ Rscript HelloWorld.R

R executes the script file Example.R and the following output is printed to the console output.

[1] "Hello World!"


In this R Tutorial, we learned basic syntax required to write R Script File and execute R Script File with Rscript.