5
$\begingroup$

I would like build a web application using R. I am using Windows Vista and have an Apache server. I have tried Rpad, but I was not able to correctly configure it. How do I set up Rpad as I am not that well off with PHP and Apache server? Or are there other ways to use R on Apache server?

$\endgroup$
2
10
$\begingroup$

Here is the easiest way to do it that I found:

This implementation of PHP and R consists of only two files. One written in PHP, and the other an R script. The PHP returns a form which uses the GET method to send a variable N to the server. When the form is submitted, the PHP will then execute an R script from the shell using a combination of the PHP command exec() and the Rscript shell command. This command will pass the variable N to the R script. The R script will then execute and save a histogram plot of N normally distributed values to the filesystem. Finally, when the R script is complete, the PHP will return the HTML tag containing the saved images path. First, the PHP file

< ?php
// poorman.php     
echo "< form action='poorman.php' method='get'>";    
echo "Number values to generate: < input type='text' name='N' />";    
echo "< input type='submit' />";    
echo "< /form>";     
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("< img src='temp.png?$nocache' /> ");    
}    
?>

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

Here are some more you are welcome to try:

  1. http://danpolant.com/r-integration-with-php/

  2. http://steve-chen.net/document/r/r_php

$\endgroup$
2
  • $\begingroup$ +1, I can confirm that this works and it is very simple. I used it in my pretty complicated project: jQuery+PHP+R. $\endgroup$
    – mpiktas
    Feb 4 '11 at 6:53
  • 1
    $\begingroup$ Above answer copy and pasted from here: r-bloggers.com/integrating-php-and-r $\endgroup$
    – b_dubb
    Jun 16 '15 at 21:46
8
$\begingroup$

If you ever think to switch to Linux, the best way would be to use RApache, which is an Apache module that embeds an R interpreter (mod_R) in the webserver

$\endgroup$
3
$\begingroup$

If you are looking for a way of executing chunks of R code from PHP, here is a library that might help:

https://github.com/kachkaev/php-r

use Kachkaev\PHPR\RCore;
use Kachkaev\PHPR\Engine\CommandLineREngine;

$r = new RCore(new CommandLineREngine('/usr/bin/R'));
$result = $r->run('1 + 1');
echo $result;

This will output:

> 1 + 1
[1] 2
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.