Skip to content

Instantly share code, notes, and snippets.

@Btibert3
Last active December 23, 2018 21:45
Show Gist options
  • Select an option

  • Save Btibert3/3dc80cd4ce695fce755d56d0118fd937 to your computer and use it in GitHub Desktop.

Select an option

Save Btibert3/3dc80cd4ce695fce755d56d0118fd937 to your computer and use it in GitHub Desktop.
Command Line Option Parsing for R
options(stringsAsFactors = FALSE)
## load the libraries
library(optparse)
## create the options
parser <- OptionParser()
parser <- add_option(parser, c("-c", "--count"),
type="integer",
default = 5,
help = "How many things to return")
## parse the things
cargs = parse_args(parser)
## do the thing
runif(cargs$count)

To run the script:

Rscript command-testing.R

and this will return the following within the terminal

[1] 0.6045390 0.8331392 0.7229302 0.1976436 0.3871353
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment