[R] R as a `script'

Thomas Lumley tlumley at u.washington.edu
Wed Feb 5 17:58:14 CET 2003


On Wed, 5 Feb 2003, Jonathan Baron wrote:

> On 02/05/03 14:30, Havard Rue wrote:
> >
> >is there any way i can use R as a tool for scripts in unix, as
> >
> >wotan[hrue]$ cat example.R
> >#!/usr/bin/R
> >x=1
> >
> >havard
>
> What I do - explained in the R man page - is
>
> R < example.R > example.out
>
> and I have found it useful (for some reason I can't recall) to
> use the --vanilla option in these cases:
>
> R --vanilla < example.R > example.out
>

It's useful because it won't work otherwise -- you need --save, --no-save
or --vanilla.


You can also send small amounts of R code into stdin directly.  For
example, a shell script Sweave.sh to run Sweave can be written as

#!/bin/sh
R --vanilla < SweaveSh.R >/dev/null  $1

with SweaveSh.R being
library(tools)
Sweave(commandArgs()[3])

or more elegantly in a single file as
#!/bin/sh
echo "library(tools);Sweave('"$1"')" | /usr/local/bin/R --vanilla --silent


(the first is mine, the second is from Fritz Leisch).


	-thomas




More information about the R-help mailing list