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

 

posted @ 2021-10-23 22:01  小鲨鱼2018  阅读(132)  评论(0编辑  收藏  举报