Re: [R] scripts with littler

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Mon 08 Jan 2007 - 23:46:23 GMT

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 executable      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@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@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


______________________________________________ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.

Received on Tue Jan 09 17:04:36 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 10 Jan 2007 - 04:30:27 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.