开发者

Using R with Apache & PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

开发者_如何学JAVA

Closed 8 years ago.

Improve this question

Is there anyway to run R scripts through Apache and PHP? I would like to be able to pass variables from PHP to 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.


See the R-FAQ:

Two projects use PHP to provide a web interface to R. R_PHP_Online by Steve Chen (though it is unclear if this project is still active) is somewhat similar to the above Rcgi and Rweb. R-php is 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.

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


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

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

Then we get PhP to do an 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.

Not elegant at all, but it works for us.


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


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.


Here is a PHP library that might help:

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.


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


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

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 mediawiki on your server as well. Also, watch out for security.


A very Simple way

You can pass variables by using argument

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

for example

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

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

Hope its helpful~

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