An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-mac/attachments/20121107/1c0075d9/attachment.pl>
R script in batch mode on Mac
3 messages · didier peeters, Peter Dalgaard, Berend Hasselman
On Nov 7, 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 ?
You need to read up on shell command syntax. In particular, the "cat $i | command" bit would pipe the content of the file $i into the command, which makes no sense when the command is "echo" or "R". Also check up on the use of single and double quotes.
Didier [[alternative HTML version deleted]]
_______________________________________________ R-SIG-Mac mailing list R-SIG-Mac at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-mac
Peter Dalgaard, Professor Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
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