Reputation: 87
I have a problem about how to pass php array to Rscript. In my project, I need to use php to call Rscript to run R code. But I want to pass php array as parameters in Rscript.But When I do this. php report error:Array to string conversion!. I don't know to do it . If it can successfully pass the parameter.I still don't know how to read the array paramter in R.Any ideas? Thanks in advance! Here is a example:
#php file
<?php
$ck=["WT1","WT2"];
$tr=["Al1","Al2"];
exec("Rscript getdata.R $ck $tr");
# getdata.R
args <- commandArgs(TRUE)
ck <- args[1]
tr <- args[2]
ckk <- lapply(strsplit(ck,','),as.character)
trr <- lapply(strsplit(tr,','),as.character)
a <- as.dataframe(a=ckk,b=trr)
write.csv(a,file="test.csv")
Upvotes: 1
Views: 176
Reputation: 8572
This seems to be a problem with converting $tr
and $ck
to strings. Lacking experience in php i base my experience on other languages, and a few other questions.
When executing from the terminal/cmd the arguments should all be strings. Following the example from an answer here you could likely use something similar to (untested!)
$ck=["WT1","WT2"];
$tr=["Al1","Al2"];
exec('Rscript getdata.R "' . implode(",", $ck) . '" "' . implode(",", $tr) . '"');
Note that this will then return the input as a string within R, which will then have to be converted back to vectors.
Upvotes: 1