Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using R through PHP

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.

like image 788
crix Avatar asked Jan 27 '11 15:01

crix


People also ask

Can we use R in PHP?

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().


1 Answers

Couple of points:

  1. You want --slave as the option, it implies --no-save and turns the greeting off; see the manual for more.

  2. 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.

  3. If you insist on piping from php, consider the littler scripting frontend Jeff and I wrote. It will start faster than R.

like image 153
Dirk Eddelbuettel Avatar answered Nov 08 '22 12:11

Dirk Eddelbuettel