[R] Is there an equivalent of "echo"

Greg Snow Greg.Snow at imail.org
Mon Sep 14 23:19:53 CEST 2009


The cat() function has already been pointed out to you (and that may be enough), but for a little more elaborate alternative, see the txtStart and txtComment functions in the TeachingDemos package for a glorified wrapper to sink/cat/etc.

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Polwart Calum (County Durham and Darlington
> NHS Foundation Trust)
> Sent: Monday, September 14, 2009 3:11 PM
> To: r-help at r-project.org
> Subject: [R] Is there an equivalent of "echo"
> 
> Sorry I'm having one of those moments where I can't find the answer but
> I bet its obvious...
> 
> I'm outputting my results to a file using sink()
> 
> Is there a command simillar to php's echo command that would allow me
> to add some text to that file ie:
> 
> dataFr$a = 1:10
> dataFr$b = 2*1:10
> sink ("filepath/filename.txt", split=T)
> #Show number of entries in vector a
> table (dataFr$a)
> #show number of entries in vector b
> table (dataFr$b)
> #show relationship between a and b
> table (dataFr$a , dataFr$b)
> sink()
> 
> Gives me a text file like this:
>  1  2  3  4  5  6  7  8  9 10
>  1  1  1  1  1  1  1  1  1  1
> 
>  2  4  6  8 10 12 14 16 18 20
>  1  1  1  1  1  1  1  1  1  1
> 
>      2 4 6 8 10 12 14 16 18 20
>   1  1 0 0 0  0  0  0  0  0  0
>   2  0 1 0 0  0  0  0  0  0  0
>   3  0 0 1 0  0  0  0  0  0  0
>   4  0 0 0 1  0  0  0  0  0  0
>   5  0 0 0 0  1  0  0  0  0  0
>   6  0 0 0 0  0  1  0  0  0  0
>   7  0 0 0 0  0  0  1  0  0  0
>   8  0 0 0 0  0  0  0  1  0  0
>   9  0 0 0 0  0  0  0  0  1  0
>   10 0 0 0 0  0  0  0  0  0  1
> 
> What I'd like is to be able to add some headers in the text file maybe
> like this:
> 
> sink ("filepath/filename.txt", split=T)
> echo "Number of entries in vector a"
> table (dataFr$a)
> echo "number of entries in vector b"
> table (dataFr$b)
> echo "relationship between a and b"
> table (dataFr$a , dataFr$b)
> sink()
> 
> Giving an output like:
> Number of entries in vector a
>  1  2  3  4  5  6  7  8  9 10
>  1  1  1  1  1  1  1  1  1  1
> number of entries in vector b
>  2  4  6  8 10 12 14 16 18 20
>  1  1  1  1  1  1  1  1  1  1
> relationship between a and b
>      2 4 6 8 10 12 14 16 18 20
>   1  1 0 0 0  0  0  0  0  0  0
>   2  0 1 0 0  0  0  0  0  0  0
>   3  0 0 1 0  0  0  0  0  0  0
>   4  0 0 0 1  0  0  0  0  0  0
>   5  0 0 0 0  1  0  0  0  0  0
>   6  0 0 0 0  0  1  0  0  0  0
>   7  0 0 0 0  0  0  1  0  0  0
>   8  0 0 0 0  0  0  0  1  0  0
>   9  0 0 0 0  0  0  0  0  1  0
>   10 0 0 0 0  0  0  0  0  0  1
> 
> Possible?  Without 200 lines of code?
> 
> ***********************************************************************
> *********************************************
> 
> This message may contain confidential information. If
> yo...{{dropped:21}}
> 
> ______________________________________________
> R-help at r-project.org 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.




More information about the R-help mailing list