Dirk answer is everything you need. I give you small example.
I made two files: exmpl.bat
and exmpl.r
.
exmpl.bat
:
set R_TERM="C:\Program Files\R-2.10.1\bin\Rterm.exe"
%R_TERM% --no-restore --no-save --args 2010-01-28 example 100 < exmpl.R > exmpl.batch 2>&1
exmpl.r
:
args <- commandArgs(trailingOnly = TRUE)
print(args)
# trailingOnly=TRUE means that only arguments after --args are returned
# if trailingOnly=FALSE then you got:
# [1] "--no-restore" "--no-save" "--args" "2010-01-28" "example" "100"
start_date <- as.Date(args[1])
name <- args[2]
n <- as.integer(args[3])
rm(args)
# Some computations:
x <- rnorm(n)
png(paste(name,".png",sep=""))
plot(start_date+(1L:n), x)
dev.off()
summary(x)
Save both files in the same directory and start exmpl.bat
. In result you got:
example.png
with some plot
exmpl.batch
with all what was done
Just to add - you could add environment variable %R_TERM%
:
"C:\Program Files\R-2.10.1\bin\Rterm.exe" --no-restore --no-save
and use it in your batch scripts as %R_TERM% --args .......