Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

batch mode quoted parameters parsing

Tags:

r

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!

like image 644
Erwan Avatar asked Aug 12 '11 14:08

Erwan


2 Answers

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.

like image 184
Tommy Avatar answered Oct 12 '22 23:10

Tommy


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

like image 29
R_User123456789 Avatar answered Oct 12 '22 23:10

R_User123456789