[R-sig-Debian] littler usage

Dirk Eddelbuettel edd at debian.org
Sat Nov 24 15:47:30 CET 2007

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 ?  

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.

More information about the R-SIG-Debian mailing list