0

I have a R script and I want to run this file from command line, how can I do that. My file has the name of RScript.R

thank you

1
  • From within R try: ?Rscript Commented Mar 21, 2014 at 13:41

2 Answers 2

6

There are multiple ways

  • Rscript

    Rscript RScript.R

  • R CMD BATCH

    R CMD BATCH RScript.R will produce Rscript.out

R CMD BATCH always goes to a file, taken from the command line if given, or else built from the input filename by appending "out", whereas the output of Rscript goes to stdout. Finally, Rscript accepts user level command-line arguments, after the script name "as usual" (Rscript foo.R 1 2), whereas CMD BATCH accepts them at the end of the options but before the script name, and prefaced by "--args" (R CMD BATCH "--args 1 2" foo.R). In either case, such arguments are available in the R program as the character vector commandArgs(trailingOnly = TRUE).

Preferably Rscript is recommended and frankly is easier to use.

Sign up to request clarification or add additional context in comments.

Comments

3

At least on Ubuntu, you want:

R < RScript.R

or

R < RScript.R > outfile

I assume it's at least similar on other platforms.

Alternatively, you can run the interpreter by itself can call the script from inside the environment with ?RScript.

1 Comment

R --vanilla < /users/rawr/Desktop/Rscript.R on a mac works for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.