11

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!

Wendan Zhong
  • 119
  • 1
  • 1
  • 4

1 Answers1

19

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])  
simonzack
  • 17,855
  • 12
  • 65
  • 107
agstudy
  • 116,828
  • 17
  • 186
  • 250