Debian Bug Tracking System wrote:
> #475343: Rscript searches for R binary in /tmp, rather than /usr/bin
>
> It has been closed by Dirk Eddelbuettel <[EMAIL PROTECTED]>.
Not quite sure why this was closed when the problem still seems to exist,
but anyway...
I've attached a perl script that should replicate the effects of the
wrapper C program that was written by The R Development Core Team.
The main change (apart from the porting) is line 72, where I have
excecuted the shell command 'which R' to determine the location of the R
excecutable. Thinking about dependencies, both the 'which' and 'perl'
commands are part of essential debian packages (debianutils, perl-base
respectively), so there shouldn't be any further dependencies required for
this script.
I've tried to test as many use cases as possible from looking at the code
(and experimenting with command-line options), but I can't promise that
there won't be a stray off-by-1 error or something like that.
David
#! /usr/bin/perl
# Rscript.pl: Copyright 2008 David Hall (gringer)
#
# A perl port of the original Rscript wrapper by The R Development Core Team
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, a copy is available at
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
## This is intended to be used in scripts like
##
## #! /path/to/Rscript --vanilla
## commandArgs(TRUE)
## q(status=7)
##
## This invokes R with a command line like
## R --slave --no-restore --vanilla --file=foo [script_args]
use strict;
use warnings;
my $PATH_MAX = 5000;
my $RSVERSION='44199-pl-1';
my $verbose = 0;
sub usage {
printf STDERR "Usage: /path/to/Rscript [--options] [-e expr] file [args]\n\n";
printf STDERR "--options accepted are\n";
printf STDERR " --help Print usage and exit\n";
printf STDERR " --version Print version and exit\n";
printf STDERR " --verbose Print information on progress\n";
printf STDERR " --default-packages=list\n";
printf STDERR " Where 'list' is a comma-separated set\n";
printf STDERR " of package names, or 'NULL'\n";
printf STDERR "or options to R, in addition to --slave --no-restore, such as\n";
printf STDERR " --save Do save workspace at the end of the session\n";
printf STDERR " --no-environ Don't read the site and user environment files\n";
printf STDERR " --no-site-file Don't read the site-wide Rprofile\n";
printf STDERR " --no-init-file Don't read the .Rprofile or ~/.Rprofile files\n";
printf STDERR " --restore Do restore previously saved objects at startup\n";
printf STDERR " --vanilla Combine --no-save, --no-restore, --no-site-file\n";
printf STDERR " --no-init-file and --no-environ\n";
}
my $buf = "";
my $buf2 = "";
my $i = 0;
my $i0 = 0;
my $ac = 0;
my $e_mode = 0;
my $set_dp = 0;
my @av = ();
if(@ARGV <= 0){
usage;
exit 1;
}
my $cmd = qx{which R};
chomp $cmd;
$av[$ac++] = $cmd;
$av[$ac++] = "--slave";
$av[$ac++] = "--no-restore";
if (@ARGV == 1){
if($ARGV[0] eq "--help"){
usage;
exit 0;
}
if($ARGV[0] eq "--version"){
printf STDERR "R scripting front-end version %s\n", $RSVERSION;
}
}
# first copy over any -e or --foo args
for ($i = 0; $i < (@ARGV - 1); $i++){
if($ARGV[$i] eq "-e"){
$e_mode = 1;
$av[$ac++] = $ARGV[$i];
if(!$ARGV[++$i]){
printf STDERR "-e not followed by an expression\n";
exit 1;
}
$av[$ac++] = $ARGV[$i];
$i0 = $i;
next;
}
if($ARGV[$i] eq "--"){
last;
}
if($ARGV[$i] eq "--verbose"){
$verbose = 1;
$i0 = $i;
next;
}
if($ARGV[$i] =~ /^--default-packages=(.*)$/){
$set_dp = 1;
if(length $ARGV[$i] > 1000){
printf STDERR "unable to set R_DEFAULT_PACKAGES\n";
exit 1;
}
$buf2 = sprintf "R_DEFAULT_PACKAGES=%s", $1;
if($verbose){
printf STDERR "setting '%s'\n", $buf2;
}
$ENV{'R_DEFAULT_PACKAGES'} = $buf2;
if($ENV{'R_DEFAULT_PACKAGES'} ne $buf2){
printf STDERR "unable to set R_DEFAULT_PACKAGES\n";
exit 1;
}
$i0 = $i;
next;
}
$av[$ac++] = $ARGV[$i];
$i0 = $i;
}
if(!$e_mode){
if(length $ARGV[++$i0] > $PATH_MAX){
printf STDERR "file name is too long\n";
exit 1;
}
$buf = sprintf '--file=%s', $ARGV[$i0];
$av[$ac++] = $buf;
}
$av[$ac++] = "--args";
for($i = $i0+1; $i < @ARGV; $i++){
$av[$ac++] = $ARGV[$i];
}
$av[$ac] = "";
if(!$set_dp && !$ENV{'R_DEFAULT_PACKAGES'}){
$ENV{'R_DEFAULT_PACKAGES'} = "datasets,utils,grDevices,graphics,stats";
}
if($verbose){
printf STDERR "running\n '%s", $cmd;
for($i = 1; $i < $ac-1; $i++){
printf STDERR " %s", $av[$i];
}
printf STDERR "'\n\n";
}
exec $cmd @av;
warn "Rscript execution error";