doushi3202 2012-06-27 15:36
浏览 62
已采纳

在PHP中检索R输出

Here's the issue:

I am using R to run some statistical analysis. The results of which will eventually be sent to a an embedded swf on the user's client machine.

To do this, I have PHP execute a shell script to run the R program, and I want to retrieve the results of that program so I can parse them in PHP and respond with the appropriate data.

So, it's simply:

$output = shell_exec("R CMD BATCH /home/bitnami/r_script.R");
echo $output;  

But, I receive nothing of course, because R CMD BATCH writes to a file. I've tried redirecting the output in a manner similar to this question which changes my script to

$output = shell_exec('R CMD BATCH /home/bitnami/raschPL.R /dev/tty');
echo $output; 

But what I get on the console is a huge spillout of the source code, and nothing is echoed.

I've also tried this question's solution in my R script.

tl;dr; I need to retrieve the results of an R script in PHP.

Cheers!

  • 写回答

3条回答 默认 最新

  • douyijin7741 2012-06-27 21:41
    关注

    Found it, the answer is through Rscript. Rscript should be included in the latest install of R.

    Using my code as an example, I would enter this at the very top of r_script.R

    #!/usr/bin/Rscript --options-you-need
    

    This should be the path to your Rscript executable. This can be found easily by typing

    which Rscript
    

    in the terminal. Where I have --options-you-need, place the options you would normally have when doing the CMD BATCH, such as --slave to remove extraneous output.

    You should now be able to run your script like so:

    ./r_script.R arg1 arg2
    

    Important! If you get the error

    Error in `contrasts<-`(`*tmp*`, value = "contr.treatment") : 
    could not find function "is" 
    

    You need to include the "methods" package, like so:

    require("methods"); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数