Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a R language(.r) file using Batch file?

Tags:

I want to run a R script file (.r) using batch file.

like image 669
Hardy Avatar asked Jul 22 '11 10:07

Hardy


People also ask

How do I run an R program in terminal?

Starting R If R has been installed properly, simply entering R on the command line of a terminal should start the program. In Windows, the program is typically specified as the action performed when clicking on an icon. You can also use this method on a *NIX system that has a window manager such as KDE.

What is R in batch file?

/r - iterate through the folder and all subfolders, i.e. recursively. %%I - placeholder for a path/filename. The above command simply lists all files in the current folder and all subfolders. Follow this answer to receive notifications.


2 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.

like image 122
mac Avatar answered Oct 17 '22 10:10

mac


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 
like image 41
Paul Rougieux Avatar answered Oct 17 '22 10:10

Paul Rougieux