commandArgs
From R.utils v1.9.6
by Henrik Bengtsson
Extract Command Line Arguments
Provides access to a copy of the command line arguments supplied when
this Rsession was invoked. This function is backward compatible with
commandArgs
() of the
- Keywords
- programming
Usage
commandArgs(asValues=FALSE, excludeReserved=FALSE, excludeEnvVars=FALSE, os=NULL, ...)
Arguments
- asValues
- If
TRUE
, a namedlist
is returned, where command line arguments of type--foo
will be returned as - excludeReserved
- If
TRUE
, arguments reserved by Rare excluded, otherwise not. Which the reserved arguments are depends on operating system. For details, see Appendix B on "Invoking R" in An Intro - excludeEnvVars
- If
TRUE
, arguments that assigns environment variable are excluded, otherwise not. As described inR --help
, these are arguments of format= . - os
- A
vector
ofcharacter
strings specifying which set of reserved arguments to be used. Possible values are"unix"
,"mac
- ...
- Passed to
commandArgs
() of thebase package.
Details
This function should be fully backward compatible with the same function in the base package.
Value
- If
asValue
isFALSE
, acharacter
vector
is returned, which contains the name of the executable and the non-parsed user-supplied arguments. IfasValue
isTRUE
, a namedlist
containing is returned, which contains the the executable and the parsed user-supplied arguments. The first element is the name of the executable by which Rwas invoked. As far as I am aware, the exact form of this element is platform dependent. It may be the fully qualified name, or simply the last component (or basename) of the application. The attributeisReserved
is alogical
vector
specifying if the corresponding command line argument is a reserved Rargument or not.
See Also
commandArgs
(), Platform
()
Examples
######################################################################
# Non-parsed command-line arguments
######################################################################
# Display how this instance of R was invoked.
cmd <- paste(commandArgs(), collapse="")
cat("How R was invoked:
");
cat(cmd, "")
# Get all arguments
args <- commandArgs()
print(args)
# Get only "private" arguments and not the name of the R executable.
args <- commandArgs(excludeReserved=TRUE)[-1]
print(args)
# Assert backward compatibility
args0 <- base::commandArgs()
args <- commandArgs()
stopifnot(all.equal(args, args0, check.attributes=FALSE))
######################################################################
# Parsed command-line arguments
######################################################################
# Get all arguments as a named list, e.g. if R is started as:
#
# R DATAPATH=../data --args --root="do da" --foo bar --details --a=2
#
# then 'args' below will equal
#
# list(R=NA, DATAPATH="../data" args=TRUE, root="do da",
# foo="bar", details=TRUE, a="2")
args <- commandArgs(asValue=TRUE)
str(args)
# Turn arguments into R variables
args <- commandArgs(asValue=TRUE, excludeReserved=TRUE)[-1]
keys <- attachLocally(args)
cat("Command-line arguments attached to global environment:
");
print(keys);
str(mget(keys, envir=globalenv()))
Community examples
Looks like there are no examples yet.