Learn R Programming

GetoptLong (version 0.1.7)

GetoptLong: Wrapper of the Perl module Getopt::Long in R

Description

Wrapper of the Perl module Getopt::Long in R

Usage

GetoptLong(..., help = TRUE, version = TRUE, envir = parent.frame(), argv_str = NULL,
    head = NULL, foot = NULL, script_name = NULL)

Arguments

...

specification of options. The value should be a vector having even number of elements.

help

whether to add help option. The help message is automatically generated according to user's specification of arguments.

version

whether to add version option

envir

user's enrivonment where GetoptLong will look for default values and export variables

argv_str

command-line arguments, only for testing purpose

head

head of the message when invoking Rscript foo.R --help

foot

foot of the message when invoking Rscript foo.R --help

script_name

alternative script name, internal use

Details

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.

Examples

Run this code
# NOT RUN {
# There is no example
NULL

# }

Run the code above in your browser using DataLab