HaifengSun的个人博客分享 http://blog.sciencenet.cn/u/HaifengSun

博文

R语言的传参

已有 3415 次阅读 2020-3-24 19:11 |系统分类:科研笔记

越南式三明治

1. mac或linux:

脚本第一行加:

#!/usr/bin/Rscript


2. commandArgs()传参:

测试脚本test.R:

args <- commandArgs()

print(args[1])

print(args[2])

Rscript test.R Hello R

输出为:

[1] "Hello"

[2] "R"


3. getopt传参:

install.packages("getopt")library("getopt")

# 首先将第一个参数的具体信息进行描述

# 每行五个,第五个可选,也就是说第五列可以不写

# byrow 按行填充矩阵的元素

# ncol  每行填充五个元素

spec <- matrix( c("first", "f", 2, "integer", "This is first!",

"second", "s", 1, "character", "This is second!",

"third", "t", 2, "double", "This is third!",

"help", "h", 0, "logical", "This is Help!"),

byrow=TRUE, ncol=5) 

# 使用getopt方法:

opt<-getopt(spec=spec)

# opt实际上就是一个列表,直接使用$来索引到对应的参数的值

print(opt$first)

print(opt$second)

print(opt$third)

在命令行执行

Rscript test.R -f 123 -t 1.1 -s Hello

输出为:

[1] 123

[1] "Hello"

[1] 1.1

参考:https://www.jianshu.com/p/8797972113d7



https://blog.sciencenet.cn/blog-3430889-1225099.html

上一篇:关于METTL4
下一篇:推荐一款免费下载论文的桌面版SciHub软件
收藏 IP: 218.94.21.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-25 17:27

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部