在ubuntu下运行php [英] Run R in php under ubuntu

查看:183
本文介绍了在ubuntu下运行php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个R脚本运行得很好,当我从命令行运行它并将一个映像保存到文件系统。然而,当我从php脚本使用以下命令运行它:

  exec(cat my_rscript.R | / usr / bin / R --vanilla); 

它不运行。请注意,当我在命令行中运行它时,我正在使用完全相同的调用。我很确定,我在正确的目录(php文件和R脚本在同一个目录中)。可能是因为我不能从PHP运行R脚本?我认为这与事实,即运行R-64位和PHP只是32位,但我不知道这是否是问题。




  1. 我推荐以下步骤来调试这个, >获取的输出,其中R (确保它在路径中)(例如,其中R> where_is_r.txt

  2. 获取shell开始执行的位置,即调用R的路径(应该是where_is_r.txt文件的位置)

  3. 创建一个简单的helloworld.r(例如,将正态分布的随机值写入文本文件),并执行 Rscript --vanilla /path/to/helloworld.r
  4. 用脚本替换helloworld.r,并执行 Rscript --vanilla /path/to/my_script.R

如果这些不显示问题出在哪里,则可能需要保存输出错误(STDERR)和输出(STDOUT)。这可以在R内完成,但最好通过bash来完成。请参阅此页获取有关如何执行此重定向的指南。另外,通常最好使用完全指定的路径,而不是依赖于在特定目录中调用的shell。完整的规范可能是绝对路径或相对路径,但相对路径和符号链接比绝对路径调试困难一些。


I have an R script that runs just fine when I run it from the command line and saves an image to the file system. However when I run it from the php script using the following command :

exec("cat my_rscript.R | /usr/bin/R --vanilla");

It doesn't run. Note that when I'm running it in the command line I'm using the exact same call. I'm pretty sure that I'm in the right directory (the php file and the R script are in the same directory). What could be the cause me not being able to run the R script from php? I think this has to do with the fact that Im running R-64 bit and php is only 32 bit, however Im not sure if thats the problem.

解决方案

I'd recommend the following steps to debug this, all via the commands to the shell, from PHP:

  1. Get the output of which R (make sure that it's in the path) (e.g. which R > where_is_r.txt
  2. Get the location of where your shell begins execution, i.e. the path from which R is being called (that should be the location of the "where_is_r.txt" file)
  3. Create a simple helloworld.r (e.g. writes normally distributed random values to a text file), and execute Rscript --vanilla /path/to/helloworld.r
  4. Replace helloworld.r with your script, and execute Rscript --vanilla /path/to/my_script.R

If these don't reveal where the problem is, you may need to save the output of error (STDERR) and output (STDOUT). This can be done inside R, but it's better to do this via bash. See this page for a guide on how to do this redirection.

Also, in general it is best to use fully specified paths rather than depend on the shell being invoked in a particular directory. The full specification could be either an absolute or relative path, but relative paths and symlinks are a little harder to debug than absolute paths.

这篇关于在ubuntu下运行php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