# How can I integrate R with PHP? [closed]

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?

• This question appears to be off-topic because it is about programming and not about statistics. Mar 23 '14 at 19:11
• Did you try RCallerPhp? Here is a link github.com/jbytecode/rcaller/tree/master/RCallerPhp Sep 21 '16 at 8:16

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, I can confirm that this works and it is very simple. I used it in my pretty complicated project: jQuery+PHP+R. Feb 4 '11 at 6:53
• Above answer copy and pasted from here: r-bloggers.com/integrating-php-and-r Jun 16 '15 at 21:46

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

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