Skip to content

Instantly share code, notes, and snippets.

@davetang
Last active December 22, 2015 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davetang/6505873 to your computer and use it in GitHub Desktop.
Save davetang/6505873 to your computer and use it in GitHub Desktop.
An example R script that takes in 2 command line parameters, checks whether these 2 arguments are digits and then sums them.
#!/bin/env Rscript
#usage
usage <- 'Usage: cmd_line_param.R <integer_1> <integer_2>';
#store command line arguments
args <- commandArgs(trailingOnly = T)
#conditional checks
if (length(args) != 2){
print(usage);
} else {
#Perl regular expression for integers
if (length(grep(pattern="^-*\\d+$",x=args,perl=T,value=T)) != 2){
print(usage);
} else {
sum(as.numeric(args));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment