g-language

Rcmd

Summary Included libraries Package variables Synopsis Description General documentation Methods

Summary
Rcmd - Perl interface for R language
Package variables top
Globals (from use vars definitions)
@EXPORT
$VERSION
@EXPORT_OK
Included modulestop
strict
Inherit top
AutoLoader Exporter
Synopsistop
  use Rcmd;

$rcmd = new Rcmd;

@result = $rcmd->exec("<R commands>","<next R command>");
Descriptiontop
  Note: We recommend using R/S Perl (http://www.omegahat.org/RSPerl/) instead
of this module in UNIX environment. Follow instructions of the above web site.

Rcmd enables Perl manipulation of the R language by simply executing them
through $rcmd->exec() function. Input is an array of R commands.

ex:

print $rcmd->exec(
"x_5",
"y_4",
"z_x*y",
"z"
);

Returned values are always an array. Therefore, in case the returned value
is only one, the value is accessible as:

@val = $rcmd->exec("y");

print $val[0];

All the values are saved in each session. Thus,

$val1 = $rcmd->exec( "x_5" , "x" );
$val2 = $rcmd->exec( "x");

will output "5" for both $val1 and $val2.

Obviously, it is also possible to use perl variables, as:

$i = 3;

print $rcmd->exec("y_y*$i","x");

The strength of R graphing abilities can be accessed as:

@array = $rcmd->exec(
"postscript(\"/tmp/out.ps\")",
"x_c(1:10)",
"y_c(3,6,3,5,8,0,1,9,2,6)",
"plot(x,y)",
"z_lsfit(x,y)",
"abline(z)",
"y"
);

system("gs /tmp/out.ps");

#R language is S-plus clone availabe with GPL at http://www.r-project.org/
Methodstop
DESTROYNo descriptionCode
execNo descriptionCode
newNo descriptionCode

Methods description


Methods code

DESTROYdescriptiontopprevnext
sub DESTROY {
    my $this = shift;
    unlink $this->{cmd};
    unlink $this->{log};
}
execdescriptiontopprevnext
sub exec {
    my $this = shift;
    my @tmp = @_;
    my $request = join("\n", @_, '');
    my $data = '';

    open(CMD, '>>' . $this->{cmd});
    print CMD $request;
    close(CMD);

    system("/usr/bin/env R --no-save --slave < "
	   . $this->{cmd} . " > " . $this->{log});

    open(DATA, $this->{log});
    while(<DATA>){
	if (/\[(\d+)\] (.*)/){
	    if ($1 > 1){
		$data .= ' ' . $2;
	    }else{
		$data = $2;
	    }
	}elsif(/Error/){
	    print STDERR $_;
	    die("Error in R, exiting...\n");
	}elsif(/Warning/){
	    print STDERR $_;
	    while(<DATA>){
		print STDERR $_;
		last;
	    }
	    warn("Warning in R...\n");
	}
    }
    close(DATA);

    if (wantarray()){
	return split(/\s+/,$data);
    }else{
	return $data;
    }
}
newdescriptiontopprevnext
sub new {
    my $this = shift;
    my $uniq = rand(9999999999);
    my $cmd = "/tmp/$uniq.cmd";
    my $log = "/tmp/$uniq.log";

    bless { 
	cmd => $cmd, 
	log => $log
    }
}

General documentation

AUTHOR top
Kazuharu Gaou Arakawa, gaou@g-language.org
SEE ALSO top
perl(1).