I'm trying to use my R script in batch mode, but R doesn't seem able to parse quoted parameters properly:
args=(commandArgs(TRUE))
for(i in 1:length(args)){
print(paste('ARG ',i,args[[i]],sep=" "))
}
Then if a parameter with spaces and quotes is supplied, like:
R CMD BATCH "--args foo=2 bar=3 's=string with spaces'" test-parameters.R output
the output is:
[1] "ARG 1 foo=2"
[1] "ARG 2 bar=3"
[1] "ARG 3 's=string"
[1] "ARG 4 with"
[1] "ARG 5 spaces'"
of course I'd like the third parameter to be s='string with spaces'
: is there a way to obtain that?
Thank you!
Yeah, R CMD BATCH acts a little weird.
Try this instead:
R --slave --vanilla --file=test-parameters.R --args foo=2 bar=3 "s=string with spaces" > output
The --slave and --vanilla options might be replaced with more suitable options as needed.
To stick with R CMD BATCH, I ended passing the argument with a caret where a space is needed, and then performing a gsub("^"," ",argumentPassed,fixed=TRUE)
Batch Script
R CMD BATCH --no-restore "--args automationRDS='//networkLocation/folder/folder/my^filename^has^spaces.RDS'" "\\networkLocation\folder\folder\RScript.R"
Within R
automationRDS <- gsub("^"," ",automationRDS,fixed=TRUE)
I like the logging feature of R CMD BATCH vs Rscript
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With