[R] R script From PHP

Romain Francois romain.francois at dbmail.com
Wed Dec 2 11:10:09 CET 2009


On 12/02/2009 10:43 AM, Amrit Nandan wrote:
> I tried calling R from a shell script and it works. This is an example
> file rscript.sh
>
> R --no-save << EOT
> x<-2*pi
> print(cbind(x,sin(x)))
> quit("no")
> EOT
>
> This works perfectly on the Linux machine in which I have logged in via
> SSH using the command ("./rscript.sh")
>
> I tried running this command using php
>
> <html>
> <?php
>
> $output = array();
> $$retval = 0;
> $a = shell_exec('./rscript.sh'." 2>&1");
> //print phpinfo();
> print $a;
>
> ?>
> </html>
>
> this does not work and gives me this error:
>
> ./rscript.sh: line 1: R: command not found

Maybe shell_exec('sh ./rscript.sh 2>&1' ) instead, maybe you need to 
have the full path of R.

With this design, you are going to start a new R session each time you 
want anything to be computed, and you are going to have to format the 
result of the R code as a string and parse this string back in php.



You really should look into Rserve and its new php client, which I think 
will solve all of these problems at once.

Just download the version 0.6-1 from here : 
http://www.rforge.net/Rserve/files/

$ wget http://www.rforge.net/src/contrib/Rserve_0.6-1.tar.gz
# R CMD INSTALL Rserve_0.6-1.tar.gz

start the server

$ R CMD Rserve

grab the php client, and include it into your php application

$ tar zxvf Rserve_0.6-1.tar.gz
$ less Rserve/src/client/php/simple.php

If you try this route, be aware that the php client is very new. also 
you probably want to post questions regarding it on this mailing list :
http://mailman.rz.uni-augsburg.de/mailman/listinfo/stats-rosuda-devel

Romain


> Anybody familiar with this?
>
> On Tue, Dec 1, 2009 at 5:35 PM, Romain Francois
> <romain.francois at dbmail.com <mailto:romain.francois at dbmail.com>> wrote:
>
>     On 12/01/2009 12:48 PM, Hrishi Mittal wrote:
>
>         Hi Amrit,
>
>         I believe you can you use the PHP system command to call Rscript
>         or R CMD. I
>         don't know much PHP but this page might be useful -
>         http://php.net/manual/en/function.system.php.
>
>         You might also want to look at rapache -
>         http://biostat.mc.vanderbilt.edu/rapache/
>
>         Good luck with your project and do post a link to your webpage
>         when you get
>         it running.
>
>
>     There is also a simple php client to Rserve in the next version of
>     Rserve. http://www.rforge.net/Rserve/index.html

-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/FtUu : new package : highlight
|- http://tr.im/EAD5 : LondonR slides
`- http://tr.im/BcPw : celebrating R commit #50000




More information about the R-help mailing list