Skip to content
Prev 80474 / 398502 Next

writing R shell scripts?

On Wed, 9 Nov 2005, Henrik Bengtsson wrote:

            
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?
I guess you are saying that sometimes R will fail if I don't use print(). 
Can you give an example of how it can fail?
It looks like I can rewrite the script like this:

#!/bin/sh
echo "$1 ; write.table(file=stdout(), out, row.names=FALSE, col.names=FALSE); quit()" | /usr/local/bin/R --slave --no-save

Then I have to always include something like this...

out <- some_operation

...as part of my command.  Example:

# doR 'A <- matrix(rnorm(100*5),c(100,5)); out <- chol(cov(A))'
1.08824564637869 0.00749462665482204 -0.109577665309141 0.123824503621501 0.0420504647142321
0 0.969304154505745 0.0689085053799411 0.143273894584171 -0.0204348333174425
0 0 0.995383836907855 0.0860782051613422 0.056980680914183
0 0 0 0.94180592438191 0.0534651651371964
0 0 0 0 0.907266109886987

Now we've got it!!

The output above is nice and compact, and it doesn't have an extra 
newline, but if you want it to look nice on the screen, my friend Stephen 
Montgomery-Smith (Math, U Missouri) made me this nice little perl script 
for aligning numbers neatly and easily (but it doesn't work if there are 
letters in there (e.g., NA or 1.3e-6):

http://taxa.epi.umn.edu/misc/numalign

# ./doR 'A <- matrix(rnorm(100*5),c(100,5)); out <- chol(cov(A))' | numalign
0.903339952680364 -0.088773840144205 -0.223677935069773  -0.0736286093726908  0.0457396703130186
0                  1.08096548052082   0.0800540640587432 -0.0457840266135511 -0.0311210293661459
0                  0                  0.93834307353671    0.0665017259723313 -0.0825698771035788
0                  0                  0                   1.03303581434252    0.118372967026342
0                  0                  0                   0                   0.972768611955302

Thanks very much for all of the help!!!

Mike