如何创建调用R脚本的shell脚本?
用于实践的示例问题(不适用于makrks)如何创建调用R脚本的shell脚本?,r,shell,R,Shell,用于实践的示例问题(不适用于makrks) #寻源功能 来源(“~/R/R-intro/myFuncs.R”) #载体 x当您在文件evalMeans.sh中写入时: Rscript selectMean.R 0 Rscript selectMean.R 1 R --no-save -s < selectMean.R --args 0 R --no-save -s < selectMean.R --args 1 在文件中选择mean.R: #reads argument on t
#寻源功能
来源(“~/R/R-intro/myFuncs.R”)
#载体
x当您在文件evalMeans.sh
中写入时:
Rscript selectMean.R 0
Rscript selectMean.R 1
R --no-save -s < selectMean.R --args 0
R --no-save -s < selectMean.R --args 1
在文件中选择mean.R
:
#reads argument on the command line
your_arg <- commandArgs(trailingOnly=TRUE)
#vector
x <-c(-10,-3,2,4,5,0.3,-0.1,2.1,3.2,-12.1,-1.2)
if (your_arg == 0) {
cat("The mean of the negative values of the data is", mean(x[x<0]), "\n")
} else if (your_arg == 1) {
cat("The geometric mean of the positive values of the data is",
exp(mean(log(x[x>0]))), "\n")
} else
cat("Error, incorrect command line argument")
它返回:
The mean of the negative values of the data is -5.28
The geometric mean of the positive values of the data is 2.07854
如果您没有Rscript
写入文件evalMeans.sh
:
Rscript selectMean.R 0
Rscript selectMean.R 1
R --no-save -s < selectMean.R --args 0
R --no-save -s < selectMean.R --args 1
R--no save-s
我不知道bash脚本是如何设置的,但是如果您使用Rscript,它有一个arguments--args
标志来传递参数。您好!非常感谢你的帮助!当我尝试获取“evalMeans.sh”源代码时,它会给我以下错误:$source evalMeans.sh bash:Rscript:command未找到bash:Rscript:command未找到我相信我位于正确的目录中,并且确实使用正确的名称保存了文件可能没有Rscript
。查看更新后的答案,如果sh
中的whereis Rscript
找到它,请重试。