以前由于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. AIApe问答机器人Scrum Meeting 4.29

    Scrum Meeting 4 日期:2021年4月29日 会议主要内容概述:汇报两日工作,讨论任务优先级. 一.进度情况 组员 负责 两日内已完成的工作 后两日计划完成的工作 工作中遇到的困难 李明 ...

  2. [no code][scrum meeting] Beta 12

    $( "#cnblogs_post_body" ).catalog() 例会时间:5月27日11:30,主持者:乔玺华 一.工作汇报 人员 昨日完成任务 明日要完成的任务 乔玺华 ...

  3. [技术博客]大闸蟹的技术博客,通过gitlab api进行用户批量创建

    技术博客--通过gitlab api批量注册用户 gitlab登录界面本身提供了register功能,但需要手工一个个添加,对于一次性会添加整个班级的学生的软工平台来说并不科学合理.使用gitlab ...

  4. linux shell exec 关联文件描述符

    在写shell脚本时,如果多个命令的输入或输出都是同一个文件,而这个文件的路径和名字都很长,则需要书写很多次同样的路径会很浪费时间,我们可以使用exec命令来关联一个自定义的文件描述符到一个特定的文件 ...

  5. live555 rtsp直播卡顿马赛克优化

    最近搞了个rtsp直播,初步是能用了,但是最终效果不是很好,客户不接受要求我们一定要继续优化. 原因是他们体验的时候发现会概率性出现马赛克和画面卡顿情况,经过我们测试验证,确实是有这个问题存在. 从原 ...

  6. 20191310李烨龙Linux C语言编程基础

    Linux C语言编程基础 任务详情 0. 基于Ubuntu或OpenEuler完成下面的任务(OpenEuler有加分) 1. 选择教材第二章的一节进行编程基础练习(2.10,2.11,2.12,2 ...

  7. HashSet的remove方法(一道面试题)

    1 public class CollectionTest { 2 3 @Test 4 public void test3(){ 5 HashSet set = new HashSet(); 6 Pe ...

  8. vue监听器watch & 计算属性computed

    侦听器watch vue中watch是用来监听vue实例中的数据变化 watch监听时有几个属性: handle:其值是一个回调函数,就是监听对象对话的时候需要执行的函数 deep:其值true 或者 ...

  9. Redis核心原理与实践--事务实践与源码分析

    Redis支持事务机制,但Redis的事务机制与传统关系型数据库的事务机制并不相同. Redis事务的本质是一组命令的集合(命令队列).事务可以一次执行多个命令,并提供以下保证: (1)事务中的所有命 ...

  10. git clone报错处理

    git clone过大的仓库时会报以下错误 remote: aborting due to possible repository corruption on the remote side. fat ...