How to run R scripts in batch mode with arguments

I recently came across this post by Forester about running R scripts in batch mode (i.e., non-interactive) with arguments. You should definitely check it out since it’s pretty well explained. However, I would like to add my two cents to it by dissecting the process a little more.

Running R in batch mode with arguments is actually a two part process. The first one is the command itself that you type on your console. The command that you need is the following:

R CMD BATCH --no-save --no-restore '--args 1 c(2,3,4) matrix(c(5,6,7,8),nrow=2)' script.R script.out

where -no-save and -no-restore means that you don’t want to save the state of your R workspace after exiting the script and that you don’t want to restore the state of a previously saved workspace. script.R is just the file name of the R script you want to run and script.out is the print out of the script. The most import part of the previous command is

'--args 1 c(2,3,4) matrix(c(5,6,7,8),nrow=2)'

Note that, in this specific case, I’m passing three arguments:

1
c(2,3,4)
matrix(c(5,6,7,8),nrow=2)

It’s imperative that you ONLY use spaces to separate the arguments (and not have spaces within the arguments themselves).

Now the second part is writing the R script that will accept and deal with those three arguments. Here’s an example of it:

# This just reads the two arguments passed from the command line
# and assigns them to a vector of characters.
args <- commandArgs(TRUE)

# Here you should add some error exception handling code
# in case the number of passed arguments doesn't match what
# you expect (check what Forester did in his example)

# Parse the arguments (in characters) and evaluate them
vec1 <- eval( parse(text=args[1]) )
vec2 <- eval( parse(text=args[2]) )
mat1 <- eval( parse(text=args[3]) )

print(vec1)   # prints a vector of length 1
print(vec2)   # prints a vector of length 3
print(mat1)   # prints a 2 x 2 matrix

The parse function creates a expression object out of each (character) argument and the function eval takes care of evaluating them. Here’s the the output of script.out:

$ tail -n 12 script.out

> print(vec1)   # prints a vector of length 1
[1] 1
> print(vec2)   # prints a vector of length 3
[1] 1 2 3
> print(mat1)   # prints a 2 x 2 matrix
     [,1] [,2]
[1,]    1    3
[2,]    2    4
>
> proc.time()
   user  system elapsed
  0.336   0.036   0.344

I’m just tailing the last 12 lines of script.out. The full script.out contains every single line in script.R just like if you were copy and pasting the script into the interactive session.

Advertisements
This entry was posted in R and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s