I want to run a R script file (.r) using batch file.
-
if you're still out there could you please help resolve the controversy here (see comment threads below)? – Ben Bolker Oct 25 '11 at 03:26
-
1Hardy: in response to your other question (calling r from .net): see http://stackoverflow.com/questions/5025340/call-r-programming-language-from-net – Ben Bolker Oct 25 '11 at 12:10
-
I don't know what .r script file is; however, you must remember that a Batch file is just an automated way to execute DOS commands, so the answer to your question is: How do you run a R script file with a DOS command? If you can't do that via a command line, then a Batch file can't do either... – Aacini Jul 22 '11 at 16:17
4 Answers
If R.exe is in your PATH, then your windows batch file (.bat) would simply consist of one line:
R CMD BATCH your_r_script.R
otherwise, you need to give the path of R.exe, so for example:
"C:\Program Files\R\R-2.13.0\bin\R.exe" CMD BATCH your_r_script.R
you can add certain input arguments to the BATCH command, such as --no-save
, --no-restore
so it would be
R CMD BATCH [options] your_r_script.R
more info on options, etc at http://stat.ethz.ch/R-manual/R-devel/library/utils/html/BATCH.html
Note: R uses the command "BATCH" to non-interactively evaluate a script located in a file. Here we are running the command "BATCH" from a windows .BAT file, but that's merely a coincidence.

- 15,821
- 6
- 77
- 100

- 3,137
- 1
- 28
- 42
-
3Why flag? Why not just downvote/encourage @mac to edit the answer? This answer tells how to run an R script from the command line, which is the missing piece of information that the OP was looking for, and which neither of the other answers provides ... – Ben Bolker Oct 25 '11 at 03:24
-
@Ben: Did you realize that mac have been a Stack Overflow member for just 3 days and that this answer is his first and only participation in this forum? He has earned all his 12 points from this answer (and your upvote)! I think is very disappointing for me that his answer had caused a -2 qualification in my record... – Aacini Oct 25 '11 at 03:47
-
@Aacini: So? It's a useful answer. As I said, I can't remove my downvote at the moment, but if you edit your answer (even slightly) then I'll remove it ... – Ben Bolker Oct 25 '11 at 03:50
-
4@Aacini: Since some were confused, I've edited my response to be a little more explicit that the code examples provided are what you would include in a Windows/DOS .BAT file. My original response was not, I think you'll find, out of context. – mac Nov 10 '11 at 20:37
An answer to another question suggests using Rscript.exe, so your batch file would contain:
"C:\Program Files\R\R-3.0.2\bin\i386\Rscript.exe" your_script.R
pause
It is a good idea to add R to the windows environment path. In a comment in this question @chase gave a link that explains how to set the path on windows 7. Once R is added to the windows path, your batch file should become simply :
Rscript.exe your_script.R
pause
You can also directly call a R command by using the -e flag. For example this batchfile will tell R to set its current working directory to Documents, then it will print the working directory:
Rscript.exe -e setwd('Documents');getwd()
pause

- 10,289
- 4
- 68
- 110
-
1I prefer this solution with Rscript.exe over mac's solution with R.exe because with Rscript.exe I can see the output of my script (generated e.g. by cat()) on the command prompt, whereas with R.exe I can't see it. – wint3rschlaefer Mar 30 '17 at 08:45
I struggled with the syntax with the answers below, but this worked for me in the .bat file:
C:\Windows\System32\cmd.exe /k ""path to Rscript.exe"
"path to .R script""
Be sure to place both the path to Rscript.exe and the script in "" together and separately as above.

- 340
- 1
- 4
- 17
I doubt you will be able to run it using a batch file.
http://www.fileinfo.com/extension/r Most known programs that use .r files do so for source code files it looks like. You will probably have to compile it using the program it was written for. I guess you could use a command line compiler from a batch file, but I don't know what language or applications you are using.
If you post the script file or give more information about it, we could probably help you better.

- 74
- 7