为什么我的 bash 无法执行 R 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2045706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:32:20  来源:igfitidea点击:

Why my bash can't execute R script?

bashshellscriptingrrscript

提问by Fabien Barbier

My script use an access to mysql to get command arguments to launch Rscript. Its use is as follows : Rscript $RFILE $ARGUMENTS (RFILE corresponding to path to Rscript, and ARGUMENTS corresponding to path file used and agr).

我的脚本使用对 mysql 的访问来获取启动 Rscript 的命令参数。它的用法如下: Rscript $RFILE $ARGUMENTS(RFILE对应Rscript的路径,ARGUMENTS对应使用的路径文件和agr)。

I try, different way, but I still have errors, here a copy of my bash script :

我尝试以不同的方式,但我仍然有错误,这里是我的 bash 脚本的副本:

#!/usr/bin/env bash
# Execute R process
# -----------------
### Mysql Setup ###
USER=...
PASS=...
HOST=...
DB=...

# Get Job ID process
# Use to retrieve args in my DB
ID=

# Get script name
RFILE=$(mysql -u$USER -p$PASS -e "SELECT script_name FROM JobProcess WHERE script_run_id=$ID;" $DB)
SUBSTRING="script_name"
RFILE="${RFILE//$SUBSTRING}"

# Get script_args
ARGUMENTS=$(mysql -u$USER -p$PASS -e "SELECT script_args FROM JobProcess WHERE script_run_id=$ID;" $DB)
SUBSTRING2="script_args"
ARGUMENTS="${ARGUMENTS//$SUBSTRING2}"

RUN="Rscript $RFILE $ARGUMENTS"

# Try Different execute process
Rscript $RFILE $ARGUMENTS
#eval "$RUN"
#`Rscript $RFILE $ARGUMENTS`
#$RUN

I verified my command line (via echo), and if I made a copy-paste to my shell I can run my R script. But from my bash, I can't execute my script (but command line is good).

我验证了我的命令行(通过 echo),如果我复制粘贴到我的 shell,我可以运行我的 R 脚本。但是从我的 bash 来看,我无法执行我的脚本(但命令行很好)。

By using : Rscript $RFILE $ARGUMENTS, Rscript $RFILE $ARGUMENTSand $RUN, I have this error :

通过使用:Rscript $RFILE $ARGUMENTSRscript $RFILE $ARGUMENTS和 $RUN,我有这个错误:

Error in parse(text = args[[i]]) : 
  unexpected end of input in ""path_in='/Users/GR/web-app/Rproject/Inputs/Rscript/Gene-level"
Calls: eval -> parse
Execution halted

By using : eval "$RUN", I have this error :

通过使用:eval“$RUN”,我有这个错误:

/Users/GR/web-app/Rproject/Scripts/Rscript.sh: line 38: /Users/GR/web-app/Rproject/Scripts/arg_file_test.R: Permission denied
/Users/GR/web-app/Rproject/Scripts/Rscript.sh: line 44: path_in<-"/Users/GR/web-app/Rproject/Inputs/Rscript/Gene-level Description for Modules.csv": No such file or directory

If I try this in my shell script, all works fine :

如果我在我的 shell 脚本中尝试这个,一切正常:

SCRIPT="/Users/GR/web-app/Rproject/Scripts/arg_file_test.R"
FILE1="path_in='/Users/GR/web-app/Rproject/Inputs/Rscript/Gene-level Description for Modules.csv'"
FILE2="path_in2='/Users/GR/web-app/Rproject/Inputs/Rscript/Template_Auto.csv'"
FILES="\"$FILE1\" \"$FILE2\""
ARG="l=32 w=33"
RUN="Rscript $SCRIPT $FILES $ARG"

Someone has an idea ?

有人有想法吗?

Thanks

谢谢

采纳答案by Fabien Barbier

I put a copy of script fix, but I still have some questions :

我放了一个脚本修复的副本,但我还有一些问题:

#!/usr/bin/env bash
# Execute R process
# -----------------
### Mysql Setup ###
USER=...
PASS=...
HOST=...
DB=...

# Get Job ID process
# Use to retrieve args in my DB
ID=

# Get script name
RFILE=$(mysql -u$USER -p$PASS -se "SELECT script_name FROM JobProcess WHERE script_run_id=$ID;" $DB)

# Get script_args
ARGUMENTS=$(mysql -u$USER -p$PASS -se "SELECT script_args FROM JobProcess WHERE script_run_id=$ID;" $DB)

RUN="Rscript $RFILE $ARGUMENTS"
eval "$RUN"

Actually, I can't use (probably due to bad seperated token) : Rscript $RFILE $ARGUMENTS, or $RUN.

实际上,我不能使用(可能是由于分离的令牌不好):Rscript $RFILE $ARGUMENTS 或 $RUN。

The purpose of this script is to run any R script by recovering all the parameters in database. Here a copy of data in my DB :

此脚本的目的是通过恢复数据库中的所有参数来运行任何 R 脚本。这是我的数据库中的数据副本:

script_run_id : 161
script_name : /Users/GR/web-app/Rproject/Scripts/arg_file_test.R
script_args : 'path_in<-"/Users/GR/web-app/Rproject/Inputs/Rscript/Gene.csv"' 'path_in2<-"/Users/GR/web-app/Rproject/Inputs/Rscript/Template_Auto.csv"' l=0 w=0 

Part script_name define R script to call, and scripts_args define arguments used with this R script. In this example, my script generate this command line :

部分 script_name 定义要调用的 R 脚本,scripts_args 定义与此 R 脚本一起使用的参数。在这个例子中,我的脚本生成这个命令行:

Rscript /Users/GR/web-app/Rproject/Scripts/arg_file_test.R 'path_in<-"/Users/GR/web-app/Rproject/Inputs/Rscript/Gene.csv"' 'path_in2<-"/Users/GR/web-app/Rproject/Inputs/Rscript/Template_Auto.csv"' l=0 w=0

Is there a better way to proceed ?
For example, define a new column for files path (ex:script_Files) ?
Separate arguments with comma (ex:l=0, w=3, r=6, etc), and split string into array (an example can help me) ?

有没有更好的方法来进行?
例如,为文件路径定义一个新列(例如:script_Files)?
用逗号分隔参数(例如:l=0、w=3、r=6 等),并将字符串拆分为数组(一个示例可以帮助我)?

Thanks
ps : A good way, is probably to use directly Rscript.

谢谢
ps: 一个好办法,大概就是直接用Rscript吧。

回答by Dirk Eddelbuettel

Why is this a bashscript when you have Rscript? So why don't you rewrite this as an R script executed by Rscript.exeallowing you to test the components

为什么这是一个bash脚本,当你有Rscript?那么为什么不将其重写为通过Rscript.exe允许您测试组件来执行的 R 脚本

  • intialization
  • database connection
  • core work
  • ...
  • 初始化
  • 数据库连接
  • 核心工作
  • ...

individually?

个人?

Edit (in response to your comment):R can call R, either packages via library()or directly via source(). You have a debug problem that is comples and you should try to remove some complextity. Moreover, R scripts can use the getopt or optparse packages to deal with command-line arguments.

编辑(响应您的评论):R 可以调用 R,可以通过包library()或直接通过source(). 您有一个复杂的调试问题,您应该尝试消除一些复杂性。此外,R 脚本可以使用 getopt 或 optparse 包来处理命令行参数。

Edit 2:Are you aware that R has an RMySQLpackage that would allow you to call the db from R?

编辑 2:你知道 R 有一个RMySQL包可以让你从 R 调用数据库吗?

回答by James Polley

In the sample output from Rscript you quoted:

在您引用的 Rscript 示例输出中:

Error in parse(text = args[[i]]) : 
  unexpected end of input in ""path_in='/Users/GR/web-app/Rproject/Inputs/Rscript/Gene-level"

I notice that there's very odd quoting going on there: two "s at the start, a single 'after the =, and just a single "on the end.

我注意到那里有非常奇怪的引用:"开头有两个s,在'之后=有一个",最后只有一个。

At the very least I'd expect a matching 'at the end of the path, and probably something to balance those "s as well

至少我希望'在路径的尽头有一个匹配,并且可能还有一些东西可以平衡这些"s

I'm guessing it's related to this line:

我猜它与这条线有关:

RFILE="${RFILE//$SUBSTRING}"

