[R] Linux command

Marc Schwartz MSchwartz at mn.rr.com
Tue Dec 20 01:43:28 CET 2005


On Tue, 2005-12-20 at 02:14 +0200, Tommi Viitanen wrote:
> I wonder if it's possible to run R-functions or other commands 
> automatically by some shell-script in Linux shell.
> 
> I thought that something like
> $ R mean(c(1,2))
> $ R xy.Rdata
> would work, but I havent found the right way.

There is some documentation in 'An Introduction to R', Appendix B
"Invoking R" which is available with the R installation and/or from the
main R web site under Documentation. There is also help available via
'man R' from the Linux console.

If you are just passing your first line to R, you can do something like
this:

$ echo "mean(c(1,2))" | R --slave --vanilla
[1] 1.5


This echos the R command string and pipes it as stdin to R.

The additional arguments make the interaction with R more streamlined
and are documented in the aforementioned references.

You can also pass a text file containing more complex R commands:

R --slave --vanilla < RCommandFile.txt


If I have the following in the file:

# Example from ?lm
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2,10,20, labels=c("Ctl","Trt"))
weight <- c(ctl, trt)
anova(lm.D9 <- lm(weight ~ group))


I can then do:

$ R --slave --vanilla < RCommandFile.txt
Analysis of Variance Table

Response: weight
          Df Sum Sq Mean Sq F value Pr(>F)
group      1 0.6882  0.6882  1.4191  0.249
Residuals 18 8.7293  0.4850


And you can of course re-direct the output:

R --slave --vanilla < RCommandFile.txt > Outfile.txt


HTH,

Marc Schwartz




More information about the R-help mailing list