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.
Whatever user apache is running as may not have the same environment variables, or permissions as when you ran it from the shell by hand. Find out what user apache runs as, and make sure it has the right permissions and environment variables.
For something this simple, you shouldn't even use PHP, 'cause you can do it solely with RApache,
brew
package and plain HTML (CSS/JS are optional). Just read the RApache manual and see Jeroen Ooms' video tutorial. You should also readbrew
manual. Jeff really did a great job!In a nutshell, you just need to setup apache2.conf or .htaccess file. Instead of using
<Directory>
or<Location>
directives, you can use<FilesMatch>
directive so you can match files with regexpr. Here's a sample .htaccess:I use special file extension
.rhtml
where I mix HTML with R syntax within<%
and%>
tags (seebrew
documentation). I didn't usebrew
with PHP, 'causebrew
with HTML did the trick.Anyway, you said that you want to:
system
orfile.*
commands and mess your system up! You can use AppArmor, but it's still a long way down...XML
R package, orrjson
(my favourite, and I recommend it). Then you can pass R object totoJSON
function and unserialize it with JavaScript. Of course, that's true only if your clients are web-browsers, otherwise, if you, say, want to pass some info to embedded system, you should use the standardXML
-wayFor any further questions, use RApache mailing list.
...and hello R-webdev world! =)
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.