Re: [R] Linux command

From: Pierre Kleiber <pkleiber_at_hawaii.rr.com>
Date: Wed 21 Dec 2005 - 14:16:29 EST

In LINUX and other unixes you can also put your R commands in a so-called "here file", which resides within the script file that calls R; so you don't need two separate files. Taking Marc Schwartz' example, you could have make the following script file (call it say "tsst"):

#!/bin/sh
R --slave --vanilla <<XXXX
  # 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))
XXXX Then do:
chmod +x tsst
./tsst

The here file is the part between <<XXXX and XXXX. Also, arguments to the script are interpretable within the here file. So for example:

#!/bin/sh
R --slave --vanilla <<YYY
  tt <- scan("$1")
  print(mean(tt))
YYY would read data from a file given as an argument in calling the script.

Cheers, Pierre

Marc Schwartz offered the following remark on 12/19/05 14:43...
> 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
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
-- 
-----------------------------------------------------------------
Pierre Kleiber, Ph.D       Email: pkleiber@honlab.nmfs.hawaii.edu
Fishery Biologist            Tel: 808 983-5399 / (hm)808 737-7544
NOAA Fisheries Service - Honolulu Laboratory    Fax: 808 983-2902
2570 Dole St., Honolulu, HI 96822-2396
-----------------------------------------------------------------
  "God could have told Moses about galaxies and mitochondria and
   all.  But behold... It was good enough for government work."

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Wed Dec 21 14:28:14 2005

This archive was generated by hypermail 2.1.8 : Wed 21 Dec 2005 - 17:28:25 EST