[R] R scripts and parameters

Jeffrey J. Hallman jhallman at frb.gov
Fri Feb 20 16:09:44 CET 2009


Here's how I do this:

The last lines of my .First() function are these:

## Run command line program if there is one
if(length(.cmd <- commandArgs(trailingOnly = TRUE)) > 0)
try(source(textConnection(.cmd), echo = T, prompt.echo = "> "))

and on my Linux path I have this csh script, called "runR"

#! /bin/csh
# This is the runR script.
# It starts R in a specified directory (default /mra/prod/R)
# and submits the string given as its last argument to that R process.
# To submit multiple Rcommands, use a single string with the commands 
# separated by semi-colons.  
#
set usage = 'runR [-d directory] "cmd1; cmd2; cmd3"'
#
#  Example: runR "chartControl()"
#
#    starts R and immediately invokes the chartControl()
#    function, and exits when chartControl() does.

if ($#argv == 0 ) then
  echo $usage
  exit(1)
endif

set rDir = /mra/prod/R
if("$1" == "-d") then
  shift
  set directory = $1
  shift
else
  set directory = $rDir
endif

umask 002
cd $directory

set log = $rDir/tmp/runR.log
if( -e $log ) then
  set nLines = `wc -l $log | awk '{print $1}'`
  if($nLines > 25000) then
    tail -20000 $log > $log.tmp
    mv $log.tmp $log
  endif
endif

R --no-save --args "$argv ; q()" |& tee -a $log
# end of runR script


-- 
Jeff




More information about the R-help mailing list