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?

3
  • 2
    You should use either Rscript or R CMD BATCH. There is no Rscript CMD BATCH. Commented Jun 27, 2016 at 20:09
  • Yep you're right, worked like a charm, thanks! Commented Jun 27, 2016 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). Commented Jun 27, 2016 at 20:41

1 Answer 1

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"

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

Comments

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.