[R] calling R from PHP and saving outputs

Laurent Faisnel laurent.faisnel at ariase.com
Tue Apr 29 16:34:05 CEST 2003


Hi all,
I have to call R from PHP code. I manage to run R and to make it parse 
my script (see below) but I have an error which I cannot cope with yet 
while running the PHP page:

Error in file("/home/faisnel/Rscripts/testphp2.Rout", open = "wt") : 
unable to open connection In addition: Warning message: cannot open file 
`/home/faisnel/Rscripts/testphp2.Rout' Execution halted

I am not sure about the "open" option I used (see R script). Does anyone 
have an idea about why the connection/file cannot be opened ? I checked 
I had the rights on the directory.
I give you my PHP main file and my R script below. Thanks in advance for 
any help.

Laurent
_
PHP :_

<?php

class Essai
{
  var $RPath; // path to R executable
  var $sortie; // display

  function Essai()
    {
      $this->RPath = "/usr/lib/R/bin/R";
      $this->sortie = $this->calcul();
    }
 
  function calcul()
    {
            $cmd = <<<EOD
echo 'source("commande.R")' | $this->RPath --slave
EOD;
        echo "\n";
        echo $cmd."\n";
        $result = shell_exec($cmd);
  
        echo($result);
        return ($result);
    }
}

$essai = new Essai();
?>

<table border='2' cellpadding='5'>
<tr>
<th align='right'> ESSAI : </th>
<td> ... </td>
<td>
<?php echo($essai->sortie);
?>
</td>
</tr>
</table>

_R script :_ (commande.R)

zz <- file("/home/faisnel/Rscripts/testphp2.Rout", open="wt");
sink(zz);
library(RMySQL);
con2 <- dbConnect("MySQL");
result <- dbGetQuery(con2,"select URL from isp_info");
dbListTables(con2);
result;
dbDisconnect(con2);
sink();



More information about the R-help mailing list