如何在 Linux 中将 R 脚本附加到 shell 脚本来执行?

如何在 Linux 中将 R 脚本附加到 shell 脚本来执行?

我正在尝试提交要在集群上执行的作业。这是通过编写sbatch提交脚本来完成的。该工作涉及打开 R 3.1.3 并在服务器上运行引用的 R 脚本。

这是我写的shell脚本:

#!/bin/bash
#SBATCH --account=810639
#SBATCH --time=1200
#SBATCH --mem-per-cpu=4096
#SBATCH --ntasks=1
#SBATCH --constraint=normalmem
#SBATCH --output=output_%j.txt
#SBATCH --error=error_output_%j.txt
#SBATCH --job-name=AggrigatePIXEL
#SBATCH --partition=ESG_Std
#SBATCH --mail-type=FAIL
#SBATCH [email protected]
# print date and time
module load R/3.1.3
module load geos/gcc/64/3.4.2
module load netcdf/gcc/64/4.3.3
module load gdal/gcc/64/1.11.1
source('AggrigatePIXEL_Forecast_easyVerfication_SERVERversion.R')

当我尝试运行 shell 脚本(另存为 yate.sh)时,它给出以下错误消息:

./yate.sh: line 20: syntax error near unexpected token
'AggrigatePIXEL_Forecast_easyVerfication_SERVERversion.R'

./yate.sh: line 20: 
source('AggrigatePIXEL_Forecast_easyVerfication_SERVERversion.R')

我知道问题与打开和执行 .R 脚本有关,该脚本需要先运行 R 程序。任何人都可以帮助我如何在 shell 脚本中指示这一点吗?

答案1

我不熟悉 R,但您的脚本尝试作为 bash 脚本运行,而不是 R 脚本。将您的第一行更改为#!/usr/bin/Rscript或 可能#!/usr/bin/env Rscript。您可能需要更改系统上 R 所在位置的路径。

根据http://stat.ethz.ch/R-manual/R-devel/library/utils/html/Rscript.html它应该像这个例子一样开始:

#! /path/to/Rscript --vanilla --default-packages=utils
args <- commandArgs(TRUE)
res <- try(install.packages(args))
if(inherits(res, "try-error")) q(status=1) else q()`

答案2

使用 Rscript 代替 source() 运行:

Rscript AggrigatePIXEL_Forecast_easyVerfication_SERVERversion.R

相关内容