How to run an R script file from the command line

road_to_quantdom picture road_to_quantdom · Dec 5, 2014 · Viewed 25.2k times · Source

I know there are a lot of questions regarding this issue but I've tried everything and I think I just don't understand how the command line works in windows. I have a file saved in a folder on my desktop, let's say:

C:\Users\abika_000\Desktop\R models\myfile.R

Here is the directory to my R/bin or Rscript/bin:

C:\Program Files\R\R-3.1.0\bin

I want to run this code using the cmd prompt. How would I go about doing this?

I've tried this solution from the question below but I just keep getting errors no matter what I do:

Run R script from command line

What I entered:

> R CMD BATCH C:\Users\abika_000\Desktop\R models\myfile.R
> Rscript C:\Users\abika_000\Desktop\R models\myfile.R

The errors I get are:

'R' is not recognized as an internal or external command, operable program or batch file
'Rscript' is not recognized as an internal or external command, operable program or batch file

EDIT::

Credit to splendour and Phil. I wound up fixing it by entering:

> "C:\Program Files\R\R-3.1.0\bin\"R CMD BATCH "C:\Users\abika_000\Desktop\R models\myfile.R"

I apologize for the poor question. I just wasn't sure how to use the cmd prompt /directories in trying to use R CMD BATCH or Rscript

Answer

mmuurr picture mmuurr · Dec 5, 2014

I think step 1, if you haven't already done it, is to use a UNIX-y shell emulator, such as Cygwin. Then, assuming it's available on Windows (as it is on Linux/OSX), you can use the wrapper program Rscript.

Or, if you have a UNIX-y shell emulator, you can always pipe input straight into R, e.g.:

R --no-save --no-restore < your_script.R