[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
短参传入:
如果所有参数的首字母不同,用首字母即可;如果有些参数名称近似,则最好用多个字母,否则会辨别不了。
比如我这里的cog和cazy参数,首字母相同,明显不能都用c,我把其中一个改成大写的C也辨别不了;其中一个用一个首字母,另一个用两个首字母也不行。用co和ca就可以了。所以参数的名字一定要明显区分开来。
$ 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命令行进行参数传入?的更多相关文章
- linux命令行传递参数定期执行PHP文件
最近在做一个项目,需要在linux下传递参数定期执行PHP文件,网上查询资料,确实有相关资料,现整理如下: 1.linux执行PHP文件 #{PHP安装bin路径} {PHP文件路径} {需要参数1 ...
- 如何在Linux命令行中创建以及展示演示稿
导读 你在准备一场演讲的时候,脑海可能会先被图文并茂.形象华丽的演示图稿所占据.诚然,没有人会否认一份生动形象的演讲稿所带来的积极作用.然而,并非所有的演讲都需要TED Talk的质量.更多时候,演讲 ...
- 如何在linux命令行无界面下使用selenium
1.安装Xvfb和pyvirtualdisplay sudo apt-get install xvfbpip install pyvirtualdisplay 2.安装chrome, chromedr ...
- 在linux命令行下执行php 程序
如何在linux命令行下,执行php程序. 例子 打印当前时间 php -r "echo time()" 随机输出一个数字 php -r "echo rand(1,20) ...
- 像黑客一样使用 Linux 命令行
前言 之前在博客园看到一篇介绍 IntelliJ IDEA 配置的文章,它里面用的是 gif 动态图片进行展示,我觉得很不错.所以在我今天以及以后的博文中,我也会尽量使用 gif 动图进行展示.制作 ...
- 十五个最常用Linux命令行 - imsoft.cnblogs
众多Linux管理员在使用Linux的时候会经常使用到很多Linux命令行,其中有绝大部分不是经常使用到的.在本文中主要为大家总结了经常使用的十五个最常用Linux命令行,希望对刚刚接触Linux命令 ...
- 《Linux命令行大全》系列(三、Linux 系统)
在<Linux命令行大全>一书中,第3章名称是 Linux 系统. 概念太大,不过该节内容却是 Linux 系统最为核心的基础——查看 Linux 系统. ls 命令 显示目录自身信息或目 ...
- LINUX命令行操作
Linux 命令行快捷键 7条回复 涉及在linux命令行下进行快速移动光标.命令编辑.编辑后执行历史命令.Bang(!)命令.控制命令等.让basher更有效率. 说明 Ctrl – k: 先按住 ...
- Linux命令行编辑快捷键
Linux命令行编辑快捷键: history 显示命令历史列表 ↑(Ctrl+p) 显示上一条命令 ↓(Ctrl+n) 显示下一条命令 !num 执行命令历史列表的第num条命令 !! 执行上一条命令 ...
随机推荐
- abstract使用方式
springMVC中的 LocalContextHolder是一个 abstract类.里边方法都是static 的. 不能被继承.不能实例化.只能调用其定义的static 方法.这种 abstrac ...
- netty中使用protobuf实现多协议的消息
在我们使用 netty 的过程中,有时候为了高效的传输数据,经常使用 protobuf 进行数据的传输,netty默认情况下为我们实现的 protobuf 的编解码,但是默认的只能实现单个对象的编解码 ...
- Noip模拟54 2021.9.16
T1 选择 现在发现好多题目都是隐含的状压,不明面给到数据范围里,之凭借一句话 比如这道题就是按照题目里边给的儿子数量不超过$10$做状压,非常邪门 由于数据范围比较小,怎么暴力就怎么来 从叶子节点向 ...
- QEvent
QEvent类是所有事件类的基类,每一个对象都包含事件参数.Qt的主事件循环(QCoreApplication::exec())从事件队列中接收本地窗口系统的事件,并将它们翻译成QEvent,将这些事 ...
- [转]浅谈电路设计中应用DDR3处理缓存问题
本文转自:浅谈电路设计中应用DDR3处理缓存问题_若海人生的专栏-CSDN博客 DDR系列SDRAM存储芯片的高速率.高集成度和低成本使其理所当然成为存储芯片中的一霸.在PC和消费电子领域自是如此,它 ...
- linux job
通常运行的进程 ctrl-z之后会暂停到后台 bash test.sh Linux-4.15.0-36-generic-x86_64-with-Ubuntu-16.04-xenial #39~16.0 ...
- SpringBoot配置文件自动映射到属性和实体类(8)
一.配置文件加载 1.Controller中配置并指向文件 @Controller @PropertySource(value = { "application.properties&quo ...
- List of devices attached 没有手机设备号 解决办法
问题: cmd下使用adb devices 没有找到手机设备,如下图 解决办法: 采用360手机助手给我们自动安装对应的手机驱动,或者去对应的手机官网下载对应的驱动,这里手机要开启调试模式 我是opp ...
- JMeter学习笔记--工具简单介绍
一.JMeter 介绍 Apache JMeter是纯JAVA桌面应用程序,被设计为用于测试客户端/服务端结构的软件(例如web应用程序).它可以用来测试静态和动态资源的性能,例如:静态文件,Java ...
- 菜鸡的Java笔记 数字操作类
数字操作类 Math 类的使用 Random 类的使用 BigInteger 和 BigDecimal 类的使用 Math 是一 ...