Getopt::Long
in RWrapper of the Perl module Getopt::Long
in R
GetoptLong(..., help = TRUE, version = TRUE, envir = parent.frame(), argv_str = NULL,
head = NULL, foot = NULL, script_name = NULL)
specification of options. The value should be a vector having even number of elements.
whether to add help option. The help message is automatically generated according to user's specification of arguments.
whether to add version option
user's enrivonment where GetoptLong
will look for default values and export variables
command-line arguments, only for testing purpose
head of the message when invoking Rscript foo.R --help
foot of the message when invoking Rscript foo.R --help
alternative script name, internal use
Following shows a simple example. Put following code at the beginning of your script (e.g. foo.R
):
library(GetoptLong) cutoff = 0.05 GetoptLong( "number=i", "Number of items, integer, mandatory option", "cutoff=f", "cutoff to filter results, optional, default (0.05)", "verbose", "print messages" )
Then you can call the script from command line either by:
~\> Rscript foo.R --number 4 --cutoff 0.01 --verbose ~\> Rscript foo.R -n 4 -c 0.01 -v ~\> Rscript foo.R -n 4 --verbose
In above example, number
is a mandatory option and should only be integer mode. cutoff
is optional and already has a default value. verbose
is a logical option. If parsing is
successful, two variables with name number
and verbose
will be imported into the working
environment with specified values, and value for cutoff
will be updated if it is specified in
command-line argument.
For advanced use of this function, please go to the vignette.
# NOT RUN {
# There is no example
NULL
# }
Run the code above in your browser using DataLab