0

I'm trying to create a batch file to run an R script for me automatically on Windows 7.

Batch file:

"C:\Program Files\R\R-3.2.3\bin\Rscript.exe" CMD BATCH "C:\Users\<my username>\Documents\R\Script Testing\script.R"
pause

R script:

write.csv("hello", "automatic_output.txt")

This script works when I run it in R, but when I try to run the .bat, I get this error:

Fatal error: cannot open file 'CMD': No such file or directory

This happens regardless of whether I paste the code into the command prompt, run the .bat as administrator, or schedule the task.

I tried following the advice given in this thread: Exporting .csv from R & Batch file but am seeing the same behavior.

I also used the Windows search to try to find "automatic_output.txt" in case it was being stored in an unexpected location but no results came up.

Lastly, https://stackoverflow.com/a/23514987 seemed to suggest that I could omit the "CMD BATCH", and when I do I no longer get an error, but automatic_output.txt is still not produced.

Any ideas?

Community
  • 1
  • 1
krock
  • 483
  • 4
  • 11
  • 2
    You should use either `Rscript` or `R CMD BATCH`. There is no `Rscript CMD BATCH`. – Thomas Jun 27 '16 at 20:09
  • Yep you're right, worked like a charm, thanks! – krock Jun 27 '16 at 20:14
  • Great. I've removed your answer from your question, per the rules for the site. Please post it as an answer and accept it when you can (after 24 hours). – Thomas Jun 27 '16 at 20:41

1 Answers1

2

As Thomas pointed out, I needed to change "Rscript.exe" to "R.exe". For future googlers, here is the line of code that worked:

"C:\Program Files\R\R-3.2.3\bin\R.exe" CMD BATCH "C:\Users\<my username>\Documents\R\Script Testing\script.R"

krock
  • 483
  • 4
  • 11