[R] writing R shell scripts?

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Nov 9 08:36:57 CET 2005


On Tue, 8 Nov 2005, Mike Miller wrote:

> On Wed, 9 Nov 2005, Henrik Bengtsson wrote:
>
>> What you really want to do might be solved by write.table(), e.g.
>>
>> x <- matrix(rnorm(25*2),c(25,2));
>> write.table(file=stdout(), x, row.names=FALSE, col.names=FALSE);
>
> Thanks.  That does what I want.
>
> There is one remaining problem for my "echo" method.  While write.table
> seems to do the right thing for me, R seems to add an extra newline to the
> output when it closes.  You can see that this produces exactly one newline
> and nothing else:
>
> # echo '' | R --slave --no-save | wc -c
>       1
>
> Is there any way to stop R from sending an extra newline?  It's funny
> because normal running of R doesn't seem to terminate by sending a newline
> to stdout.  Oops -- I just figured it out.  If I send "quit()", there is
> no extra newline!  Examples that send no extra newline:
>
> echo 'quit()' | R --slave --no-save
>
> echo "x <- matrix(rnorm(25*2),c(25,2)); write.table(file=stdout(), x, row.names=FALSE, col.names=FALSE); quit()" | R --slave --no-save
>
> I suppose we can live with that as it is.  Is this an intentional feature?

Yes.  You get a prompt (suppressed here) and did not give an input for it, 
so a newline is needed to finish the line.

R scripts should end in 'q()'.

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