Last active
December 22, 2015 17:28
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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