Re: [R] commandArgs usage and --args invokation

2007-04-01 Thread Prof Brian Ripley
On Sat, 31 Mar 2007, ivo welch wrote:

 Dear R experts:

 I am a bit stymied by how the argument picking-off works in R batch file 
 usage.

You seem to mean 'R CMD BATCH' usage (there are other ways to run R in 
batch), and also I will assume you are talking only about a Unix-alike 
(these things are somewhat different on Windows).  Please do follow the R 
posting guide and provide basic information (and read the help).

In R 2.5.0 you probably want to use 'Rscript' instead for this sort of 
batch usage.

 $ cat commandArgs.R
 cat( Command Line Arguments were , commandArgs(), \n);

We don't want the trailing ';': this is R not C.

 $ /usr/bin/R CMD BATCH commandArgs.R --args 1 2 3

 $ /usr/bin/R --args 1 CMD BATCH commandArgs.R
 ... I am now getting into interactive mode ?!  I guess it really is 
 skipping the rest of the command line

Yes, that is what --args is documented to do, skip the rest of the command 
line.  Try R --help

 $ /usr/bin/R CMD BATCH commandArgs.R --args 1

 $ ls -lt | head
 total 1628148
 -rw-rw-r-- 1 ivo ivo   827 Mar 31 21:47 --args
 ...

Please DO read the help here:

gannet% R CMD BATCH --help
Usage: R CMD BATCH [options] infile [outfile]

so you have 'options' in the wrong place, and --args is matching 
'outfile'.  As is says that only arguments starting with '-' are 
considered to be options, you cannot use '1' here as an option.  What you 
can do is

R CMD BATCH '--args 1' commandArgs.R

Once again, this is doing what it is documented to do.


 Could someone please let me know?

 [also, are there multi-core versions of R?  my guess is no, but since
 I have a second processor lying around, I am trying to get it into the
 fray.]

 A working example would be appreciated.

R is not multithreaded, but you can make use of multithreaded BLASes: see 
the R-admin installation manual.

-- 
Brian D. Ripley,  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] commandArgs usage and --args invokation

2007-04-01 Thread Dirk Eddelbuettel

Ivo,

For R 'shell scripts', esp. on Unixy system (incl OS X), our littler frontend
can be of help. See

http://dirk.eddelbuettel.com/code/littler.html

or

http://biostat.mc.vanderbilt.edu/LittleR

as well as the SVN archive

svn checkout http://littler.googlecode.com/svn/trunk/ littler

Using littler scripts, you can then do things like

[EMAIL PROTECTED]:~/src/debian/Misc/svn/littler head -20 examples/pace.r
#!/usr/bin/env r
#
# a simple example to convert miles and times into a pace
# where the convention is that we write e.g. 37 min 15 secs
# as 37.15 -- so a call 'pace.r 4.5 37.15' yields a pace of
# 8.1667, ie 8 mins 16.67 secs per mile

if (is.null(argv) | length(argv)!=2) {

  cat(Usage: pace.r miles time\n)
  q()

}

dig - 5

rundist - as.numeric(argv[1])
runtime - as.numeric(argv[2])

[...]

which, while not as direct as getopt in bash or C, allows you to pick
arguments off the command-line. E.g.

[EMAIL PROTECTED]:~ pace.r 3 25
Miles   :  3
Time:  25
Pace:  8 min 20 sec
[EMAIL PROTECTED]:~


I use littler as a command-line frontend to most of my (batch) R work for
simulation or recurring data analysis.

As Prof Ripley noted. R 2.5.0 will give you something similar via Rscript.

Hth, Dirk




-- 
Hell, there are no rules here - we're trying to accomplish something. 
  -- Thomas A. Edison

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] commandArgs usage and --args invokation

2007-04-01 Thread ivo welch
hanks, dirk.  this looks a bit nicer than what I put together in the
last hour, which is a simple perl script that gives me the syntax I
most like:
  $ R batch.R arg1 arg2
personally, because this has no meaning in the current invoke
syntax, I think that R should understand this as the obvious
shortcut for R CMD BATCH '--args arg1 arg2' batch.R.

