YES! yes, it’s very simple. I will describe the procedure:
1. You should create the file with code R. Command-line parameters are accessible via commandArgs()
.
2. You can use Rscript
on all platforms, including Windows. It will support commandArgs()
, for example: In the terminal
Rscript myscript.R arg1 arg2 arg3
arg1, arg2 and arg3 are arguments into your R script. If your args are strings with spaces in them, enclose within double quotes. There are two add-on packages on CRAN — getopt and optparse — which were both written for command-line parsing.
Tiny example: script.R
options(echo=TRUE) # To see commands in output file
args <- commandArgs(trailingOnly = TRUE)
# trailingOnly=TRUE means that only your
# arguments are returned
print(args)
start_date <- as.Date(args[1]) # First argument
figure_name <- args[2] # Second argument
n <- as.integer(args[3]) # Third argument
rm(args)
# Some computations:
x <- rnorm(n)
postscript(paste(figure_name,".eps",sep=""))
plot(start_date+(1L:n), x,type="l")
dev.off()
summary(x)
To run:
Rscript script.R 02/06/2015 figure 1000