tags:

views:

403

answers:

5

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.

+4  A: 

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

rcs
This project provides full R capabilities, this isn't what I'm looking for. I'm looking for something that can facilitate handing variables from PHP to R and R output back to PHP on an Apache server.
Brandon Bertelsen
+2  A: 

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

Mark
Not entirely -- you also need a client that speaks the Rserve protocol. Simon has given us Java and C++ clients and AFAIK nothing exists for PHP. That said, Rserve is very useful and rather under-appreciated.
Dirk Eddelbuettel
That's part of the problem - I don't know Java. Otherwise, this would likely be my solution.
Brandon Bertelsen
+3  A: 

Hi.

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.

dangerstat
Thanks for the Answer dangerstat. This was not the best solution, but so chose it. In another forum, I found two options that were much more agreeable for my purposes.
Brandon Bertelsen
+2  A: 

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.

Alex Brown
This definitely looks interesting... I can't seem to get it to work on any of the servers that I have access to though :(
Brandon Bertelsen
+3  A: 

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.

Brandon Bertelsen
... and be sure that you have `brew` package install - then you can pass R code within `<%` and `%>` tags.
aL3xa