以前由于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

[R] 如何在Linux命令行进行参数传入?的更多相关文章

  1. linux命令行传递参数定期执行PHP文件

    最近在做一个项目,需要在linux下传递参数定期执行PHP文件,网上查询资料,确实有相关资料,现整理如下: 1.linux执行PHP文件 #{PHP安装bin路径} {PHP文件路径} {需要参数1 ...

  2. 如何在Linux命令行中创建以及展示演示稿

    导读 你在准备一场演讲的时候,脑海可能会先被图文并茂.形象华丽的演示图稿所占据.诚然,没有人会否认一份生动形象的演讲稿所带来的积极作用.然而,并非所有的演讲都需要TED Talk的质量.更多时候,演讲 ...

  3. 如何在linux命令行无界面下使用selenium

    1.安装Xvfb和pyvirtualdisplay sudo apt-get install xvfbpip install pyvirtualdisplay 2.安装chrome, chromedr ...

  4. 在linux命令行下执行php 程序

    如何在linux命令行下,执行php程序. 例子 打印当前时间 php -r "echo time()" 随机输出一个数字 php -r "echo rand(1,20) ...

  5. 像黑客一样使用 Linux 命令行

    前言 之前在博客园看到一篇介绍 IntelliJ IDEA 配置的文章,它里面用的是 gif 动态图片进行展示,我觉得很不错.所以在我今天以及以后的博文中,我也会尽量使用 gif 动图进行展示.制作 ...

  6. 十五个最常用Linux命令行 - imsoft.cnblogs

    众多Linux管理员在使用Linux的时候会经常使用到很多Linux命令行,其中有绝大部分不是经常使用到的.在本文中主要为大家总结了经常使用的十五个最常用Linux命令行,希望对刚刚接触Linux命令 ...

  7. 《Linux命令行大全》系列(三、Linux 系统)

    在<Linux命令行大全>一书中,第3章名称是 Linux 系统. 概念太大,不过该节内容却是 Linux 系统最为核心的基础——查看 Linux 系统. ls 命令 显示目录自身信息或目 ...

  8. LINUX命令行操作

    Linux 命令行快捷键 7条回复 涉及在linux命令行下进行快速移动光标.命令编辑.编辑后执行历史命令.Bang(!)命令.控制命令等.让basher更有效率. 说明 Ctrl – k: 先按住 ...

  9. Linux命令行编辑快捷键

    Linux命令行编辑快捷键: history 显示命令历史列表 ↑(Ctrl+p) 显示上一条命令 ↓(Ctrl+n) 显示下一条命令 !num 执行命令历史列表的第num条命令 !! 执行上一条命令 ...

随机推荐

  1. Flink sql 之 join 与 StreamPhysicalJoinRule (源码解析)

    源码分析基于flink1.14 Join是flink中最常用的操作之一,但是如果滥用的话会有很多的性能问题,了解一下Flink源码的实现原理是非常有必要的 本文的join主要是指flink sql的R ...

  2. [敏捷软工团队博客]The Agiles 团队介绍&团队采访

    项目 内容 课程:北航-2020-春-敏捷软工 博客园班级博客 作业要求 团队作业-团队介绍和采访 团队名称来源 The Agile is The Agile. 敏捷就是敏捷.我们只是敏捷的践行者罢了 ...

  3. virtual box搭建虚拟机nat和host only网络配置实用

    virtual box搭建虚拟机nat和host only网络配置实用 一.背景 二.需求 二.设置虚拟机的网络 1.创建一个全局的nat网络 2.添加主机网络管理器 3.设置虚拟机网络 1.网卡1设 ...

  4. Noip模拟17 2021.7.16

    我愿称这场考试为STL专练 T1 世界线 巧妙使用$bitset$当作vis数组使用,内存不会炸,操作还方便,的确是极好的. 但是这个题如果不开一半的$bitset$是会炸内存的,因为他能开得很大,但 ...

  5. 五分钟搞懂spring-cloud-square

    欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 初识spring-cloud-square 2021年 ...

  6. 零基础玩转C语言单链表

    下图为最一简单链表的示意图: 第 0 个结点称为头结点,它存放有第一个结点的首地址,它没有数据,只是一个指针变量.以下的每个结点都分为两个域,一个是数据域,存放各种实际的数据,如学号 num,姓名 n ...

  7. MyBatis源码分析(四):SQL执行过程分析

    一.获取Mapper接口的代理 根据上一节,Mybatis初始化之后,利用sqlSession(defaultSqlSession)的getMapper方法获取Mapper接口 1 @Override ...

  8. readelf

    readelf的help内容如下所示: Usage: readelf <option(s)> elf-file(s) Display information about the conte ...

  9. P2774 方格取数问题(最小割)

    P2774 方格取数问题 一看题目便知是网络流,但由于无法建图.... 题目直说禁止那些条件,这导致我们直接建图做不到,既然如此,我们这是就要逆向思维,他禁止那些边,我们就连那些边. 我们将棋盘染色, ...

  10. hdu 4788 Hard Disk Drive (水题)

    题意: Input The first line contains an integer T, which indicates the number of test cases. For each t ...