r -在php中由exec函数提供参数


r - Supplying argument by exec function in php

我使用以下php代码运行R语言的脚本。

echo "<form action='rtest1.php' method='get'>";
echo "Number values to generate: <input type='text' name='N' />";
echo "<input type='submit' />";
echo "</form>";
if(isset($_GET['N']))
{
  $N = $_GET['N'];
  // execute R script from shell
  // this will save a plot at temp.png to the filesystem
  $result=array();
  $int='';
  exec("D:''R-2.14.0''bin''Rscript.exe D:''wamp''www''R''test.R 7 2 1");

}

下面是我的Rscript的示例代码

args <- commandArgs(TRUE);
    #assign data path
    data_path <- "D:''wamp''www''R";
    #assign valus to the following three percent
    train_per <- args[1];
    test_per <- args[2];
    val_per <- args[3];

我想把值7,2和1从php代码传递给Rscript

exec("D:''R-2.14.0''bin''Rscript.exe D:''wamp''www''R''test.R 7 2 1");

那么我该怎么做呢?请帮助

要在R脚本中获得不同的参数值,您可以执行以下操作:

myarg <- commandArgs() #get all arguments from console
n <- myarg[length(myarg)] #number of arguments
tmp<-strsplit(n,",") # parsing of the arguments (here the convention to separate the arg is the comma , )
folderPath <- tmp[[1]][1]; # value of the 1st argument
paramPath <- tmp[[1]][2]; # value of the 2nd argument  
dataPath <- tmp[[1]][3]; # value of the 3rd argument

您已经完成了,除了您可能希望执行as.numeric(args[1])等操作以将字符"7"转换为数字7。

尝试以下代码:

myarg <- commandArgs()
tmp <- strsplit(myarg," ")
arg1 <- tmp[[1]][1]; # value of the 1st argument
arg2 <- tmp[[2]][1]; # value of the 2nd argument  
arg3 <- tmp[[3]][1]; # value of the 3rd argument