[R] writing R shell scripts?

Henrik Bengtsson hb at maths.lth.se
Wed Nov 9 10:01:23 CET 2005


Mike Miller wrote:

>On Wed, 9 Nov 2005, Henrik Bengtsson wrote:
>
>  
>
>>>>A note of concern: When writing batch scripts like this, be explicit 
>>>>and use the print() statement.  A counter example to compare
>>>>
>>>>echo "1; 2" | R --slave --no-save
>>>>
>>>>and
>>>>
>>>>echo "print(1); print(2)" | R --slave --no-save
>>>>
>>>>        
>>>>
>>>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?
>>>      
>>>
>
>This may have been a misunderstanding because it looks like my R and your 
>R are not functioning in the same ways.  More below...
>
>
>  
>
>>What I was really try to say is that if you running the R terminal, that 
>>is, you "sending" commands via the R prompt, R will take the *last* 
>>value and call print()  on it.  This is why you get the result when you 
>>type
>>
>>    
>>
>>>1+1
>>>      
>>>
>>[1] 2
>>
>>Without this "feature" you would have had to type
>>    
>>
>>>print(1+1)
>>>      
>>>
>>[1] 2
>>
>>to get any results.  Note that it is only the last value calculate, that will 
>>be output this way, cf.
>>    
>>
>>>1+1; 2+2
>>>      
>>>
>>[1] 4
>>    
>>
>
>
>My version of R works differently:
>
># echo "1+1; 2+2" | R --slave --no-save
>[1] 2
>[1] 4
>
>It does the same thing from the interactive prompt.  This holds in both of 
>these versions of R on Red Hat Linux:
>
>R 1.8.1 (2003-11-21).
>R 2.2.0 (2005-10-06).
>
>
>If I assign the results of earlier computations to variables, then they 
>are not printed:
>
># echo "x <- 1+1; 2+2" | R --slave --no-save
>[1] 4
>
>  
>
Hmm... You're completely correct.  I did not know this, that is, that 
print() seems to be called by the R terminal also after the completion 
of each expression and not just newlines.  Anyway, my overall message is 
that you should still be explicit about what you want to output in your 
code.  The following does definitely show what I want to stress:

foo <- function() {
 1+1;
 2+2;
}

 > foo()
[1] 4

and even

 > { 1+1; 2+2; }
[1] 4

>>Or go get the last value calculated by .Last.value, see
>>
>>echo "$1; out <- .Last.value; write.table(file=stdout(), out, row.names=FALSE, col.names=FALSE); quit()" | /usr/local/bin/R --slave --no-save
>>    
>>
>
>Beautiful.  I didn't know about .Last.value, but now that I do, I think we 
>can shorten that script to this...
>
>echo "$1; write.table(file=stdout(), .Last.value, row.names=FALSE, col.names=FALSE); quit()" | /usr/local/bin/R --slave --no-save
>
>...because we no longer need the "out" variable.  It seems like one 
>problem I'm having is that R returns results of every computation, and not 
>just the last one, unless I assign the result to a variable.  Example 
>using the doR one-line script above:
>
># doR 'chol(cov(matrix(rnorm(100*5),c(100,5))))'
>          [,1]       [,2]        [,3]          [,4]        [,5]
>[1,] 1.021414 0.09806281 0.003275454  0.0009819654  0.05031847
>[2,] 0.000000 1.10031274 0.002696835 -0.0990352880  0.17356877
>[3,] 0.000000 0.00000000 0.822075977 -0.0353553332 -0.04559222
>[4,] 0.000000 0.00000000 0.000000000  0.9367890692 -0.01513027
>[5,] 0.000000 0.00000000 0.000000000  0.0000000000  0.97588119
>1.02141394873274 0.0980628119885006 0.00327545419626209 0.000981965434760053 0.050318470112499
>0 1.10031274450895 0.00269683530006245 -0.0990352879929318 0.173568771318532
>0 0 0.82207597738982 -0.0353553332133034 -0.0455922206141078
>0 0 0 0.936789069194909 -0.0151302741201435
>0 0 0 0 0.975881188029811
>
># doR 'x <- chol(cov(matrix(rnorm(100*5),c(100,5))))'
>1.09005225946311 0.183719241993361 -0.211250918511775 -0.0148273333266647 -0.097633753471306
>0 0.990599902490968 0.0546812452445389 -0.0255188599622241 0.0502929718369168
>0 0 0.982263267444303 -0.0587151164554906 -0.046018923176493
>0 0 0 1.00433563628640 0.222340686806836
>0 0 0 0 0.976420329786668
>
>I suppose I can live with that.  
>
Put everything in curly brackets as my above example show.

>Is my R really working differently from 
>the R other people are using?
>
>  
>
No. Sorry about the confusion.

Cheers

Henrik

>  
>
>>You may also want to create your own method for returning/outputting 
>>data. An ideal way for doing this is to use create a so called generic 
>>function, say, returnOutput(), and then special functions for each class 
>>of object that you might get returned, e.g. returnOutput.matrix(), 
>>returnOutput.list(), etc. Don't forget returnOutput.default().  If you 
>>do not understand what I'm talking about here, please read up on 
>>S3/UseMethod in R documentation.  It's all in there.  Then you'll also 
>>get a much deeper understanding of how print() (and R) works.
>>    
>>
>
>Thanks yet again for another great tip!
>
>Mike
>
>  
>




More information about the R-help mailing list