我在String变量中有一个Rscript,我想从Java程序执行它并将一些变量传递给它.如果我独立执行该R脚本,它可以正常工作.我通过使用Python程序将所有R脚本转换为一行,如下所示:
import json
jsonstr = json.dumps({"script": """\
#!/usr/bin/Rscript
# read the data file
library('jsonlite')
library('rpart')
args
我使用打印出来的字符串并将其存储在String变量中,然后我执行下面的代码,它根本不起作用.我将path和client_users变量传递给上面的R脚本.
public static void main(String[] args) throws IOException,InterruptedException {
// this is your script in a string
// String script = "#!/bin/bash\n\necho \"Hello World\"\n\n readonly PARAM1=$param1\n echo $PARAM1\n\nreadonly PARAM2=$param2\n echo $PARAM2\n\n";
String script = "above R Script here";
List
上面的代码适用于bash shell脚本.我需要为R脚本做些什么特别的事吗?我将对bash脚本和R脚本使用相同的代码.
这是我得到的错误:
/bin/bash: line 7: -: No such file or directory /bin/bash: line 10: Syntax error near unexpected token `'jsonlite'' /bin/bash: line 10: `library('jsonlite')'
如果我删除commandList.add(“/ bin / bash”);并添加commandList.add(“/ bin / Rscript”);然后我看到下面的错误:
Cannot run program "/bin/Rscript": error=2,No such file or directory
更新: –
我没有使用上面的脚本,而是决定在r中使用简单的打印地狱脚本来查看是否可以通过Java执行它.
// this will print hello
String script = "#!/usr/bin/env Rscript\nsayHello
当我用commandList.add(“/ bin / bash”);执行此脚本时,我收到此错误:
/bin/bash: line 2: Syntax error near unexpected token `('
/bin/bash: line 2: `sayHello
但是,如果我使用此commandList.add(“/ bin / sh”);执行,我收到此错误:
/bin/sh: 2: Syntax error: "(" unexpected