Running R script from command line

Ads
 
Tutorials   
Introduction to the ReactJS ReactJs Tutorial How long does it take to learn R Programming? R Programming for Data Science Reserved words in R Programming Downloading and Installing RStudio Running R script from command line R command not found Windows R Hello World Example R Programming Setup Overview of R Programming R Tutorial Question Answer Question-Answer Guidelines Regular Expressions Finding start and end index of string using Regular expression Matching Zip code using regular expression Match string using regular expression without Case sensitivity Pattern resetting using regular expression Replacing all the text of String using expression Reset the matcher using regular expression Split string after each character using regular expression Appending and replacing string using regular expression Finding word using regular expression Pointing and indicating matcher using regular expression Pointing last index of the Matcher Replacing the first subsequence of the input Finding a given word using regular expression Matching Address using regular expressions Retrieve the String using expression Retrieving String after given Regular expression Retrieving String by using Regular Expression Calculating Repetingwords using Regular expression Listing Directory using Regular expression Print Matchingwords using Regular expression Replacing String using Regularexpression Split Demo using RegularExpression Interchanging String using RegularExpression Matching Pattern using Regularexpression

Running R script from command line

In this tutorial you will learn to run R Script from command line.

In this tutorial you will learn to run R Script from command line.

Steps of running R script from command line

In the previous tutorial we developed Hello World program and executed the code from RGui. In this tutorial we will create a file for R script and then run this script from command line.

You can use the development tool such as RStudio or RGui for developing and testing your R Programs, but in production environment you should be able to run the script from command prompt without GUI.

Here are the need of running R Script from command line:

  • For automating the script
  • For Integrating the R Script in production environment
  • Running R programs from other programs in scheduled or real time applications
  • Running R program from Linux cron or Windows scheduler

So, importance of running the R Script from console is very important.

How to run run R Script from command line?

To run the R script from command line you can use the following command:

R < rscript.r --no-save

In the above command rscript.r is the name of r file containing the R code and  --no-save option instruct r not to save the "Work space image".

If you want to save the image you can pass the option --save.

Now create a file with the name hello.r and then add following code:

print("Hello World!")
print("Hello World!", quote = FALSE)
print(paste("Welcome","to","R","Programming"))

Save hello.r file and then open command prompt and go the directory where you file is saved. To run it from command line use following command:

R < hello.r --no-save

Here is the output of the command:

C:\Tutorials2017\rtutorial\examples>R < hello.r --no-save

R version 3.4.0 (2017-04-21) -- "You Stupid Darkness"
Copyright (C) 2017 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> print("Hello World!")
[1] "Hello World!"
> print("Hello World!", quote = FALSE)
[1] Hello World!
> print(paste("Welcome","to","R","Programming"))
[1] "Welcome to R Programming"
>

C:\Tutorials2017\rtutorial\examples>

In this tutorial you learned how to run r script from command line.

R Programming Online Training

Join our online programming training course and learn it from experts.

Ads

Ads