I'm relatively new to R and very new to the Linux (Ubuntu) command line. I'm trying to write a page in PHP that contains a command I would like to execute in R and then pass the results back to be able to work with them. I've tried variations of the code below, but I only get the R welcome message as my output:
<?php
$rQuery = "\"echo 3 + 1;\" | /usr/bin/R --no-save";
exec($rQuery, $output);
print_r($output);
?>
When I manually type $rQuery in the command line the result is what I would expect: [1] 4.
I know I must be missing something towards the end there, but I haven't been able to figure it out through my own searching.
There are times when you want to showcase the output of R program like charts that you create based on the user inputted data from a web page. In that case you might want your PHP based web application to communicate with the R script. When it comes to PHP, it has a very useful function called exec().
Couple of points:
You want --slave
as the option, it implies --no-save
and turns the greeting off; see the manual for more.
What you really want is Jeff Horner's excellent R-inside-Apache, see here for more. It can use templating frameworks like brew as well. Best of all, Jeff now provides a .deb package for you: use deb http://ppa.launchpad.net/jeffreyhorner/rapache/ubuntu lucid main
in /etc/apt/sources.list
.
If you insist on piping from php, consider the littler scripting frontend Jeff and I wrote. It will start faster than R.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With