Polwart Calum (County Durham and Darlington NHS Foundation Trust)
2009-Sep-14 21:10 UTC
[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}}
Hi, On Sep 14, 2009, at 5:10 PM, Polwart Calum (County Durham and Darlington NHS Foundation Trust) wrote:> 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:See ?cat -steve> > 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.-- Steve Lianoglou Graduate Student: Computational Systems Biology | Memorial Sloan-Kettering Cancer Center | Weill Medical College of Cornell University Contact Info: http://cbio.mskcc.org/~lianos/contact
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.