php - 如何在 Windows 上集成 PHP 和 R?

标签 php r windows-8

在集成 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

我在运行:

  • window 服务器 8 R2
  • IIS 8
  • PHP 5.5
  • R 3.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

关于php - 如何在 Windows 上集成 PHP 和 R?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23718375/

相关文章:

windows-8 - 试图获得 Windows 8 开发人员许可证但出现错误 0x80004005

node.js - 当我更改观看的文件时,fs.watch 触发了两次

php - Twitter api 1.1 oauth 在搜索主题标签时失败

php - 重新打开 : PHP array_shift(); VS reset(); unset(); array_splice();

r - 在起点和终点之间过滤

r - 是否有可能以 TeX 形式输出符号表达式

javascript - 使用 php、javascript 或 jquery 禁用 html 页面上的所有超链接 10 秒

php - 在 php 中处理数学方程式

带有 as.factor() : any way to specify the argument as a variable content instead of directly by name? 的 R 公式

Jquery 仅被调用一次(win 8 javascript 应用程序)