[R] R via Browser

Chunlou Yung chunlou at yahoo.com
Thu Jun 19 00:21:24 CEST 2003


It seems all R-to-CGI libraries (all two of them, which I'm aware) run only
on Unix/Linux  (but I use Windows) and create temporary files to pass R
commands.

So, I wrote a short Apache-based Perl CGI script to execute R commands on
browser without needing temporary files--just for fun, babyish, nothing
robust, slow but works. (The rationale? I prefer distributing results via
the intranet on browser, rather than sending huge spreadsheets around, for
which the sysadmin bitterly complained causing the email server to grow
unduly large.)

Here it is, in case anyone interested (more like a "sample answer" than a
product, for sure):

#-----------------------------File Name:
simpleR.pl---------------------------
#! /usr/local/bin/perl -w
use Apache::Request () ;
use strict ;
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# You only need to modify this:
my $Rpath = "C:\\R\\rw\\bin\\" ;		# path to rterm.exe
# The rest will hopefully run itself.
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# to execute R cmd: R($Rpath, $R_cmd)
sub R {
	my $Rpath = shift ;
	my $Rcmd = $Rpath . "rterm --vanilla --quiet --slave" ;
	my $Rscript = shift ;
	$Rscript =~ s/(\r|;\r)/ ;/gm ; $Rscript =~ s/<-/=/gm ;	# \r or <- break
"echo"
	return `echo $Rscript | $Rcmd` ;
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
my $r = shift ;					# Apache stuff
my $q = Apache::Request->new($r) ;		# Apache Query obj
my $command = $q->param('command') ;
my $result = $command ? R($Rpath, $command) : "You didn't input any
command." ;
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
print "Content-type: text/html\n\n";
print <<"EOF";
<html><body>
	<form method="get" action="./simpleR.pl">
		Please enter your R command:<br>
		<textarea rows="4" name="command" cols="60">$command</textarea><br>
		<input type="submit" value="Submit">
	</form><br>
	<textarea rows="10" name="result" cols="80">$result</textarea>
</body></html>
EOF
exit;




More information about the R-help mailing list