作者Schematic (小小宝的妈)
看板PHP
标题[请益] PHP call R的问题
时间Thu Jan 7 16:27:25 2016
手边的程式希望能让使用者在网页上输入参数,
然後利用R来绘出统计图表和做一些统计运算
再把结果传回到PHP来呈现
目前已经测试过:
1. PHP把参数传给R,并让R根据这个参数来绘图,再由PHP显示 <- ok
2. R 连接资料库,取出资料绘图 <- ok
PHP 连结资料库,取出资料做成表格 <- ok
但是PHP呼叫R,由R根据参数到资料库取资料绘图之後就没下文了
程式撷取如下
PHP部分
<?php
// student_enroll.php
echo "<form action='student_enroll.php' method='get'>";
echo "近<input type='text' value='3' name='N' />来的学生入学人数分析";
echo "<br><input type='submit' />";
echo "</form>";
if(isset($_GET['N']))
{
$N = $_GET['N'];
// execute R script from shell
// this will save a plot at temp.png to the filesystem
$str = '"C:\Program Files\R\R-3.2.3\bin\Rscript"' . ' .\student_enroll.R' .
" $N";
$data = shell_exec($str);
echo $data;
// return image tag
$nocache = rand();
echo("<img src='temp.png?$nocache' />");
}
?>
R的部分
library(RODBC)
library(MASS)
library(ggplot2)
library(tidyr)
#从PHP取得参数
args <- commandArgs(TRUE)
set.year <- strtoi(args[1])
print(set.year)
#取得现在是哪一年并转换成中国年
this.year <- strtoi(substring(Sys.time(), 0, 4)) - 1911
#设定资料库连结
print(5)
odbcChannel <- odbcDriverConnect(connection="Driver={SQL
Server};server=127.0.0.1\\servername;database=database_name;
trusted_connection=yes;uid=uid;pwd=pwd")
print(6)
query.result <- sqlFetch(odbcChannel, "table_name")
print(7)
print(query.result)
odbcClose(odbcChannel)
红字以下都没有顺利执行完
现在有质疑是否PHP等待R存取资料库的时间太久了
因为不知道怎麽让PHP显示错误讯息
只能用这种土法炼钢法得知程式执行到哪一行之後没有继续
盼望有人可以解惑阿,不然已经卡在这个步骤一个多星期了
感谢
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 140.113.159.193
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/PHP/M.1452155247.A.DBF.html
2F:→ Peruheru: 虽然跟你的错误无关XD 01/07 17:36
3F:→ Peruheru: 回到问题,你遇到的问题可能是伺服器逾时 01/07 17:37
4F:→ Peruheru: 查询超过30秒就会被当成执行失败,这数字好像可以调 01/07 17:38
5F:推 Peruheru: 根本之道还是得加速查询才行 01/07 17:40
6F:→ MOONRAKER: php叫R开始算 算好放在/tmp/res{pid}底下 01/07 20:58
7F:→ MOONRAKER: 隔30秒再用js叫另一只php去把它打开 01/07 20:59
8F:→ Schematic: 请问哪边可以调整30秒的等待时间呢?我想先试着调整这边 01/08 09:11
9F:推 xdraculax: set_time_limit 01/08 11:59
10F:→ Schematic: 感谢所有的回答,继续测试中 01/08 12:02
11F:→ Schematic: 已经更改过系统的等待时间,也试过设定执行时间 01/08 12:58
12F:→ Schematic: 结果都还是一样>"< 01/08 12:58