[R] 如何在Linux命令行进行参数传入?

以前由于R命令行传参不友好,经常嵌套在其他程序语言(如Perl/Python)中来进行传参,但现在也陆续有一些方式来实现R的传参了,这里简单罗列下。

方法一

最传统的方法就是使用系统自带的commandArgs函数,直接按位置顺序传入。这种方法简短、快速,适合个人使用。一般也能满足我们的需求了,但对于其他用户是不够友好的。

#test.R
args=commandArgs(T)
file=read.table(args[1])
...
#command line
$Rscript test.R file

方法二

使用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)
# 构建参数矩阵
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)

以我的数据作为例子,部分脚本如下:

library(getopt)
command=matrix(c("exp","e",1,"character",
                 "ko","k",1,"character",
                 "cazy","z",1,"character",
                 "cog","c",1,"character",
                 "help","h",0,"logical"),byrow=T,ncol=4)
args=getopt(command)
#帮助信息
if (!is.null(args$help) || is.null(args$exp) || is.null(args$ko) || is.null(args$cazy)|| is.null(args$cog)) {
  cat(paste(getopt(command, usage = T), "\n"))
  q()
}

#读入参数
exp <- readr::read_delim(args$exp,delim = "\t")
ko <- readr::read_delim(args$ko,delim = "\t",comment = '#',col_names=F)
cazy <- readr::read_delim(args$cazy,delim = '\t')
cog <- readr::read_delim(args$cog,delim = '\t')
......

命令行运行:
帮助

$Rscript getopt_test.R -h
Usage: getopt_test.R [-[-exp|e] <character>] [-[-ko|k] <character>] [-[-cazy|z] <character>] [-[-cog|c] <character>] [-[-help|h]]

运行

$ Rscript getopt_test.R --exp protein.xls --ko test.ko --cazy cazy.anno --cog protein2cog.xls

方法三

使用GetoptLong包。这是由大佬Zuguang Gu开发(就是开发ComplexHeatmap
circlize的那位),借用了Perl GetoptLong模块的传参形式,用法也几乎一样。

GetoptLong(..., help = TRUE, version = TRUE, envir = parent.frame(), argv_str = NULL,
    head = NULL, foot = NULL, script_name = NULL)

可以看下他提供的例子:
https://github.com/jokergoo/GetoptLong
https://www.rdocumentation.org/packages/GetoptLong/versions/0.1.7/topics/GetoptLong

 #r script
library(GetoptLong)
    cutoff = 0.05 #default
    GetoptLong(
        "number=i", "Number of items, integer, mandatory option",
        "cutoff=f", "cutoff to filter results, optional, default (0.05)",
        "verbose",  "print messages"
    )  

#Then you can call the script from command line either by:
    $ Rscript foo.R --number 4 --cutoff 0.01 --verbose
    $Rscript foo.R -n 4 -c 0.01 -v
    $ Rscript foo.R -n 4 --verbose  

以我自己的数据为例。部分R脚本如下:

suppressMessages(library(GetoptLong))
suppressMessages(library(tidyverse))

GetoptLong(
  "expression=s", "protein expression matrix",
  "ko=s", "ko annotation outcome",
  "cazy=s", "cazy annotation outcome",
  "cog=s", "cog annotation outcome",
  "verbose!","print messages"
)

#读入参数
exp <- readr::read_delim(expression,delim = "\t")
ko <- readr::read_delim(ko,delim = "\t",comment = '#',col_names=F)
cazy <- readr::read_delim(cazy,delim = '\t')
cog <- readr::read_delim(cog,delim = '\t')

命令行运行会自动生成帮助文档。

$ Rscript test.R --help
Usage: Rscript function_summary.R [options]

  --expression character
    protein expression matrix

  --ko character
    ko annotation outcome

  --cazy character
    cazy annotation outcome

  --cog character
    cog annotation outcome

  --verbose
    print messages

  --help
    Print help message and exit.

  --version
    Print version information and exit.

长参传入:

$Rscript test.R --expression protein.xls --ko test.ko --cazy cazy.anno --cog protein2cog.xls

短参传入:
如果所有参数的首字母不同,用首字母即可;如果有些参数名称近似,则最好用多个字母,否则会辨别不了。
比如我这里的cogcazy参数,首字母相同,明显不能都用c,我把其中一个改成大写的C也辨别不了;其中一个用一个首字母,另一个用两个首字母也不行。用coca就可以了。所以参数的名字一定要明显区分开来。

$ Rscript test.R -e protein.xls -k test.ko -c cazy.anno -C protein2cog.xls
Option c is ambiguous (cazy, cog)
Option c is ambiguous (cazy, cog)
Usage: Rscript test.R [options]

  --expression character
    protein expression matrix

  --ko character
    ko annotation outcome

  --cazy character
    cazy annotation outcome

  --Cog character
    cog annotation outcome

  --verbose
    print messages

  --help
    Print help message and exit.

  --version
    Print version information and exit.

这个就可以了

$ Rscript test.R -e protein.xls -k test.ko -ca cazy.anno -co protein2cog.xls

Ref:
https://www.cnblogs.com/timeisbiggestboss/p/7811009.html
https://www.rdocumentation.org/packages/GetoptLong/versions/0.1.7/topics/GetoptLong

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,490评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,581评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,830评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,957评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,974评论 6 393
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,754评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,464评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,357评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,847评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,995评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,137评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,819评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,482评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,023评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,149评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,409评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,086评论 2 355

推荐阅读更多精彩内容