Re: [R] writing R shell scripts?

From: Deepak Chandra <>
Date: Thu 25 Jan 2007 - 19:22:30 GMT

> > Hi All,
> >
> > Another newbie question. I want to write an R script that takes
> > argument from command line and runs and produces output to stdin.
> You will find that difficult: did you mean stdout?

My mistake -- I meant stdout :).

> > For example if there is file foo.R with following in it.
> >
> > args = commandArgs()
> > print(args)
> >
> > then, when I run it like
> > $ R foo.R hello
> > it should print 'hello' on the terminal.
> >
> > I searched the mainling list and found a very old post that said said

> Please give an exact reference so we know what you are referring to.
> This is not what is usually meant by 'R shell scripts' (it is an R script,
> not a shell script).
> > that this feature will be implemented soon. I am expecting this has
> > already been implemented by now.
> That is not what commandArgs() is documented to do so you will need to do
> something slightly different. But in the development version of R you can
> come very close:
> gannet% cat foo.R
> args <- commandArgs(TRUE)
> print(args)
> gannet% ~/R/R-devel/bin/Rscript foo.R hello
> [1] "hello"
> gannet%
> and in any recent version of R
> gannet% cat foo2.R
> args <- commandArgs()
> m <- match("--args", args)
> print(args[-(1:m)])
> gannet% R --slave --args hello < foo2.R
> [1] "hello"
> If you have a platform and build of R for which it works, there is also
> littler (,
> but beware that are restrictions not stated on that page (such as the need
> for R built as a shared library, which is not the default).

My apologies for being sketchy in my example: I was hoping to elucidate my problem without too many details. I was looking #! capabilites for running my scripts with different arguments from my shell.
Your response was helpful, and one of the three alternatives that you pointed to should solve my problem.

Deepak mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Fri Jan 26 06:26:32 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 25 Jan 2007 - 20:30:38 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.