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.
Couple of points:
You want
--slaveas the option, it implies--no-saveand 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 mainin/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.