I'm guessing that you're removing one of the 's in that substitution.

我猜您正在删除'该替换中的s 之一。

Updated: As others have pointed out, your filenames contain spaces. alwaysput "s around the name of a variable name when it contains filenames, especially when you know those filenames contain spaces. Try:

更新:正如其他人所指出的,您的文件名包含空格。当变量名包含文件名时,始终"s放在变量名周围,尤其是当您知道这些文件名包含空格时。尝试:

Rscript "$RFILE" $ARGUMENTS

回答by JaakkoK

It appears that you are losing some of the quoting of spaces somewhere. You have this error message:

您似乎在某处丢失了一些空格的引用。您收到此错误消息:

Error in parse(text = args[[i]]) : 
  unexpected end of input in ""path_in='/Users/GR/web-app/Rproject/Inputs/Rscript/Gene-level"

but from other texts the file name should evidently be

但从其他文本来看,文件名显然应该是

/Users/GR/web-app/Rproject/Inputs/Rscript/Gene-level Description for Modules.csv

This would also make sense as an explanation for your problem, as the variable substitution you are doing could well cause a space-containing argument to lose its protective quoting. Would it be possible for you to rename that file (and any other similar ones) to a name that does not contain spaces, say, by replacing spaces with underscores, and trying again?

这也可以解释您的问题,因为您正在执行的变量替换很可能导致包含空格的参数失去其保护性引用。您是否可以将该文件(以及任何其他类似文件)重命名为不包含空格的名称,例如,通过用下划线替换空格,然后再试一次?

回答by rcarson

First you may want to try adding -s (updating -e to -se) to your mysql line:

首先,您可能想尝试将 -s(将 -e 更新为 -se)添加到您的 mysql 行:

RFILE=$(mysql -u$USER -p$PASS -se "SELECT script_name FROM JobProcess WHERE script_run_id=$ID;" $DB)

You should be able to remove your SUBSTRING replacement. Same goes for your variables ARGUMENTS. Replace -e with -se in your mysql line and remove the SUBSTRING replace.

您应该能够删除您的 SUBSTRING 替换。您的变量 ARGUMENTS 也是如此。在您的 mysql 行中用 -se 替换 -e 并删除 SUBSTRING 替换。

This probably won't fix your issue but it will remove any questions about your expansion replacement. Although if for some reason there was an \n or something at the end of the first line, where you replace SUBSTRING in $RFILE and $ARGUMENTS...

这可能不会解决您的问题,但会消除有关您的扩展更换的任何问题。尽管如果由于某种原因在第一行的末尾有一个 \n 或其他东西,您可以在其中替换 $RFILE 和 $ARGUMENTS 中的 SUBSTRING ...

回答by rcarson

okay, I think you may need to change what you are storing in your database.

好的,我认为您可能需要更改您在数据库中存储的内容。

your script_args field should probably look like this in your db:

您的 script_args 字段在您的数据库中可能看起来像这样:

path_in=\"/Users/GR/web-app/Rproject/Inputs/Rscript/Gene.csv\" path_in2=\"/Users/GR/web-app/Rproject/Inputs/Rscript/Template_Auto.csv\" l=0 w=0

path_in=\"/Users/GR/web-app/Rproject/Inputs/Rscript/Gene.csv\" path_in2=\"/Users/GR/web-app/Rproject/Inputs/Rscript/Template_Auto.csv\" l= 0 w=0

without any other punctuation or escapes... When you populate your variable and then expand it during your eval or other method it would look like this after expansion:

没有任何其他标点符号或转义...当您填充变量然后在 eval 或其他方法期间扩展它时,扩展后看起来像这样:


Rscript /Users/GR/web-app/Rproject/Scripts/arg_file_test.R path_in="/Users/GR/web-app/Rproject/Inputs/Rscript/Gene.csv" path_in2="/Users/GR/web-app/Rproject/Inputs/Rscript/Template_Auto.csv" l=0 w=0

Also you probably don't need eval in this case and you could run $RUN by itself without eval or even just put

此外,在这种情况下您可能不需要 eval 并且您可以在没有 eval 的情况下自行运行 $RUN 甚至只是放置

Rscript $RFILE $ARGUMENTS

on a line all by itself.

在一条线上。