[R-sig-Debian] littler usage

Dirk Eddelbuettel edd at debian.org
Sat Nov 24 16:55:59 CET 2007


On 24 November 2007 at 08:47, Dirk Eddelbuettel wrote:
| 
| On 23 November 2007 at 10:48, Sebastian P. Luque wrote:
| | Thanks Dirk for feedback on this.
| | 
| | I'm also trying to take two arguments from stdin and do a simple
| | computation.  I'm looking for something like:
| | 
| | echo 1 10 | r -e "print(pretty(c(as.numeric(argv[1]), as.numeric(argv[2]))))"
| | 
| | which of course doesn't work.  Any tips?
| 
| You have a logical inconsitency here. The string 
| 
|   echo 1 10 | r -e "print(pretty(c(as.numeric(argv[1]), as.numeric(argv[2]))))"
| 
| go from your shell to the OS.  Who should get argv now -- echo or r ?  

Sorry, I think I am wrong here. It is not the 'compound command from issue'
but rather the fact that r is used in a pipe.  In that case you need stdin
processing as used below rather than argv processing.

Dirk
 
| The way to get what you want is to forget about argv and to read from stdin
| in the classic Unix filter tradition. 
| 
| The example 'fsizes.r' in the littler sources does just that using
| readLines().  As we write in the docs:
| 
|     $ ls -l /boot | awk '!/^total/ {print $5}' | \
|                              r -e 'fsizes <- as.integer(readLines());
|                                     print(summary(fsizes)); stem(fsizes)'
| 
| so you get to your example via
| 
|    $ echo 1 10 | r -e 'print(readLines())'    
| 
| which gets you a single input line "1 10" which you can break, eg via
| strsplit, and then turn to integer and consume:
| 
|    $ echo 1 10 | r -e 'print(sum(as.integer(strsplit(readLines(), " ")[[1]])))'
|    [1] 11
| 
| 
| 
| Cheers, Dirk
| 
| -- 
| Three out of two people have difficulties with fractions.
| 
| _______________________________________________
| R-SIG-Debian mailing list
| R-SIG-Debian at r-project.org
| https://stat.ethz.ch/mailman/listinfo/r-sig-debian

-- 
Three out of two people have difficulties with fractions.



More information about the R-SIG-Debian mailing list