【问题标题】:Why does this R script not work when called from this bash script?为什么从这个 bash 脚本调用这个 R 脚本不起作用?
【发布时间】:2014-09-18 10:17:45
【问题描述】:

我有以下 R 脚本,为此我添加了 cmets 来制作一个简单的示例,其中包含我想要实现的汽车:

#!/usr/bin/env Rscript

# the arguments come in this way: 
# args[1] is a file containing the maximum speeds of different cars (one per line) 
# args[2] is the title that the plot will have
# args[3] contains the horsepower of the engine of the first car in args[1] (the lowest)
# args[4] contains the horsepower of the engine of the last car in args[1] (the highest)
# NOTE1: the speeds in args[1] must be listed starting from the car 
# with the lowest horsepower to the car with the highest horsepower 
# NOTE2: in args[1], a car must differ from the next one by 1 horsepower, i.e., if
# there are 5 speeds, and the horsepower of the first car in the file is 30, then the 
# the horsepower of the second one must be 31, the third one 32, .... the fifth one must
# be 34.


args<-commandArgs(TRUE)

# creating the vector with the horsepower of each car

horsepowers = numeric()

for (i in args[3]:args[4]) {

        horsepowers = c(horsepowers,i)

}

# reading file with speeds and getting vector with speeds

speeds <- read.csv(file=args[1],head=FALSE,sep="\n")$V1

# creating plot with speeds in previous vector

outputTitle = gsub(" ","", args[2] , fixed=TRUE)

pdf(paste(outputTitle, ".pdf", sep = ""))

plot(horsepowers, speeds, type="o", col="red", xlab="horsepowers", ylab="speeds")

# giving a title to the plot

title(main=args[2], col.main="Black")

我有一个名为myFile 的示例文件,它的速度为 5 辆汽车,看起来像这样

150
156
157
161
164

假设第一辆车的马力是30,那么最后一辆车的马力就是34(记住第一档对应马力最小的车,第二档对应第二档的车)最低马力,依此类推;并且汽车必须相差 1 马力,否则脚本中的 for 循环将没有意义)。因此,如果我像这样在命令行中运行脚本:

./myPlotter.R myFile "My Title" 30 34

它工作正常并制作了情节(我裁剪了 y 标签、x 标签和标题,只是因为它们与上面的汽车示例不匹配,但使用的脚本是相同的,我只是更改了车例):

但是,当从以下 bash 脚本调用时:

#!/bin/bash

while getopts ":a:1:2:3:4" arg; do

case "$arg" in

a)
    option=$OPTARG
    ;;
1)
    fileWithSpeeds=$OPTARG
    ;;
2)
    titleOfGraph=$OPTARG
    ;;
3)
    lowestHP=$OPTARG
    ;;
4)
    highestHP=$OPTARG
    ;;

esac

done

# I do not think that this if statement makes any difference for this example
# but I left it there just in case

if [ $option == "option1" ]; then

        ./myPlotter.R $fileWithSpeeds $titleOfGraph $lowestHP $highestHP

fi

这样:

./bashPlot.sh -a option1 -1 myFile -2 "My Title" -3 30 -4 34

我收到以下错误:

Error in args[3]:args[4] : NA/NaN argument
Execution halted

这是什么原因造成的?

总结:我有一个 R 脚本,从命令行调用时可以正常工作,但从带有从 getopts 获取的参数的 bash 脚本调用时会出错。

【问题讨论】:

  • 你能把命令改成echo "./myPlotter.R $fileWithSpeeds $titleOfGraph $lowestHP $highestHP",看看它到底想运行什么吗?

标签: r bash


【解决方案1】:

错误在getots 字符串中,而不是这个:

while getopts ":a:1:2:3:4" arg; do

应该是这样的:

while getopts "a:1:2:3:4:" arg; do

您可以通过回显执行 R 脚本的命令来查看问题:

echo "./myPlotter.R $fileWithSpeeds $titleOfGraph $lowestHP $highestHP"
./myPlotter.R $fileWithSpeeds $titleOfGraph $lowestHP $highestHP

您可以从输出中看到$highestHP 参数始终为空。

额外提示

这里不需要双引号:

if [ $option == "option1" ]; then

[ ... ] 运算符现在已过时,请改用[[ ... ]],如下所示:

if [[ $option == option1 ]]; then

【讨论】:

    猜你喜欢
    • 2020-12-14
    • 2018-12-12
    • 2015-06-16
    • 2013-10-20
    • 2014-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多