加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 01:03:41 所属栏目:Java 来源:网络整理
导读:我在String变量中有一个Rscript,我想从Java程序执行它并将一些变量传递给它.如果我独立执行该R脚本,它可以正常工作.我通过使用Python程序将所有R脚本转换为一行,如下所示: import jsonjsonstr = json.dumps({"script": """#!/usr/bin/Rscript# read the da

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

import json

jsonstr = json.dumps({"script": """
#!/usr/bin/Rscript

# read the data file
library('jsonlite')
library('rpart')

args <- as.list(Sys.getenv(c(
                        "path","client_users")))

if (args[["path"]]==""){
    args[["path"]] <- "."
}

# other stuff here
# other stuff here

"""})

print jsonstr

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

public static void main(String[] args) throws IOException,InterruptedException {

    // this is your script in a string
    // String script = "#!/bin/bashnnecho "Hello World"nn readonly PARAM1=$param1n echo $PARAM1nnreadonly PARAM2=$param2n echo $PARAM2nn";
    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 RscriptnsayHello <- function(){n   print('hello')n}nnsayHello()n";

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

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

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

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

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

例如,这是一个POC:

public static void main(String[] args) throws IOException,InterruptedException {

    // Your script
    String script = "#!/usr/bin/env Rscriptn" +
            "n" +
            "sayHello <- function() {n" +
            "    print('hello')n" +
            "}n" +
            "n" +
            "sayHello()n";

    // create a temp file and write your script to it
    File tempScript = File.createTempFile("test_r_scripts_","");
    try(OutputStream output = new FileOutputStream(tempScript)) {
        output.write(script.getBytes());
    }

    // build the process object and start it
    List

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

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

要修改的代码部分是:

...

// create a temp file and write your script to it
File tempScript = File.createTempFile("test_r_scripts_","");
tempScript.setExecutable(true);
try(OutputStream output = new FileOutputStream(tempScript)) {
    output.write(script.getBytes());
}

// build the process object and start it
List

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读