Skip to content
Prev 82987 / 398502 Next

Linux command

On Tue, 2005-12-20 at 02:14 +0200, Tommi Viitanen wrote:
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