[R] scripts with littler

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Jan 9 00:46:23 CET 2007

He missed

     o	There is a new front-end Rscript which can be used for #!
 	scripts and similar tasks.  See help("Rscript") and 'An
 	Introduction to R' for further details.

and that is needed for #! scripts.  (You cannot write #!/path/to/R -f as R 
is a shell script and so disallowed on most OSes.)

As I understand the earlier question, if you have

#! /usr/env cmd arg1 arg2

/usr/env is passed 'cmd arg1 arg2' as the name of the utility, at least 
under bash which says

    If  the program is a file beginning with #!, the remainder of the first
    line specifies an interpreter for the program.  The shell executes  the
    specified interpreter on operating systems that do not handle this exe-
    cutable format themselves.  The arguments to the interpreter consist of
    a  single optional argument following the interpreter name ...

Note the 'single'.  This is detailed as part of the description of Rscript 
referred to in the NEWS item.  I don't know how universal this is, but the 
Solaris Bourne shell does the same thing.

François Pinard's idea of here documents is nice until you want to read 
from the script's stdin rather than the script itself.

On Mon, 8 Jan 2007, Gabor Grothendieck wrote:

> Looks like it will be possible to write scripts with R 2.5.0 using the
> new -f flag and file("stdin").  From https://svn.r-project.org/R/trunk/NEWS :
>    o	Command-line R (and Rterm.exe under Windows) accepts the options
> 	'-f filename', '--file=filename' and '-e expression' to follow
> 	other script interpreters.  These imply --no-save unless
> 	--save is specified.
> [..]
>    o	file("stdin") is now recognized, and refers to the process's
> 	'stdin' file stream whereas stdin() refers to the console.
> 	These may differ, for example for a GUI console, an embedded
> 	application of R or if --file= has been used.
> On 1/8/07, John Lawrence Aspden <news at aspden.com> wrote:
>> Thanks, that's a really neat mechanism, ( I especially like the note to vim,
>> which will save all my scripts having to end .R )
>> Is there any way to get at the command line and stdio though?
>> With littler I can do things like:
>> #!/usr/bin/env r
>> print(argv)
>> t=read.table(file=stdin())
>> so that I can write unix-style filters.
>> Cheers, John.
>> François Pinard wrote:
>>> I usually do something like:
>>> #!/bin/sh
>>> R --slave --vanilla <<EOF
>>>    R script goes here...
>>> EOF
>>> # vim: ft=r
>>> If you need to search special places for packages, you may tweak
>>> exported environment variables between the first and second line.

Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

More information about the R-help mailing list