【问题标题】:How to integrate PHP and R on Windows?如何在 Windows 上集成 PHP 和 R?
【发布时间】:2014-05-18 03:49:13
【问题描述】:

在集成 PHP 和 R 时遇到一些问题。我正在研究这篇文章:

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

R 已安装并通过我们的 R 脚本验证:

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

打印作为其计算结果的单个值:

[1] "0"

(Rscript.exe的路径在Windows环境变量中设置)

我有一个使用 exec() 的 PHP 脚本,该脚本使用以下命令成功测试:

$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);

返回:

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

当我尝试在 exec 命令中运行 R 脚本时,它失败了:

$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);

返回:

result 
output 
Array
(
)


return 1

我正在跑步:

  • Windows Server 8 R2
  • IIS 8
  • PHP 5.5
  • R 3.1

【问题讨论】:

  • 您是否尝试过输入 Rscript.exe 的完整路径?
  • 顺便说一句,我验证了 IIS 有权读取和执行 Rscript.exe 和 R 文件。
  • @dmullings 是的,结果相同: 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);
  • 尝试使用 system() php.net/manual/en/function.system.php 而不是 exec(),它应该会显示实际输出,希望您可以查看是否显示任何错误
  • @dmullings 尝试了 system() 和 passthru() 都无济于事。 exec 中的 $output var 与 system 中的返回值不一样吗?顺便说一句,回复和帮助:)

标签: php r windows-8


【解决方案1】:

无法让 exec() 工作或输出可用的错误,我决定寻找替代路线。使用 COM 类似乎给了我想要的东西。

这是最终的可操作代码:

$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>";

奇怪的是我无法让 exec() 完成这项工作,因为这似乎是大多数讨论 R/PHP 集成的博主首选的解决方案。

无论如何,我希望这个解决方案可以帮助其他遇到我这种情况的人!

附:您需要确保 php.ini 中的扩展已打开(安装时默认关闭):extension=php_com_dotnet.dll

【讨论】:

  • 什么是 WScript.Shell ?
猜你喜欢
  • 2014-11-09
  • 1970-01-01
  • 1970-01-01
  • 2015-09-29
  • 2013-06-17
  • 1970-01-01
  • 2021-12-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多