[R] Command line and R

(Ted Harding) Ted.Harding at nessie.mcc.ac.uk
Wed Nov 9 13:25:37 CET 2005


On 09-Nov-05 Roger Bivand wrote:
> On Wed, 9 Nov 2005, Angelo Secchi wrote:
>> Hi,
>> I wrote a small R script (delta.R) using commandArgs(). The script
>> works from the shell in usual way
>> 
>> R --no-save arg1 < delta2.R
>> 
>> Suppose arg1 is the output of another shell command (e.g. gawk,
>> sed ...). Is there a way to tell R to read arg1 from the
>> output of the previous command? Any other workaround?
> 
> Use shell variables, possibly also Sys.getenv() within R as well as or 
> instead of commandArgs().

If it's a fairly simple shell comand (and even if it isn't, though
it could get tricky for complicated ones) you can use the "backquote"
trick (called, in well-spoken circles, "command substitution"):

  R --no-save `shellcmd` < delta2.R

As in all shell command lines, wherever you have a command (including
arguments etc.) between backquotes, as exemplified by "`shellcmd`" above,
the output of the command (as sent to stdout) replaces "`shellcmd`" in
the command-line. This could be a lot of stuff (depending on what
"shellcmd" is), or just one value, or whatever.

Best wishes,
Ted.


--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 09-Nov-05                                       Time: 12:25:32
------------------------------ XFMail ------------------------------




More information about the R-help mailing list