[R] how to write text into a file

(Ted Harding) Ted.Harding at nessie.mcc.ac.uk
Fri May 9 10:34:39 CEST 2003


On 09-May-03 Thomas Lumley wrote:
> On Fri, 9 May 2003 Ted.Harding at nessie.mcc.ac.uk wrote:
> <snip>
>> However, when I try the same idea for something more complicated:
>> Ex 2:
>>   ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
>>   trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
>>   group <- gl(2,10,20, labels=c("Ctl","Trt"))
>>   weight <- c(ctl, trt)
>>   zz<-pipe("enscript -o tempout.ps","w")
>>   cat(anova(lm.D9 <- lm(weight ~ group)),sep="\n",file=zz)
>>
>> I get the error message:
>>
>> Error in cat(list(...), file, sep, fill, labels, append) :
>>         argument 1 not yet handled by cat
>>
>> Am I doing something wrong, or is there a gap in this area?
> 
> You can't use cat() on arbitrary objects.
> You could use sink().

Thanks, Thomas! It turns out I'd missed a trick when using 'sink':
when I tried
  sink(file=pipe("enscript -o tempout.ps","w")) 
  anova(lm.D9 <- lm(weight ~ group))
  sink()
it only placed the PS prologue into the file, and I gave up!

Of course I should have realised that the output of anova won't
be flushed into the pipe until the pipe is closed, so you have
to be able to close it which means you need to be able to refer
to it. Now I find that
  zz<-pipe("enscript -o tempout.ps","w")
  sink(zz)
  anova(lm.D9 <- lm(weight ~ group))
  sink()
  close(zz)
works (of course!).

> A fairly minor adjustment to capture.output(), to allow it to take an
> arbitrary connection for its `file' argument works very nicely, and one
> can then do eg
>    capture.output(example(glm), file=pipe("enscript -o
> tempout.ps","w"))

Hmm -- where does one find capture.output()? Doesn't seem to show up in
any libraries I have installed.

> However, I think the proposal was to put text on to graphics devices
> too.
> A similar strategy would probably work, but as it's a class project it
> would be kinder not to go much further in case the problem gets solved
> from under them.

Well, point taken ... but it's also a slightly subtle issue which might
concern any of us, and I think the clarifcation which has emerged is
of general use, just like other discussions on this list which we use
as people might discuss when physically gathered in a coffee-room and
questions come up, only on a global scale.

Anyway, thanks for the stimulation to think this issue out more clearly!
Ted.


--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 167 1972
Date: 09-May-03                                       Time: 09:34:39
------------------------------ XFMail ------------------------------




More information about the R-help mailing list