Dirk's answer here is everything you need. Here's a minimal reproducible example.
I made two files: exmpl.bat
and exmpl.R
.
exmpl.bat
:
set R_Script="C:Program FilesR-3.0.2inRScript.exe"
%R_Script% exmpl.R 2010-01-28 example 100 > exmpl.batch 2>&1
Alternatively, using Rterm.exe
:
set R_TERM="C:Program FilesR-3.0.2ini386Rterm.exe"
%R_TERM% --no-restore --no-save --args 2010-01-28 example 100 < exmpl.R > exmpl.batch 2>&1
exmpl.R
:
options(echo=TRUE) # if you want see commands in output file
args <- commandArgs(trailingOnly = TRUE)
print(args)
# trailingOnly=TRUE means that only your arguments are returned, check:
# print(commandArgs(trailingOnly=FALSE))
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 the result you'll get:
example.png
with some plot
exmpl.batch
with all that was done
You could also add an environment variable %R_Script%
:
"C:Program FilesR-3.0.2inRScript.exe"
and use it in your batch scripts as %R_Script% <filename.r> <arguments>
Differences between RScript
and Rterm
:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…