0

Here's the issue:

I am using R to run some statistical analysis. The results of which will eventually be sent to a an embedded swf on the user's client machine.

To do this, I have PHP execute a shell script to run the R program, and I want to retrieve the results of that program so I can parse them in PHP and respond with the appropriate data.

So, it's simply:

$output = shell_exec("R CMD BATCH /home/bitnami/r_script.R");
echo $output;  

But, I receive nothing of course, because R CMD BATCH writes to a file. I've tried redirecting the output in a manner similar to this question which changes my script to

$output = shell_exec('R CMD BATCH /home/bitnami/raschPL.R /dev/tty');
echo $output; 

But what I get on the console is a huge spillout of the source code, and nothing is echoed.

I've also tried this question's solution in my R script.

tl;dr; I need to retrieve the results of an R script in PHP.

Cheers!

Community
  • 1
  • 1
PandemoniumSyndicate
  • 2,855
  • 6
  • 29
  • 49
  • Try the `passthru()` command instead. Let me know the output. – ionFish Jun 27 '12 at 15:38
  • It output 0 when I used passthru – PandemoniumSyndicate Jun 27 '12 at 15:43
  • Hm. Then using your current command, maybe put a `./home` or `./dev` but also make sure PHP has permissions to actually run those commands? – ionFish Jun 27 '12 at 15:44
  • I don't think the issue is on PHP, I think it's on R. R prefers to output to a file, but that's not kosher since I can't have my users overwriting eachothers output. So I'm trying to find a way to force R to output it's results to the command line – PandemoniumSyndicate Jun 27 '12 at 15:46
  • Make the file use a random name: in the PHP command, append `.rand(1000,9999).` to the file that R wants to save to. Be sure to save it as a PHP variable too so you can read it and/or unlink when finished. – ionFish Jun 27 '12 at 15:51
  • Hm, would that acceptable performance wise? If I have 300 users running an R script every 10 seconds or so, would writing a new file each time, reading the file, and removing it be terribly slow? – PandemoniumSyndicate Jun 27 '12 at 15:54
  • Maybe just delete all of the files at midnight via a `cron job`. – ionFish Jun 27 '12 at 16:40

3 Answers3

0

If it writes to file perhaps you could use file_get_contents to read it?

http://php.net/manual/en/function.file-get-contents.php

Gabriel Baker
  • 1,209
  • 11
  • 21
  • My concern is what happens when I have multiple users all running this command. I could write a new file for each user, retrieve the data, and remove the file, but that seems dangerous and silly. But it's still on the table so far. – PandemoniumSyndicate Jun 27 '12 at 15:51
0

Found it, the answer is through Rscript. Rscript should be included in the latest install of R.

Using my code as an example, I would enter this at the very top of r_script.R

#!/usr/bin/Rscript --options-you-need

This should be the path to your Rscript executable. This can be found easily by typing

which Rscript

in the terminal. Where I have --options-you-need, place the options you would normally have when doing the CMD BATCH, such as --slave to remove extraneous output.

You should now be able to run your script like so:

./r_script.R arg1 arg2

Important! If you get the error

Error in `contrasts<-`(`*tmp*`, value = "contr.treatment") : 
could not find function "is" 

You need to include the "methods" package, like so:

require("methods"); 
PandemoniumSyndicate
  • 2,855
  • 6
  • 29
  • 49
0

Perhaps,a much simpler workaround, would be:

    $output = shell_exec('R CMD BATCH /home/bitnami/raschPL.R > /dev/tty 2>&1');
    echo $output; 

Redirects both STDOUT and STDERR, since R outputs to STDERR, by default.

useR
  • 23
  • 5