writing R shell scripts?
Mike Miller wrote:
Many thanks for the suggestions. Here is a related question: When I do things like this... echo "matrix(rnorm(25*2),c(25,2))" | R --slave --no-save ...I get the desired result except that I would like to suppress the "[,1]" row and column labels so that only the values go to stdout. What is the trick to making that work?
What you ask R to do is matrix(rnorm(25*2),c(25,2)) which is equivalent to print(matrix(rnorm(25*2),c(25,2))) What you really want to do might be solved by write.table(), e.g. x <- matrix(rnorm(25*2),c(25,2)); write.table(file=stdout(), x, row.names=FALSE, col.names=FALSE); 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
By the way, I find it useful to have a script in my path that does this: #!/bin/sh echo "$1" | /usr/local/bin/R --slave --no-save Suppose that script was called "doR", then one could do things like this from the Linux/UNIX command line: # doR 'sqrt(35.6)' [1] 5.966574 # doR 'runif(1)' [1] 0.8881654 Which I find to be handy for quick arithmetic and even for much more sophisticated things. I'd like to get rid of the "[1]" though!
If you want to be lazy and not use, say, doR 'cat(runif(1),"\n")' above, maybe a simple Unix sed in your shell script can fix that?! /Henrik
Mike