Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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.

like image 342
nilesh1006 Avatar asked Feb 22 '23 10:02

nilesh1006


2 Answers

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

like image 57
mana Avatar answered Feb 25 '23 12:02

mana


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.

like image 24
aL3xa Avatar answered Feb 25 '23 11:02

aL3xa