Learn R Programming

W4MRUtils (version 1.0.0)

parse_args: Parse Command arguments

Description

parse_args Replacement for the parseCommandArgs utility from batch. Note that inputs like script.R some-list c(1, 2, 3) will result in args$some-list to be the string "c(1, 2, 3)", and not a vector anymore as this ability was permitted by dangerous behaviours from the batch package (the usage of eval which MUST NEVER be used on user's inputs).

To get a list of numeric from users, instead of using the c(1, 2) trick, please, use regular lists parsing:

> args$`some-list`
[1] "1,2"
args$`some-list` <- as.numeric(strsplit(args$`some-list`, ",")[[1]])
> args$`some-list`
[1] 1 2

Usage

parse_args(
  args = NULL,
  convert_booleans = TRUE,
  convert_numerics = TRUE,
  strip_trailing_dash = TRUE,
  replace_dashes = TRUE
)

Value

a named list object containing the input parameters in values and the parameters names in names

Arguments

args

optional, provide arguments to parse. This function will use 'commandArgs()' if args is not provided

convert_booleans

logical - tells the function to convert values into logical if their value is "TRUE" or "FALSE".

convert_numerics

logical - tells the function to convert values into numeric if possible.

strip_trailing_dash
  • tells whether to remove trailing hyphens from the start of the parameter name

replace_dashes
  • tells whether to turn trailing hyphens into underscores

Author

L.Pavot

Examples

Run this code
## faking command line parameters:

commandArgs <- function() {
  list(
    "--args",
    "param1", "a value",
    "param2", "42"
  )
}

## extracting command line parameters:
parameters <- W4MRUtils::parse_args(args = commandArgs())
str(parameters)

Run the code above in your browser using DataLab