首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过php中的exec函数提供参数

通过php中的exec函数提供参数
EN

Stack Overflow用户
提问于 2011-11-14 19:36:13
回答 3查看 1.1K关注 0票数 1

我正在使用下面的php代码来运行R语言的脚本。

代码语言:javascript
复制
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的示例代码

代码语言:javascript
复制
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

代码语言:javascript
复制
exec("D:\\R-2.14.0\\bin\\Rscript.exe D:\\wamp\\www\\R\\test.R 7 2 1");

那么我该如何做this.Please帮助呢

EN

回答 3

Stack Overflow用户

发布于 2011-11-14 23:41:54

要在R脚本中获取不同的参数值,您可以这样做:

代码语言:javascript
复制
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
票数 1
EN

Stack Overflow用户

发布于 2011-11-14 23:43:12

您已经做到了,只是您可能希望执行as.numeric(args1)等操作来将字符" 7“转换为数字7。

票数 1
EN

Stack Overflow用户

发布于 2012-01-17 06:16:19

尝试以下代码:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8120981

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档