Created
September 19, 2014 01:21
-
-
Save gyli/277143a29e40b2d6e86a to your computer and use it in GitHub Desktop.
Input arguments (flags like) through command line to R
This file contains 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
#Rscript filename.R --host= --dbname= --user= --password= | |
options<-paste0("--",c("host","dbname","user","password")) | |
args <- commandArgs(TRUE) | |
if(length(args)!=4)stop("Please input the full options of host, dbname, user and password.") | |
db.options<-function(option){ | |
option.location <- grepl(option,args) | |
if(sum(option.location)!=1) | |
stop(paste0("Something wrong with ", option, ".")) | |
if(!grepl("=",args[option.location])) | |
stop(paste0("Value needed in ", option, ".")) | |
gsub(paste0("^",option,"="),"",args[option.location]) | |
} | |
config<-vector() | |
for(option in options){ | |
config[gsub("^--","",option)]<-db.options(option) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment