Kaja
Kaja

Reputation: 3057

Running R script file from command

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

Upvotes: 0

Views: 396

Answers (2)

romants
romants

Reputation: 3648

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.

Upvotes: 6

John C
John C

Reputation: 1981

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.

Upvotes: 3

Related Questions