6

I have a php script that sends 3 values ​​per parameter for a script R.

$typeOfData = 1;
$month = 2;
$year = 2014;

exec("Rscript C:/xampp/R-script/plot.R $typeOfData $month $year", $response);

var_dump($response);

And a R script that receives these parameters.

library(rjson)

args <- commandArgs(TRUE)
tmp <- strsplit(args, " ")
typeOfData <- tmp[[1]][1]
month <- tmp[[2]][1]
year <- tmp[[3]][1]

output <- list(imgname="imgs/tmax.tiff")
cat(toJSON(output))

When I run the php script the variable $ response does not display the json generated by R, it returns me an empty set. Is there any other way to integrate R and PHP.

perondi
  • 165
  • 1
  • 3
  • 8

3 Answers3

9

There are several options, but one option is to use RApache. Install RApache as indicated in http://rapache.net/manual.html

Set the Apache directive in httpd.conf which will make sure all files under /var/www/brew are parsed as R scripts

<Directory /var/www/brew>
    SetHandler r-script
    RHandler brew::brew
</Directory>

Make your R script with your API with file name plot.R and put it under the /var/www/brew folder. This R script file can e.g. look as follows:

<%
library(rjson)

args <- GET
tmp <- lapply(args, FUN=function(x) strsplit(x, " "))
typeOfData <- tmp[[1]][1]
month <- tmp[[2]][1]
year <- tmp[[3]][1]

output <- list(imgname="imgs/tmax.tiff")
cat(toJSON(output))
%>

Mark the GET

Now you can call your API from PHP as you would call any other webservice by calling http://localhost/brew/plot.R?typeOfData=1&month=2&year=2014. Replace localhost with the IP of the server where you are hosting the API.

When using RApache, each time you get GET, POST, COOKIES, FILES, SERVER variables which were passed on to the API call. So if you want to use POST in your call instead of the GET example, go ahead. See the documentation in http://rapache.net/manual.html for these variables.

This is almost the same answer as indicated here: What's the easiest way to deploy an API incorporating R functions?

Community
  • 1
  • 1
  • I have the same problem as in the Q. You mentioned several ways, can you elaborate on other ways ? I can open a new Q if you want me to. I have Xampp configured with PHP/MySQL/Apache. So, I prefer to `exec` R with this installation. My Q is similar to http://stackoverflow.com/questions/23718375/how-to-integrate-php-and-r-on-windows – user5249203 Apr 25 '16 at 19:51
  • and also similar to this Q. http://stackoverflow.com/questions/2046881/using-r-with-apache-php. Don't want to duplicate the Q, but there are no proper answers. If you can address, please let me know. – user5249203 Apr 25 '16 at 19:58
  • One way I can visualize is save the output (data frame) from R in a Text file and read it back in PHP variable as array. Is there a more elegant way, where the output from command line is called back and passed to a PHP variable ? I see some directions http://stackoverflow.com/questions/7941356/check-the-output-of-php-exec-always-returns-empty-array, but I am not a bash/ unix person. So, had hard time understanding. Thank you – user5249203 Apr 25 '16 at 20:01
1

Check out Rserve-php. It uses Rserve as a backend which is a TCP/IP server for R.

Christopher Louden
  • 7,540
  • 2
  • 26
  • 29
0

Have a look at php-r library on github, it lets you execute R code from PHP (having R interpreter installed on your machine).

Alexander Kachkaev
  • 842
  • 15
  • 29