1. 程式人生 > 其它 >R指令碼中使用命令列 進行傳參

R指令碼中使用命令列 進行傳參

1、

root@PC1:/home/test# cat test.r
library(optparse)

option_list <- list(
  make_option(c("-p", "--arg1"), type = "character", default=FALSE),
  make_option(c("-q", "--arg2"), type = "character", default=FALSE),
  make_option(c("-m", "--arg3"), type = "character", default=FALSE)
)
opt_parser = OptionParser(option_list=option_list);
opt 
= parse_args(opt_parser); file1 = read.table(opt$arg1,header = F) file2 = read.table(opt$arg2,header = F) file3 = read.table(opt$arg3,header = F) file4 = rbind(file3, file2, file1) write.table(file4, "x.csv", row.names = F, col.names = F, sep = "\t")
root@PC1:/home/test# cat a.txt
1       1       1
1 1 1 root@PC1:/home/test# cat b.txt 2 2 2 2 2 2 root@PC1:/home/test# cat c.txt 3 3 3 3 3 3

2、測試執行

root@PC1:/home/test# Rscript test.r --arg1 a.txt --arg2 b.txt --arg3 c.txt
root@PC1:/home/test# ls
a.txt  b.txt  c.txt  test.r  x.csv
root@PC1:/home/test# cat x.csv
3 3 3 3 3 3 2 2 2 2 2 2 1 1 1 1 1 1