如何在Windows上集成PHP和R?
在集成PHP和R时遇到一些问题。我从本文开始工作: R已安装,并已通过验证,可与我们的R脚本一起使用:如何在Windows上集成PHP和R?,php,r,windows-8,Php,R,Windows 8,在集成PHP和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环境变量中设置) 我有一个PHP脚本,它使用exec()成功地测试了以下命令: $result =
Rscript C:\inetpub\wwwroot\client\includes\decisionTreePredictor.R 20 10 O 1000 10000 5000 0.2 10.2
打印作为其计算结果的单个值:
[1] "0"
(Rscript.exe的路径在Windows环境变量中设置)
我有一个PHP脚本,它使用exec()成功地测试了以下命令:
$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);
当我尝试在exec命令中运行R脚本时,它失败了:
result
output
Array
(
)
return 1
我正在跑步:
- Windows Server 8 R2
- IIS 8
- PHP5.5
- R 3.1
$command='C:\ProgramFiles\R\R-3.1.0\bin\Rscript.exe C:\inetpub\wwwroot\client\includes\decisiontreeprodictor.R 20 10 O 1000 5000 0.2 10.2';
$pCom=newcom(“WScript.Shell”);
$pShell=$pCom->Exec($command);
$sStdOut=$pShell->StdOut->ReadAll;#标准输出
$sStdErr=$pShell->StdErr->ReadAll;#错误
回显“$sStdOut”;
奇怪的是,我无法让exec()来完成这项工作,因为这似乎是大多数讨论R/PHP集成的博客作者首选的解决方案
不管怎么说,我希望这个解决方案能帮助其他任何陷入我处境的人
另外,您需要确保扩展名在php.ini中打开(安装时默认为关闭):extension=php_com_dotnet.dll您是否尝试输入Rscript.exe的完整路径?顺便说一句,我验证了IIS有权读取和执行Rscript.exe和R文件。@dmullings是的,结果相同:exec('C:\Program Files\R\R-3.1.0\bin\Rscript.exe C:\inetpub\wwwroot\client\includes\decisiontreeprodictor.R 20 10 O 1000 10000 5000 0.2 10.2',$output,$returnVar);请尝试使用system(),而不是exec(),它应该显示实际的输出,希望您能看到是否存在任何错误displayed@dmullings已尝试使用system()和passthru()两者都无效。exec中的$output变量是否与系统中的返回值相同?顺便说一句,用于响应和帮助:)什么是WScript.Shell?
$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
$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>";