Skip to content

Command line and R

6 messages · Angelo Secchi, Roger Bivand, (Ted Harding) +2 more

#
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?
Thanks
#
On Wed, 9 Nov 2005, Angelo Secchi wrote:

            
Use shell variables, possibly also Sys.getenv() within R as well as or 
instead of commandArgs().

  
    
#
On 09-Nov-05 Roger Bivand wrote:
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 ------------------------------
#
On Wed, 09 Nov 2005 12:25:37 -0000 (GMT)
(Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> wrote:

            
Thanks.
A.

  
    
#
Angelo Secchi wrote:
... and this behaviour is OS (or at least command shell specific) for 
anyone trying this on Windows and wondering why it doesn't work.
#
On Thu, 10 Nov 2005, paul sorenson wrote:

            
But it does work on Windows if you have a reasonable shell.  Cmd.exe is 
and (especially) command.com are not shells in the usually accepted sense.
Better to use Rterm than incur the additional overhead of R, though.