Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to integrate PHP and R on Windows?

Tags:

php

r

windows-8

Having some issues integrating PHP and R. I am working from this article:

http://www.r-bloggers.com/integrating-php-and-r/

R is installed and is verified working with our R script:

Rscript C:\inetpub\wwwroot\client\includes\decisionTreePredictor.R 20 10 O 1000 10000 5000 0.2 10.2

Printing a single value that is a result of its calculations:

[1] "0"

(The path to Rscript.exe is set in the Windows environmental variables)

I have a PHP script in place that is using exec() which tests successfully with commands such as:

$result = exec('dir',$output,$returnVar);
echo "<br>result ". print_r($result,true);
echo "<br>output <pre>". print_r($output,true) , "</pre>";
echo "<br>return ". print_r($returnVar,true);

returning:

result 2 Dir(s) 117,749,354,496 bytes free
output 
Array
(
    [0] =>  Volume in drive C is C_DRIVE
    [1] =>  Volume Serial Number is 7EB2-A074
    [2] => 
    [3] =>  Directory of C:\inetpub\wwwroot\client\temp
    [4] => 
    [5] => 05/17/2014  10:29 PM              .
    [6] => 05/17/2014  10:29 PM              ..
    [7] => 05/16/2014  09:24 AM             5,181 dbimporttest.php
    [8] => 05/17/2014  10:29 PM                 0 routput.txt
    [9] => 05/17/2014  11:42 PM               701 rscripttest.php
    [10] => 05/16/2014  04:59 PM               425 whoami.php
    [11] =>                4 File(s)          6,307 bytes
    [12] =>                2 Dir(s)  117,749,354,496 bytes free
)


return 0

When I try to run the R script within the exec command, it fails:

$result = exec('Rscript.exe C:\inetpub\wwwroot\client\includes\decisionTreePredictor.R 20 10 O 1000 10000 5000 0.2 10.2',$output,$returnVar);
echo "<br>result ". print_r($result,true);
echo "<br>output <pre>". print_r($output,true) , "</pre>";
echo "<br>return ". print_r($returnVar,true);

Returning:

result 
output 
Array
(
)


return 1

I am running:

  • Windows Server 8 R2
  • IIS 8
  • PHP 5.5
  • R 3.1
like image 433
mwex501 Avatar asked Nov 01 '22 23:11

mwex501


1 Answers

Unable to get exec() to work or output errors that are usable, I decided to seek an alternative route. Using the COM class seems to have given me what I was looking for.

Here is the final, operational code:

$command = 'C:\Program Files\R\R-3.1.0\bin\Rscript.exe C:\inetpub\wwwroot\client\includes\decisionTreePredictor.R 20 10 O 1000 10000 5000 0.2 10.2';
$pCom = new COM("WScript.Shell");
$pShell = $pCom->Exec($command);
$sStdOut = $pShell->StdOut->ReadAll;    # Standard output
$sStdErr = $pShell->StdErr->ReadAll;    # Error
echo "<pre>$sStdOut</pre>";

Odd that I couldn't get exec() to do the job as that seems be the solution preferred by most bloggers discussing R/PHP integration.

Anyway, I hope this solution helps anyone else that finds themselves in my situation!

P.S. You will want to make sure the extension is on in php.ini (it is off by default on install): extension=php_com_dotnet.dll

like image 129
mwex501 Avatar answered Nov 09 '22 09:11

mwex501