我正在使用下面的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];我想从php代码中将值7、2和1传递给Rscript
exec("D:\\R-2.14.0\\bin\\Rscript.exe D:\\wamp\\www\\R\\test.R 7 2 1");那么我该如何做this.Please帮助呢
发布于 2011-11-14 23:41:54
要在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发布于 2011-11-14 23:43:12
您已经做到了,只是您可能希望执行as.numeric(args1)等操作来将字符" 7“转换为数字7。
发布于 2012-01-17 06:16:19
尝试以下代码:
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 argumenthttps://stackoverflow.com/questions/8120981
复制相似问题