upon completion, if there is an error, it also prints the last 20
lines of the output, thereby alerting me real well that something went
wrong.  in case someone else finds it useful, I am dropping it in here
(it's short enough not to be too much of a bother, I hope).

the remaining problem that I have not yet tackled is that if I want a
few invokations of the same script, each with different args, the .Rout
file name should reflect the args...this is not hard, I think


#!/usr/bin/perl -w
use strict;

### Q: command line interface to R that allows the following
###simplified syntax
###
###  $ Q [any R options] batch.R  arg1 arg2 arg3 ...
###  in R itself, use commandArgs() to pick them off
###
### unless the --restore option or the --save option is given,
### we wipe out the .RData file, and invoke with --no-restore
### and --no-save.  I don't like accidentally contaminated
### R data spaces.
###
### if invoked with a CMD or without a .R file, then it just
### invokes the original /usr/bin/R with the same commandline.
###
### Finally, upon exit, if R exits with an error code, then it
### prints the last 20 lines of the .Rout file.  If I knew how
### to reliably ring a bell in linux, I would do this, too.
###
###
### in my .Rprofile, I have
###
### ARGV= vector(mode=character, 0);
### if (!interactive()) {
###  all.args=commandArgs();
###  nowargs=F;
###  for (i in 1:length(all.args)) {
###if (nowargs) ARGV= c(ARGV, all.args[i]);
###if (all.args[i] == --args) { nowargs=T;}
###  }
### }
###
### so I can now use the ARGV vector.  Note: this script has not been debugged
### or used for very long.


if ($#ARGV0) {
 (-e .RData) and unlink(.RData);  # by default, forget the environment
 exit(system(/usr/bin/R --no-restore-data --no-save));
}

my $originalline= join( , @ARGV);

($originalline !~ /[a-zA-Z0-9\_]\.R\b/) and exit(system(/usr/bin/R
$originalline));  ## there is no .R file anywhere
($originalline =~ /\bCMD\b/) and exit(system(/usr/bin/R
$originalline));  ## the user gives his own CMD file

### ok, pick off all arguments after the .R batch file

my $Rbatch; my $Rpgmopts=;
while (1) {
 $Rbatch = pop(@ARGV);
 ($Rbatch =~ /\.R$/) and last;
 $Rpgmopts= $Rbatch $Rpgmopts;
}

## ok, we have the .R batch file in $Rbatch, and the options in $pgmopts;
(-e $Rbatch) or die File $Rbatch does not exist\n;

my $RLNGOPTS= ($#ARGV=0) ? join( , @ARGV) : ;

 ## wipe out .RData;  I don't use it;
my $nowipeout=0;
if ($RLNGOPTS =~ /--restore/) { $nowipeout=1; } else { $RLNGOPTS .= 
--no-restore; }
if ($RLNGOPTS =~ /--save/) { $nowipeout=1; } else { $RLNGOPTS .=  --no-save; }
if (!$nowipeout) { (-e .RData) and unlink(.RData); }

if ($Rpgmopts ne ) { $Rpgmopts='--args $Rpgmopts'; }

print STDERR /usr/bin/R $RLNGOPTS CMD BATCH $Rpgmopts $Rbatch...;
my $rc=system(/usr/bin/R $RLNGOPTS CMD BATCH $Rpgmopts $Rbatch\n);

if (!$nowipeout) { (-e .RData) and unlink(.RData); }


## now check the output;

my $Rbatchlog= ${Rbatch}out;

if ($rc) {
 my $t=\n\n.`tail -20 $Rbatchlog`.;
 warn 

 /usr/bin/R $originalline: ERROR CODE $rc\n$t

;
} else { print STDERR ok\n; }

exit $rc;

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] commandArgs usage and --args invokation

2007-03-31 Thread ivo welch
Dear R experts:

I am a bit stymied by how the argument picking-off works in R batch file usage.

$ cat commandArgs.R
cat( Command Line Arguments were , commandArgs(), \n);

$ /usr/bin/R CMD BATCH commandArgs.R --args 1 2 3

$ /usr/bin/R --args 1 CMD BATCH commandArgs.R
... I am now getting into interactive mode ?!  I guess it really is skipping the
rest of the command line

$ /usr/bin/R CMD BATCH commandArgs.R --args 1

$ ls -lt | head
total 1628148
-rw-rw-r-- 1 ivo ivo   827 Mar 31 21:47 --args
...


Could someone please let me know?

[also, are there multi-core versions of R?  my guess is no, but since
I have a second processor lying around, I am trying to get it into the
fray.]

A working example would be appreciated.

regards,

/ivo welch

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.