[R] how to write text into a file

Thomas Lumley tlumley at u.washington.edu
Fri May 9 02:14:20 CEST 2003


On Fri, 9 May 2003 Ted.Harding at nessie.mcc.ac.uk wrote:

> On 08-May-03 Xirui Yang wrote:
> > Hello,
> >
> > Would somebody tell me how to write the text result into a postscript
> > file?  The command postscript() sents only the graphics output into
> > PostScript files.
>
> Thinking I might have an answer to Xirui's problem, I tried the
> following.
>
> Using R in Linux, which has the program 'enscript' for converting
> a text file as it would appear on screen into PostScript:
> based on examples given in "?pipe" and "?cat", I can make the
> following work for making a postscript file which will display R
> output as it normally appears on screen:
>
<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().

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

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.


	-thomas




More information about the R-help mailing list