nilesh1006
nilesh1006

Reputation: 165

Integration of R Language with php to take the result from R

I have the following R script

 #assign data path
    data_path <- "C:\\Users\\Owner\\Desktop\\R\\work";

    #assign valus to the following three percent
    train_per <- 0.7;
    test_per <- 0.2;
    val_per <- 0.1;


    data <- read.csv(file=paste(data_path,"\\test.csv",sep=""), sep=",");
    train_d <- sort(sample(nrow(data), nrow(data)*train_per));


    train <- data[train_d,];
    rem_train <- data[-train_d,]; 

    test_d <- sort(sample(nrow(rem_train), nrow(data)*test_per));
    test <- rem_train[test_d,];
    validation <- rem_train[-test_d,]; 

    m <- glm(der_var4~.,data=train,family=binomial());

    coef_data <- data.frame(coef(m));
    coef_data[7:8,"column_name"] <- NA;
    coef_data$column_name <- row.names(coef_data);

    write.table(coef_data,file=paste(data_path,"\\coef_data.csv",sep=""),sep=",",row.names=FALSE);

    anova_data <- data.frame(anova(m));
    anova_data[7:8,"column_name"] <- NA;
    anova_data$column_name <- row.names(anova_data);

    write.table(anova_data,file=paste(data_path,"\\anova_data.csv",sep=""),sep=",",row.names=FALSE);

The above script is running fine in R.The above script will create two csv files. But i want to run that script by using my php code .My main goal is to take the values of the following variables from the php

train_per ;
test_per ;
val_per ;

and then i have to send the values of above to R script and then i have to run that script from my php code.Please help me i am new to R as well as php.I am trying to use exec function but not reaching anywhere and i have also seen older posts and also surf the net but could not find any solution.

Upvotes: 4

Views: 7716

Answers (3)

mana
mana

Reputation: 1239

In your PHP code, use the exec to pass your variable to R $response is the R response.

exec("Rscript /path/to/rcode.R  $a $b ", $response);
$str = $response[0];
$myobj = json_decode($str);

echo $myobj->first_name;

In your R code (my exmaple using rcode.R)

args <- commandArgs(TRUE)
tmp <- strsplit(args, " ")

a <- as.numeric(tmp[[1]][1])
b <- as.numeric(tmp[[2]][1])


output <- list(first_name="Finau")

library(rjson)
cat(toJSON(output))

//You need to use the RJson for R

Upvotes: 5

Spacedman
Spacedman

Reputation: 94202

You can run any external program from PHP with the 'exec' function. Read all the warnings on the manual page though. Read them and understand them.

The problem can be that R takes a while to start up, which makes your web page a bit slow. If that is a problem, then you need another solution (possibly running a private Rserve port will do).

I see you say you are trying to use the exec function. Well, how far have you got? What you can do is put the values of your PHP variables on the command line that you create and pass to exec, and then get those values using the R commandArgs() function.

Upvotes: 1

aL3xa
aL3xa

Reputation: 36080

Use JSON to pass the data back and forth. There's RJSONIO package that will help you the R part. I'd rather store your variables in a named list, and serialize them to JSON:

lst <- list(
            train_per = train_per,
            test_per = test_per,
            val_per = val_per
            )
res <- toJSON(lst)
cat(res)

this piece of code should return JSON encoded list. I'll assume that you're using RApache, since it has PHP-like superglobal called POST. Then, on the PHP side, use output buffering:

<?php
ob_start();
// call R script here
$resp = json_decode(ob_get_clean());
?>

You should test the result by simply echoing it out or by issuing print_r. If everything runs smooth, you will get variable values in an associative array elements.

EDIT:

You should head over to RApache homepage, and install it. Learn how to send form data to R scripts - Jeroen Ooms' video is a good starting point. Then you can send POST requests with PHP cURL - link on David Walsh's blog should be helpful. Of course, this piece of code should be inserted between ob_start() and ob_get_clean() to capture stuff that R script echoes.

I'm having a deja-vu as I'm writing this... something tells me that I've already wrote something similar here. I know that there were a lot of rants about R/PHP integration.

Upvotes: 2

Related Questions