Integration between R and php

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

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

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

var_dump($response);

And an 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 $ response variable does not display the json generated by R, it returns an empty set to me. Is there any other way to integrate R and PHP.

+3
source share
3 answers

There are several options, but one of them is using RApache. Install RApache as directed at http://rapache.net/manual.html

Set the Apache directive in httpd.conf, which will check all the files in the / var / www / brew directory as R scripts

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

R script API plot.R /var/www/ brew. R script , , :

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

GET

API PHP, -, http://localhost/brew/plot.R?typeOfData=1&month=2&year=2014. localhost IP- , API.

RApache , GET, POST, COOKIES, FILES, SERVER, API. , POST GET, . . http://rapache.net/manual.html .

, : API, R?

+5

Rserve-php. Rserve backend, TCP/IP R.

+1

Look at the php-r library in github, it allows you to execute R-code with PHP (with an R-interpreter installed on your computer).

0
source

All Articles