[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

短參傳入:
如果所有參數的首字母不同,用首字母即可;如果有些參數名稱近似,則最好用多個字母,否則會辨別不了。
比如我這里的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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM