Here's an example to explain :
Two files are created: example.bat and example.R
-
example.bat:
set R_Script="C:\Program Files\R-3.0.2\bin\RScript.exe"
%R_Script% example.R 2018-01-28 example 100 > example.batch 2>&1
Alternatively, using Rterm.exe:
set R_TERM="C:\Program Files\R-3.0.2\bin\i386\Rterm.exe"
%R_TERM% --no-restore --no-save --args 2018-01-28 example 100 < example.R > example.batch 2>&1
-
example.R:
options(echo=TRUE) # To see commands in output file
args <- commandArgs(trailingOnly = TRUE)
print(args)
# trailingOnly=TRUE this means that only your arguments are returned,
# To check you can use:
# print(commandArgs(trailingOnly=FALSE))
start_date <- as.Date(args[1])
name <- args[2]
n <- as.integer(args[3])
rm(args)
# Few computations:
x <- rnorm(n)
png(paste(name,".png",sep=""))
plot(start_date+(1L:n), x)
dev.off()
summary(x)
Now, save both files in the same directory and start example.bat. In the result you'll get:
- example.png with some plot
- example.batch with all that was done
You can add an environment variable %R_Script%:
"C:\Program Files\R-3.0.2\bin\RScript.exe"
And then use it in your batch scripts as %R_Script% <filename.r> <arguments>
Differences between RScript and Rterm: