The document discusses the Getopt::Long module for parsing command line arguments in R. It provides an example of using Getopt::Long to parse arguments in an R script called getopt_long_sample.R, where the --save.path argument is parsed and assigned to the save.path variable. Getopt::Long allows defining argument types like strings, integers, and flags to parse command line options consistently in both Perl and R scripts.
International Business Environments and Operations 16th Global Edition test b...
R版Getopt::Longを作ってみた
1. R Getopt::Long
Tsukuba.R #9 (2011/11/12)
@a_bicky
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14. parseArgs <- function() {
argv <- commandArgs(trailingOnly = TRUE)
argv <- strsplit(argv, "=")
argv <- sapply(argv, function(arg) {
ret <- ifelse(is.na(arg[2]), 1, arg[2])
names(ret) <- arg[1]
return(ret)
})
# show usage
# Not use 'existsFunction' because loading 'methods' package
takes about 60% of the startup time
if ("help" %in% names(argv)) {
tryCatch(usage(), error = function(e) cat("No usage for
this scriptn"))
quit()
}
# check arguments
# Not use 'existsFunction' because loading 'methods' package
takes about 60% of the startup time
try(checkArgs(argv), silent = TRUE)
return(argv)
}