This blog is clearly dead. Follow me at
https://twitter.com/GregorGorjanc
or
## File file <- "myfile.txt" ## Create connection con <- file(description=file, open="r") ## Hopefully you know the number of lines from some other source or com <- paste("wc -l ", file, " | awk '{ print $1 }'", sep="") n <- system(command=com, intern=TRUE) ## Loop over a file connection for(i in 1:n) { tmp <- scan(file=con, nlines=1, quiet=TRUE) ## do something on a line of data }
## Collect arguments args <- commandArgs(TRUE) ## Default setting when no arguments passed if(length(args) < 1) { args <- c("--help") } ## Help section if("--help" %in% args) { cat(" The R Script Arguments: --arg1=someValue - numeric, blah blah --arg2=someValue - character, blah blah --arg3=someValue - logical, blah blah --help - print this text Example: ./test.R --arg1=1 --arg2="output.txt" --arg3=TRUE \n\n") q(save="no") } ## Parse arguments (we expect the form --arg=value) parseArgs <- function(x) strsplit(sub("^--", "", x), "=") argsDF <- as.data.frame(do.call("rbind", parseArgs(args))) argsL <- as.list(as.character(argsDF$V2)) names(argsL) <- argsDF$V1 ## Arg1 default if(is.null(args$arg1)) { ## do something } ## Arg2 default if(is.null(args$arg2)) { ## do something } ## Arg3 default if(is.null(args$arg3)) { ## do something }
## ... your code here ...