The hardware and bandwidth for this mirror is donated by METANET, the Webhosting and Full Service-Cloud Provider.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]metanet.ch.

parse_arguments

library(W4MRUtils)

R script command line

Rscript my_script.R --a-integer 42 --a-float 3.14 --a-boolean FALSE --a-list 1,2,3

Parse those parameters in the R script

param_printer <- function(name, args) {
  sprintf(
    "%s[%s] %s",
    name,
    class(args[[name]])[1],
    paste(args[[name]], collapse = " ")
  )
}
args <- W4MRUtils::parse_args(commandArgs())
#> Warning in W4MRUtils::parse_args(commandArgs()): Please, use the 'optparse'
#> library instead of the 'parse_args' function.
args
#> $a_integer
#> [1] 42
#> 
#> $a_float
#> [1] 3.14
#> 
#> $a_boolean
#> [1] FALSE
#> 
#> $a_list
#> [1] "1,2,3"
param_printer("a_integer", args)
#> [1] "a_integer[numeric] 42"
param_printer("a_float", args)
#> [1] "a_float[numeric] 3.14"
param_printer("a_boolean", args)
#> [1] "a_boolean[logical] FALSE"
param_printer("a_list", args)
#> [1] "a_list[character] 1,2,3"
args$a_list <- as.numeric(strsplit(args$a_list, ",")[[1]])
param_printer("a_list", args)
#> [1] "a_list[numeric] 1 2 3"

Keep original strings

param_printer <- function(name, args) {
  sprintf(
    "%s[%s] %s",
    name,
    class(args[[name]])[1],
    paste(args[[name]], collapse = " ")
  )
}
args <- W4MRUtils::parse_args(
  commandArgs(),
  convert_booleans = FALSE,
  convert_numerics = FALSE
)
#> Warning in W4MRUtils::parse_args(commandArgs(), convert_booleans = FALSE, :
#> Please, use the 'optparse' library instead of the 'parse_args' function.
args
#> $a_integer
#> [1] "42"
#> 
#> $a_float
#> [1] "3.14"
#> 
#> $a_boolean
#> [1] "FALSE"
#> 
#> $a_list
#> [1] "1,2,3"
param_printer("a-integer", args)
#> [1] "a-integer[NULL] "
param_printer("a-float", args)
#> [1] "a-float[NULL] "
param_printer("a-boolean", args)
#> [1] "a-boolean[NULL] "
param_printer("a-list", args)
#> [1] "a-list[NULL] "

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.