码迷,mamicode.com
首页 > 其他好文 > 详细

R中的参数传递函数:commandArgs(),getopt().

时间:2017-11-09 20:53:45      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:[1]   bug   debug   erb   cal   一个   complex   test   filename   

1.commandArgs(),是R自带的参数传递函数,属于位置参数。

##test.R
args=commandArgs(T)
print (args[1])##第一个外部参数
print (args[2])##第二个外部参数
##运行脚本:Rscript test.R first second
结果:

  技术分享

2.getopt(),是getopt包的函数,需要先按照getopt包

getopt(spec = NULL, opt = commandArgs(TRUE),command = get_Rscript_filename(), usage = FALSE,debug = FALSE)

spec:一个4-5列的矩阵,里面包括了参数信息,前四列是必须的,第五列可选。

第一列:参数的longname,多个字符。

第二列:参数的shortname,一个字符。

第三列:参数是必须的,还是可选的,数字:0代表不接参数 ;1代表必须有参数;2代表参数可选。

第四列:参数的类型。logical;integer;double;complex;character;numeric

第五列:注释信息,可选。

library(getopt)
spec = matrix(c( ‘verbose‘, ‘v‘, 2, "integer", ‘help‘ , ‘h‘, 0, "logical", ‘count‘ , ‘c‘, 1, "integer", ‘mean‘ , ‘m‘, 1, "double", ), byrow=TRUE, ncol=4) opt = getopt(spec) print(opt$count) print(opt$mean)

  技术分享

 

R中的参数传递函数:commandArgs(),getopt().

标签:[1]   bug   debug   erb   cal   一个   complex   test   filename   

原文地址:http://www.cnblogs.com/timeisbiggestboss/p/7811009.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!