5

I have a bunch of R scripts that do some calculations and return a result. I am planning of building a PHP website that the user can actually submit a form where the data gets passed to my R script, processed and then return the result to the PHP and update the interface.

The plan is to have a database so when a user submits a form, the data gets stored in the database so R can read, process the input and then insert the result in the database so PHP can grab it. However, there are 2 problems:

  1. How do my R script knows that certain values have been stored in the database so it can grab those values and do the processing?
  2. When my R script finishes processing the data and insert it to mysql db, how do I get PHP to understand that at this moment PHP needs to query the database and grab the value?

Let's say my R script is like the following:

range<-1:20
m<-mean(range)
s<-sum(range)
print(m)
print(s)

As you can see the input at this case would be 1 and 20 to define the range, and the output is to show the values of m and s on my webpage.

Any idea how to accomplish that? thanks!

Ray
  • 781
  • 2
  • 17
  • 42

2 Answers2

4

shell_exec() or exec() are likely your best choices in PHP. This answer explains the difference.

echo shell_exec("Rscript my_script.R {$_GET['range']}");
alttag
  • 1,163
  • 2
  • 11
  • 29
  • So I don't need to use mysql database here? – Ray Aug 14 '17 at 18:30
  • If you need the inputs to persist across page loads, then store inputs in a database. If there is a huge amount of data in each call, and you're using the database as a way to persist the data between languages, then great (but this wasn't clear in the question). If the data is just pass-through and brief, there is no reason to involve a database. – alttag Aug 15 '17 at 19:33
  • Thank you for your reply.. this is helpful – Ray Aug 16 '17 at 14:52
  • 1
    Except that you always use `escapeshellarg()` to escape user input: `echo shell_exec("Rscript my_script.R " . escapeshellarg($_GET["range"]));` – miken32 Sep 15 '17 at 18:45
1

I'm no r expert, but it's been done : / poorman.php

echo "
";
echo "Number values to generate: 
";
echo "Submit";
echo ""
;

if(isset($_GET['N']))
{
  $N = $_GET['N'];

  // execute R script from shell
  // this will save a plot at temp.png to the filesystem
  exec("Rscript my_rscript.R $N");

  // return image tag
  $nocache = rand();
  echo("");
}
?>

and the R script…

my_rscript.R

args <- commandArgs(TRUE)

N <- args[1]
x <- rnorm(N,0,1)

png(filename="temp.png", width=500, height=500)
hist(x, col="lightblue")
dev.off()

source

catbadger
  • 1,662
  • 2
  • 18
  • 27
  • Basically, you're telling me that I don't need a database at all? – Ray Aug 14 '17 at 18:29
  • You can execute r scripts directly, yes. – catbadger Aug 14 '17 at 18:32
  • In the source, they mentioned that Only one user can be using this web app at a time.. I understand that because the R script will be busy processing the input.. how can my script be handle concurrent requests? – Ray Aug 14 '17 at 18:41
  • 1
    I am not sure of this solution, but maybe each time we generate an R script and once it's done we delete it? – Ray Aug 14 '17 at 18:44
  • 1
    @Riad re concurrency: May I suggest trying it, seeing whether there's a problem, and then opening a different question if there is? (Include your code!) You may find this isn't an issue at all. – alttag Aug 15 '17 at 19:34