[R-SIG-Mac] R script in batch mode on Mac

Berend Hasselman bhh at xs4all.nl
Wed Nov 7 13:11:21 CET 2012


On 07-11-2012, at 12:08, didier peeters wrote:

> Hi list,
> 
> I would like the run a R script in a batch mode.  So i use a command line like this :
> 
> for i in *.xlsx; do cat $i | echo 'argv <- $i; source("path/to/script.R")' | R --vanilla --slave ; done
> 
> which gives the error " Erreur : '$' inattendu(e) dans "argv <- $" "
> 
> i've also tried  :
> 
> for i in *.xlsx; do cat $i | R CMD BATCH --args $i "path/to/script.R" ; done
> 
> which gives me nothing.  
> 
> The idea is to read dozens of similar excel files in a folder with gdata package and to write their content in another single file.  
> My script works fine with a read.xls when i specify a file name, but and here I get the name of the file with :
> argv <- commandArgs(TRUE)
> 
> This is all I've been able to find from various docs and I'm not familiar with the command line.  
> What am I doing wrong ?
> Could anyone help me ?


Try Rscript like this

for k in *.R; do
    Rscript -e "source('path/to/script.R')" $k
done

and get the name of the file to process from commandArgs()

or

Rscript -e "source('path/to/script.R')" *.xlsx

and loop over the elements of commandArgs() in the script.

or

use list.files() -- something like list.files(pattern="^.*\\.xlsx") -- in the script then you don't need to loop in the shell script.

Berend



More information about the R-SIG-Mac mailing list