美文网首页
R语言命令行参数传递函数

R语言命令行参数传递函数

作者: BBio | 来源:发表于2020-04-02 15:29 被阅读0次

R语言命令行参数传递函数

  • commandArgs()R自带函数,以位置顺序传递参数,使用于简单脚本
  • getopt()getopt包中函数。可以输出帮助文档,好!
  • OptionParseroptparse包中函数。也可以输出帮助文档,真香!

gzh:BBio

详解

commandArgs

#test.r:待执行的脚本文件
#文件内容:
args <- commandArgs(trailingOnly = FALSE)
cat(args)

#执行
Rscript test.r a b c
#return:返回R的路径,脚本的路径等
#[1] "/path/R"
#[2] "--slave"
#[3] "--no-restore"
#[4] "--file=test.r"
#[5] "--args"
#[6] "a"
#[7] "b"
#[8] "c"

#test.r
#args <- commandArgs(T)
#cat(args)

Rscript test.r a b c
#return:第一个参数就是a,第二个参数是b,类推
#[1] "a" 
#[2] "b"
#[3] "c"

getopt

getopt(spec = NULL, opt = NULL, command = get_Rscript_filename(), usage = FALSE, debug = FALSE)
#参数说明
#spec:一个4列或者5列的矩阵,设置参数的类型
#    第一列为longflag
#    第二列为shortflag
#    第三列为参数的类型,0代表不需要参数,1代表可选参数,2代表必须指定参数
#    第四列为参数的数据类型,integer、logical、double、character、complex
#    第五列为注释,可不填写。

#opt:默认从commandArgs接入参数
#command:命名执行的脚本文件,默认真实名字
#usage:输出帮助文档

#test.r:待执行的脚本文件
#文件内容:
library("getopt")

options<-matrix(c(
    "help", "h", "0", "logical", "help",
    "first", "f", "1", "integer", "first argument",
    "second", "s", "1", "double", "second argument",
    "third", "t", "2", "character", "third argument"
), ncol=5, byrow=T)

opt<-getopt(options)

if(is.null(opt$help)
    cat(getopt(options, usage=T))
    q() 
}
   
#执行
Rscript test.r -f 5 -s 3.14 -t a_b
#return:
#$first
#[1] 5
#$second
#[1] 3.14
#$third
#[1] "a_b"
   
#执行
Rscript test.r -h
#return:
#Usage: test.r [-[-help|h]] [-[-first|f] <integer>] [-[-second|s] <double>] [-[-third|t] #[<character>]]
#    -h|--help      help
#    -f|--first     first argument
#    -s|--second    second argument
#    -t|--third     third argument

optparse

#需要安装optparse包

#1.使用make_option函数构建参数列表
make_option(opt_str, action = "store", type = NULL, dest = NULL,
default = NULL, help = "", metavar = NULL, callback = NULL,
callback_args = NULL)
#参数说明
#opt_str:参数字符串,"--version"或者c("-v", "--version")
#action:store代表保存输入的参数值,store_true代表保存"TRUE",store_false代表保存"FALSE"
#type:参数数据类型
#dest:参数保存的名字,默认是opt_str中的longflag
#default:默认值
#help:帮助文档,使用-h时输出

#2.OptionParser函数解析参数列表
OptionParser(usage = "usage: %prog [options]", option_list = list(),
add_help_option = TRUE, prog = NULL, description = "", epilogue = "")
#参数说明
#usage:使用说明,%prog代表prog参数的值,默认是脚本名称
#option_list:参数列表
#add_help_option:是否使用-h输出帮助文档
#description:脚本功能描述

#3.parse_args解析命令
parse_args(object, args = commandArgs(trailingOnly = TRUE),
print_help_and_exit = TRUE, positional_arguments = FALSE,
convert_hyphens_to_underscores = FALSE)

########################################################################################
#test.r:待执行的脚本文件
#文件内容:
library("optparse")

option_list <- list(
    make_option("--version", action="store_true", help="Print script version"),
    make_option(c("-f", "--first"), action="store", default=1, type="integer", help="first argument [default %default]"),
    make_option(c("-s", "--second"), default="second argument", type="character", help="second argument [default %default]"),
    make_option(c("-q", "--quiet"), action="store_false", dest="version", help="dont`t Print output")
)

opt <- parse_args(OptionParser(
    usage = "usage: %prog [options]",
    option_list = option_list,
    add_help_option = T,
    description="\nDecription: a script for test"))

#执行
Rscript test.r -h
#return:
#Usage: test.r [options]
#
#Decription: a script for test
#
#Options:
#   --version
#       Print script version
#
#   -f FIRST, --first=FIRST
#       first argument [default 1]
#
#   -s SECOND, --second=SECOND
#       second argument [default second argument]
#
#   -q, --quiet
#       dont`t Print output
#
#   -h, --help
#       Show this help message and exit

#执行
Rscript test.r --version -f 123 -s abc 
#return:
#$version
#[1] TRUE
#
#$first
#[1] 123
#
#$second
#[1] "abc"
#
#$help
#[1] FALSE

相关文章

  • R语言接收命令行参数

    R语言接收命令行参数 调用结果如下:

  • R语言接受命令行参数的三种方式

    R语言接受命令行参数的三种方式 最近要用一下R的命令行,但是发现命令行参数不是很友好,就总结了一下目前常用的R的命...

  • iOS 如何在其他类中调用本类的方法

    宏定义 - 传递函数、参数

  • go 语言第三节课

    命令行参数 1.C语言中的命令行参数在C语言中main函数可以接收两个参数int main(int argc, c...

  • pflag命令行参数

    简介 本文讲解如何在go程序中使用命令行参数包pflag来获取gong语言编写的命令行程序的传入参数。 命令行参数...

  • R语言_函数认知&R包安装

    主要从以下三方面去学习R语言函数与R包: 1.R语言函数:形式参数实际参数默认参数了解函数的方式2.R包:什么是R...

  • requirejs

    使用 r.js 命令行参数可以与构建配置文件属性互换您可以在命令行上指定选项:node r.js -o baseU...

  • 【使用Python打造Linux命令行工具1】

    一、与命令行相关的python语言特性 使用sys.argv获取命令行参数 sys库中有一个保存所有命令行参数的a...

  • Go语言命令行和运算符

    命令行参数 在Go语言中main函数是不接收任何参数的, 但是还是可以通过其它方式来接收命令行参数 方法一 导入o...

  • Spark编程讲解

    Spark严重依赖传递函数类型的参数,即 在spark中,transformation 和 action接收的参数...

网友评论

      本文标题:R语言命令行参数传递函数

      本文链接:https://www.haomeiwen.com/subject/hxohphtx.html