无法从java程序执行R脚本?

前端之家收集整理的这篇文章主要介绍了无法从java程序执行R脚本?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我在String变量中有一个Rscript,我想从Java程序执行它并将一些变量传递给它.如果我独立执行该R脚本,它可以正常工作.我通过使用Python程序将所有R脚本转换为一行,如下所示:

  1. import json
  2. jsonstr = json.dumps({"script": """\
  3. #!/usr/bin/Rscript
  4. # read the data file
  5. library('jsonlite')
  6. library('rpart')
  7. args <- as.list(Sys.getenv(c(
  8. "path","client_users")))
  9. if (args[["path"]]==""){
  10. args[["path"]] <- "."
  11. }
  12. # other stuff here
  13. # other stuff here
  14. """})
  15. print jsonstr

我使用打印出来的字符串并将其存储在String变量中,然后我执行下面的代码,它根本不起作用.我将path和client_users变量传递给上面的R脚本.

  1. public static void main(String[] args) throws IOException,InterruptedException {
  2. // this is your script in a string
  3. // 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";
  4. String script = "above R Script here";
  5. List

上面的代码适用于bash shell脚本.我需要为R脚本做些什么特别的事吗?我将对bash脚本和R脚本使用相同的代码.

这是我得到的错误

  1. /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”);然后我看到下面的错误

  1. Cannot run program "/bin/Rscript": error=2,No such file or directory

更新: –

我没有使用上面的脚本,而是决定在r中使用简单的打印地狱脚本来查看是否可以通过Java执行它.

  1. // this will print hello
  2. String script = "#!/usr/bin/env Rscript\nsayHello <- function(){\n print('hello')\n}\n\nsayHello()\n";

当我用commandList.add(“/ bin / bash”);执行此脚本时,我收到此错误

  1. /bin/bash: line 2: Syntax error near unexpected token `('
  2. /bin/bash: line 2: `sayHello <- function(){'

但是,如果我使用此commandList.add(“/ bin / sh”);执行,我收到此错误

  1. /bin/sh: 2: Syntax error: "(" unexpected
最佳答案
您必须直接运行/usr/bin/Rscript.此外,此程序不读取标准输入中的脚本(您必须指定脚本的路径作为Rscript的参数),因此您必须:

>创建临时文件
>编写脚本
>使用Rscript执行脚本
>删除你的临时文件(作为一个很好的编程实践)

例如,这是一个POC:

  1. public static void main(String[] args) throws IOException,InterruptedException {
  2. // Your script
  3. String script = "#!/usr/bin/env Rscript\n" +
  4. "\n" +
  5. "sayHello <- function() {\n" +
  6. " print('hello')\n" +
  7. "}\n" +
  8. "\n" +
  9. "sayHello()\n";
  10. // create a temp file and write your script to it
  11. File tempScript = File.createTempFile("test_r_scripts_","");
  12. try(OutputStream output = new FileOutputStream(tempScript)) {
  13. output.write(script.getBytes());
  14. }
  15. // build the process object and start it
  16. List

作为替代方案,如果您的脚本有第一行“shebang”,您可以执行以下更改:

>将其可执行属性设置为“true”
>使用临时文件的路径作为commandList中的第一个元素(即删除commandList.add(“/usr/bin/Rscript”);)

修改代码部分是:

  1. ...
  2. // create a temp file and write your script to it
  3. File tempScript = File.createTempFile("test_r_scripts_","");
  4. tempScript.setExecutable(true);
  5. try(OutputStream output = new FileOutputStream(tempScript)) {
  6. output.write(script.getBytes());
  7. }
  8. // build the process object and start it
  9. List

猜你在找的Java相关文章