[R] non-interactive R usage...

David Brahm a215020 at agate.fmr.com
Mon Oct 8 16:39:33 CEST 2001


Chris Milkosky <c.milkosky at xpedite.com> wrote:
> Does R give you the capability to run a script (read as non-interactive) that
> would perform a statistical task, and then send output to a file?
> ...can we pass command line parameters to our R scripts somehow?

One way to pass arguments and flags is through a Perl wrapper.  This also
allows you to set up environment variables appropriately in a Unix cron job.
Here's my template.  I'm not sure about graphics, though.

###### Begin Code: ######
#!/usr/local/bin/perl -s
(($arg1, $arg2) = @ARGV) || die "Must supply 2 arguments";
$f += 0;			                       # -f flag, force numeric
$ENV{"R_LIBS"} = "$ENV{SetThisAppropriately}/Rlib";
$| = 1;				                     # Don't buffer Perl output
$script = "/tmp/rwrap$$.R";
$outfile = "/tmp/rwrap$$.out";
open(SCRIPT, ">$script");
print SCRIPT <<EOF;
  arg1  <- "$arg1"
  arg2  <- "$arg2"
  fflag <- $f
EOF
print SCRIPT <<'EOF';
Z<-require(misc, q=T)                                                # Quietly!
# YOUR CODE HERE
q()
EOF
close(SCRIPT);
system("R --vanilla --slave < $script > $outfile");
$? && die "Aborted in R with errcode $?";   # Check for successful R completion
unlink $script;                   # The script remains for debugging if failure
####### End Code #######

					-- David Brahm (brahm at alum.mit.edu)
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list