I am running an R script using Rscript from the command line (linux) like this:
Rscript myscript.R inputfile1.csv inputfile.csv integerArg
My command args are structured like this
cmd_args <- commandArgs()
infile1 <- cmd_args[1]
infile2 <- cmd_args[2]
intArg <- cmd_args[3]
main_func<-function(infile1, infile2, intArg){
print(infile1)
print(infile2)
print(intArg)
}
main_func(infile1, infile2, intArg)
This prints out
[1] "/home/miniconda3/envs/bionano_python3.0/lib/R/bin/exec/R" [1] "--slave" [1] "--no-restore"
I guess I don't have to tell you that the script gives an error the first time it hits one of the input arguments.
I have also tried running the script like this and it gives the exact same output/error
R --slave --no-restore --file=myscript.R --args inputfile1.csv inputfile.csv integerArg
I'm confused because I am not setting these flags when I call Rscript so why are they appearing, and how can I make the commandArgs read only the designated input parameters? Any ideas on how to solve this issue? I can't seem to find any information about why this could be happening and I'm clearly just missing something. Thanks so much in advance.
"That's just the way it is" with Rscript
.
One fix: use commandArgs(trailingOnly=TRUE)
Another fix: use littler
(see littler at CRAN which prunes the arguments similar to trailingOnly=TRUE
and leaves them in `argv[]
And another fix: use eg docopt
which standardizes option process (see docopt at CRAN.
There are a bunch of examples combining littler
and docopt
in the littler repo in inst/examples. I have used a number of those daily for many years. "Works for me" as they say.