[R] writing R shell scripts?

Mike Miller mbmiller at taxa.epi.umn.edu
Wed Nov 9 17:06:57 CET 2005


On Wed, 9 Nov 2005, Henrik Bengtsson wrote:

> Put everything in curly brackets as my above example show.

Ah ha! This leads to the latest version of my little one-liner (called 
"doR"):

#!/bin/sh
echo "output <- { $1 }; write.table(file=stdout(), .Last.value, row.names=FALSE, col.names=FALSE); q()" | /usr/local/bin/R --slave --no-save


The use of "output <-" suppresses the unwanted intermediate computations 
and the curly brackets make it so that ".Last.value" does what we want. 
So I can now do a series of semi-colon separated commands in my one-liner 
and pump out the result of only the last step.  Like so:

# doR 'dims <- c(100,5) ; A <- matrix(rnorm(100*5)*4,dims); chol(cov(A))'
3.94658490894107 0.317366981840069 1.06924803070620 0.340120807287771 -0.497063892955837
0 3.8326282049957 -0.675108240711913 -0.0199506782900556 -0.306406513059080
0 0 3.56097699699067 -0.52407121204737 0.107635052917745
0 0 0 3.88866072385884 -0.365208065096497
0 0 0 0 3.81017455630087

That isn't a great example, but you get the idea.  I use this kind of 
thing pretty often.

Mike

-- 
Michael B. Miller, Ph.D.
Assistant Professor
Division of Epidemiology and Community Health
and Institute of Human Genetics
University of Minnesota
http://taxa.epi.umn.edu/~mbmiller/




More information about the R-help mailing list