Wednesday, June 24, 2015

command line argument in R

http://stackoverflow.com/questions/2151212/how-can-i-read-command-line-parameters-from-an-r-script

Byte-2:R-args hqin$ 
Byte-2:R-args hqin$ cat R-args.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(commandsArgs(trailingOnly=FALSE))

i = as.integer(args[1])
j = as.integer(args[2])
x = seq(i, j)
print(x)



Byte-2:R-args hqin$ R -f R-args.R --args 2 5

R version 3.0.2 (2013-09-25) -- "Frisbee Sailing"
Copyright (C) 2013 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin10.8.0 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> options(echo=TRUE) # if you want see commands in output file
> args <- commandArgs(trailingOnly = TRUE)
> print(args)
[1] "2" "5"
> # trailingOnly=TRUE means that only your arguments are returned, check:
> # print(commandsArgs(trailingOnly=FALSE))
> i = as.integer(args[1])
> j = as.integer(args[2])
> x = seq(i, j)
> print(x)

[1] 2 3 4 5

No comments:

Post a Comment