欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

在Linux系统中使用Rscript运行R脚本

最编程 2024-01-07 10:24:44
...

之前R都是在Windows运行比较多,Rstudio基本满足数据分析和绘图需求。但是这次通过R进行WGCNA,基因数目比较多Windows带不动,所以转战Linux。学校的大型机是不允许login直接跑计算的,而bsub如何提交R作业?

方法一:R CMD BATCH

格式:R CMD BATCH [options] [infile] [outfile]

  • [options] :
    --help 查看帮助信息
    --version 查看R版本
    --slave 只打印R脚本的输出,而不显示脚本具体执行情况
    --no-timing 去除输出文档结束的运行时间输出
    默认--restore -- save --no-readline
  • [infile] :输入R脚本文件
  • [outfile]:如果没有命名,会照搬infile名字+ .Rout

在linux下,因为BATCH指令的权限问题,将会导致非root权限无法调用此条指令。这时使用方法三Rscript file代替即可。

方法二:R

格式:R [options] [< infile] [> outfile]

  • [options] :
    --help 查看帮助信息
    --version 查看R版本
    --slave 只打印R脚本的输出,而不显示脚本具体执行情况
    --vanilla是 --no-save, --no-restore, --no-site-file, --no-init-file and --no-environ的合体
    --args,这个就是传递给.R文件的参数,在脚本中调用这个参数需要用到commandArgs()这个函数。
    注意:options必须选择--save,--no-save, --vanilla三个中的一个。
示例:
# R脚本 test.R
args <- commandArgs(TRUE) 
paste(c("I", "like", args[1], "and", args[2], "!"), collapse = " ")

运行:

$ R --slave --vanilla --args tea coffee<test.R >testR.Rout
#tea和coffee是传递的参数

查看输出结果:

$ cat testR.Rout
WARNING: ignoring environment value of R_HOME
[1] "I like tea and coffee !"

方法三:Rscript

格式:Rscript [--options] [-e expr] file [args] [> outfile]

  • [-- options] :
    默认--slave --no-restore
    其他选项与方法二中类似
  • [- e expr] :可以通过expr输入R的表达式。这个我不是很理解,想明白了再来补充
  • file :输入脚本文件名
  • [args] :与方法二中一致
  • [> outfile] :输出文件名
示例:
# R脚本 Rscript.R
x<-sample(1:10,3)
print(x)
# 运行 (login运行)
$ Rscript Rscript.R >Rscript.out
# 运行 (bsub)
$ bsub -n 32 Rscript Rscript.R >Rscript.out
# 查看结果
$ cat Rscript.out
WARNING: ignoring environment value of R_HOME
[1] 5 3 7

参考资料:
https://www.cnblogs.com/xianghang123/archive/2013/01/08/2851480.html
http://www.360doc.com/content/11/1201/22/5013584_169013651.shtml

推荐阅读