在 Apache 和 PHP 中使用 R

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2046881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 18:37:18  来源:igfitidea点击:

Using R with Apache & PHP

phpapacher

提问by Brandon Bertelsen

Is there anyway to run R scripts through Apache and PHP? I would like to be able to pass variables from PHP to R.

无论如何通过Apache和PHP运行R脚本?我希望能够将变量从 PHP 传递到 R。

Has anyone done something like this?

有没有人做过这样的事情?

Essentially, I would like to know if there is a method for passing variables from PHP to R on an apache server.

本质上,我想知道是否有一种方法可以在 apache 服务器上将变量从 PHP 传递到 R。

采纳答案by dangerstat

Personally we used the R CMD BATCH interface to R to achieve this. It's not very elegant, but essentially we have a user pasting data into a form which is passed to PhP which generates a .R file with something

我们个人使用 R CMD BATCH 接口到 R 来实现这一点。这不是很优雅,但本质上我们有一个用户将数据粘贴到一个表单中,该表单传递给 PhP,后者生成一个带有某些内容的 .R 文件

x = c(1,2,3,4); dev.off(); plot(x);

x = c(1,2,3,4); dev.off(); 情节(x);

Then we get PhP to do an exec "R CMD BATCH "

然后我们让 PhP 做一个 exec "R CMD BATCH "

In the directory you should then get Rplots.ps file which can be served to the client. Equally you can use the jpeg() function to return a jpeg picture of the graphic which can then be displayed on the web page PhP returns.

在该目录中,您应该获得可以提供给客户端的 Rplots.ps 文件。同样,您可以使用 jpeg() 函数返回图形的 jpeg 图片,然后可以在 PhP 返回的网页上显示该图片。

Not elegant at all, but it works for us.

一点也不优雅,但它对我们有用。

回答by rcs

See the R-FAQ:

请参阅R-FAQ

Two projects use PHP to provide a web interface to R. R_PHP_Onlineby Steve Chen (though it is unclear if this project is still active) is somewhat similar to the above Rcgi and Rweb. R-phpis actively developed by Alfredo Pontillo and Angelo Mineo and provides both a web interface to R and a set of pre-specified analyses that need no R code input.

有两个项目使用 PHP 为 R 提供 Web 接口。Steve Chen 的R_PHP_Online(虽然不清楚这个项目是否仍然活跃)有点类似于上面的 Rcgi 和 Rweb。R-php是由 Alfredo Pontillo 和 Angelo Mineo 积极开发的,它提供了一个 R 的 Web 界面和一组不需要 R 代码输入的预先指定的分析。

and this paper: Using R via PHP for Teaching Purposes: R-php

和这篇论文:通过 PHP 使用 R 用于教学目的:R-php

回答by kmace

Rserve now has support for php you can find more info at: http://code.google.com/p/rserve-php/

Rserve 现在支持 php,您可以在以下位置找到更多信息:http: //code.google.com/p/rserve-php/

回答by Brandon Bertelsen

Answers from Elsewhere:

其他地方的回答:

  1. RApache seems to be relevant: http://biostat.mc.vanderbilt.edu/rapache/. It integrates R with web serving in the form of an Apache module. It's available as a VMWare image, so you can test it smoothly. Jeroen Ooms developed some amazing webapps based on RApache: http://www.stat.ucla.edu/~jeroen/

  2. Another solution for integrationg PHP, Apache and R might be using one of the R web service solutions like statconnWS or rsoap. This has the advantage of being able to later simply separate the web server and the computer running R. The computational overhead should not be too large.

  1. RApache 似乎是相关的:http://biostat.mc.vanderbilt.edu/rapache/ 。它以 Apache 模块的形式将 R 与 Web 服务集成在一起。它可作为 VMWare 映像使用,因此您可以顺利进行测试。Jeroen Ooms 基于 RApache 开发了一些惊人的 web 应用程序:http: //www.stat.ucla.edu/~jeroen/

  2. 另一个集成 PHP、Apache 和 R 的解决方案可能是使用 R 网络服务解决方案之一,如 statconnWS 或 rsoap。这样做的好处是以后可以简单地将web服务器和运行R的计算机分开。计算开销应该不会太大。

回答by Alexander Kachkaev

Here is a PHP library that might help:

这是一个可能有帮助的 PHP 库:

https://github.com/kachkaev/php-r

https://github.com/kachkaev/php-r

use Kachkaev\PHPR\RCore;
use Kachkaev\PHPR\Engine\CommandLineREngine;

$r = new RCore(new CommandLineREngine('/usr/bin/R'));
$result = $r->run('1 + 1');
echo $result;

This will output:

这将输出:

> 1 + 1
[1] 2

The library is also available as Symfony2 bundle.

该库也可以作为Symfony2 bundle 使用

回答by Alex Brown

Yes, R in mediawikiachieves this quite nicely, and it's open source so you can see how they did it.

是的,mediawiki 中的 R很好地实现了这一点,而且它是开源的,因此您可以看到他们是如何做到的。

I have played extensively with the source myself, it's really simple and shouldn't take you long to adapt it.

我自己已经广泛地使用了源,它真的很简单,不应该花很长时间来适应它。

Of course you can't use it as-is unless you plan to use mediawikion your server as well. Also, watch out for security.

当然,除非您也打算mediawiki在服务器上使用,否则您不能按原样使用它。另外,注意安全。

回答by Mark

Another option is Rserve. It allows you to call R from any language that support TCP sockets.

另一种选择是Rserve。它允许您从任何支持 TCP 套接字的语言中调用 R。

回答by BoCai

A very Simple way

一个非常简单的方法

You can pass variables by using argument

您可以使用参数传递变量

every time exec("Rscript yourRscript.R") you pass arguments in that

每次 exec("Rscript yourRscript.R") 你传递参数

for example

例如

exec("Rscript yourRscript.R arg1 arg2 arg3")

exec("Rscript yourRscript.R arg1 arg2 arg3")

then yourRscript can load these agrus by using args <- commandArgs()

然后 yourRscript 可以使用 args <- commandArgs() 加载这些 agrus

Hope its helpful~

希望有帮助~