Learn R Programming

reader (version 1.0.6)

parse.args: Function to collect arguments when running R from the command line

Description

Allows parameter specification by A=..., B=... in the command line e.g, R < myScript.R M=1 NAME=John X=10.5, using commandArgs()

Usage

parse.args(arg.list = NULL, coms = c("X"), def = 0, list.out = F, verbose = TRUE)

Arguments

arg.list
the result of a commandArgs() call, or else NULL to initiate this call within the function
coms
list of valid commands to look for, not case sensitive
def
list of default values for each parameter (in same order)
list.out
logical, whether to return output as a list or data.frame
verbose
logical, whether to print to the console which assignments are made and warning messages

Value

returns dataframe showing the resulting values [column 1, "value"] for each 'coms' (rownames); or, if list.out=TRUE, then returns a list with names corresponding to 'coms' and values equivalent to 'value' column of the data.frame that would be returned if list.out=FALSE

Examples

Run this code
parse.args(c("M=1","NAME=John","X=10.5"),coms=c("M","X","NAME"))
parse.args(c("N=1")) # invalid command entered, ignored with warning
temp.fn <- "tempScript1234.R"
# make a temporary R Script file to call using the command line
# not run # writeLines(c("require(reader)","parse.args(coms=c('M','X','NAME'))"),con=temp.fn)
bash.cmd <- "R --no-save < tempScript1234.R M=1 NAME=John X=10.5"
# run above command in the terminal, or using 'system' below:
# not run # arg <- system(bash.cmd)
# not run # unlink(temp.fn) # delete temporary file

Run the code above in your browser using DataLab