Wendan Zhong
Wendan Zhong

Reputation: 119

run R script from .bat (batch file)

I am trying to run a r script using batch file. Currently I am using start"" "shortcut of R" to open R. However, I wanna R automatically run a r script that I have saved on the computer.

Possibly R will be closed after running the script and user do not see R running.

Is that possible? Thanks a lot!

Upvotes: 11

Views: 12535

Answers (1)

agstudy
agstudy

Reputation: 121568

Rscript is a non-interactive variant of the standard R command, just designed for this kind of use.

For example, under windows you can define a launcher.bat like this :

PATH PATH_TO_R/R-version/bin;%path%
cd PATH_TO_R_SCRIPT
Rscript myscript.R arg1 arg2

In myscript.R you add the code to read the arguments:

args <- commandArgs(trailingOnly = TRUE)
arg1 <- as.character(args[1])  
arg2 <- as.numeric(args[2])  

Upvotes: 19

Related Questions