5

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
halfer
  • 19,824
  • 17
  • 99
  • 186
mwex501
  • 492
  • 1
  • 8
  • 23
  • 2
    have you tried putting in the full path to Rscript.exe ? – dmullings May 18 '14 at 03:52
  • BTW I verified that IIS has rights to read and execute Rscript.exe and the R file. – mwex501 May 18 '14 at 03:53
  • @dmullings Yes, same results: exec('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',$output,$returnVar); – mwex501 May 18 '14 at 03:54
  • Instead of exec(), try using system() http://php.net/manual/en/function.system.php which should display the actual output and hopefully you can see if any errors are displayed – dmullings May 18 '14 at 04:04
  • @dmullings tried system() and passthru() both to no avail. Isn't the $output var in exec the same as the returned value in system anyway? BTW ty for responding and helping :) – mwex501 May 18 '14 at 04:34
  • The return value of 1 is an indication of the process returning an error. I cant see how to catch error text with `exec`, so it might be going to a log file. Check your server's log files. In unix terms, you want to catch `stderr` output, maybe this helps: http://stackoverflow.com/questions/2320608/php-stderr-after-exec but check log files first. – Spacedman May 18 '14 at 09:42
  • @Spaceman. Yes, checked logs and I have settings to display errors at the beginning of the file (error_reporting(E_ALL); ini_set('display_errors',1);) - neither report anything. – mwex501 May 18 '14 at 18:19
  • It seems exec is behaving as if it cannot find (or doesn't have access to) Rscript.exe. I double checked that EVERYONE has full rights to Rscript.exe and the R file (and the model file that the R file calls upon). To further complicate issues, when sexec() fails it does not write anything to the error log (I tested by having exec on a junk command, same results and no error logged). So I think that's our first order of business - how to SEE the error that is occurring with exec()?? – mwex501 May 18 '14 at 18:51
  • Could be user/env variables, such as the home path, that are not set in a PHP CLI context. – halfer May 23 '14 at 20:21

1 Answers1

2

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

mwex501
  • 492
  • 1
  • 8
  • 23