【问题标题】:Unable to run string related functions using rJava (JRI) in Java无法在 Java 中使用 rJava (JRI) 运行与字符串相关的函数
【发布时间】:2017-03-09 20:46:21
【问题描述】:

我用 Java (eclipse) 编写了一个小程序来使用 JRI (rjava) 运行 R。所有路径都设置好了。问题是虽然我可以运行数字函数(如add),但我不能运行像cat 这样的字符串函数。 (请原谅任何错误;我昨天第一次进行 Java 编码)。

package com.trial.ss;

import org.rosuda.JRI.Rengine;

public class RScriptConnection {

    public Rengine getRScriptEngine() throws Exception {

        Rengine engine = null;
        try {
            engine = Rengine.getMainEngine();
            if (engine == null) engine = new Rengine(new String[] {
                "--vanilla"
            },
            false, null);

            /*  if (!engine.waitForR()) {
                        System.out.println("Unable to load R");
                        return null;
                    } else*/
            System.out.println("Connected to R");
            String rScriptSourceFile = "source('" + RScriptConstant.RS_FILE_LOCATION + "',verbose=TRUE)";
            engine.eval(rScriptSourceFile);
            System.out.println("loading RScript file || completed");

            //return engine;
        } catch(Exception ex) {
            System.out.println("Exeption while connecting to REngine " + ex.getMessage());
            //throw new Exception("Error while creating REngine in RScriptConnection:getRScriptEngine()");
        }
        return engine;
    }

    public static void main(String[] args) {

        String libpath = System.getProperty("java.library.path");
        System.out.println("##############libpath=" + libpath);
        //      System.out.println("Method to be called in RScript=" + "Add(x1 = " + 10 + ", x2 = " + 20 + ", x3 = " + 30 + ", x4 = " + 50 + ")");
        RScriptConnection rScriptConnection = new RScriptConnection();

        try {
            Rengine rEngine = rScriptConnection.getRScriptEngine();
            String Value1 = "\"Advisory\"";
            String Value2 = "\"Assurance\"";
            double svalue = rEngine.eval("(1+2)").asDouble();
            System.out.println("mvalue=" + svalue);
            System.out.println("method to be called in RScript is " + "cat(" + Value1 + "," + Value2 + ")");
            String value = rEngine.eval("cat(" + Value1 + "," + Value2 + ")").asString();
            System.out.println(value);
            rEngine.end();

        } catch(Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

请帮助我理解为什么像cat 这样的字符串函数不起作用。 这是我目前得到的输出:

##############libpath=C:\Users\myname\Documents\R\win-library\3.3\rJava\jri\x64
Connected to R
loading RScript file || completed
mvalue=3.0
method to be called in RScript is cat("Advisory","Assurance")
null

为什么我最后会得到null?我应该得到Advisory Assurance

【问题讨论】:

  • 它不会自动为您将 R 控制台“stdout”连接到 Java“stdout”。您可以通过 rforge.net/org/docs/org/rosuda/JRI/…, boolean, org.rosuda.JRI.RMainLoopCallbacks)> 获得控制台输出的自动回调,但这意味着有一个主循环。使用 capture.output() 包装 cat 调用将使您能够以字符串形式获取结果(在示例代码中分配给 value)。您可以将 , file='/some/dir/and/file.txt' 添加到您的 cat 调用中,然后读取该文件的输出。

标签: java r rjava jri


【解决方案1】:

Bellow 是如何将 R 输出显示为 Java 的示例。你基本上必须实现RMainLoopCallbacks

import org.rosuda.JRI.RMainLoopCallbacks;
import org.rosuda.JRI.Rengine;
import java.util.logging.Logger;

public class Runner {

    private static Logger log = Logger.getLogger("Runner");

    static class LoggingConsole implements RMainLoopCallbacks {
        private Logger log;

        LoggingConsole(Logger log) {
            this.log = log;
        }

        public void rWriteConsole(Rengine re, String text, int oType) {
            log.info(String.format("rWriteConsole: %s", text));
        }

        public void rBusy(Rengine re, int which) {
            log.info(String.format("rBusy: %s", which));
        }

        public void rShowMessage(Rengine re, String message) {
            log.info(String.format("rShowMessage: %s",  message));
        }

        public String rReadConsole(Rengine re, String prompt, int addToHistory) {
            return null;
        }

        public String rChooseFile(Rengine re, int newFile) {
            return null;
        }

        public void rFlushConsole(Rengine re) {
        }

        public void rLoadHistory(Rengine re, String filename) {
        }

        public void rSaveHistory(Rengine re, String filename) {
        }
    }

    Rengine engine = new Rengine(new String[] {"--no-save"}, false, new LoggingConsole(log));
    ...
    // Use the engine somewhere to evaluate a R method and see the output
    engine.eval(rScriptSourceFile);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-06
    • 1970-01-01
    • 2015-11-29
    • 1970-01-01
    • 2021-09-04
    • 1970-01-01
    • 2023-02-24
    • 1970-01-01
    相关资源
    最近更新 更多