我正在尝试运行JRI的示例并且不成功,这里是示例的链接。 http://blog.comsysto.com/2013/07/10/java-r-integration-with-jri-for-on-demand-predictions/
import org.rosuda.JRI.Rengine;
import org.rosuda.JRI.REXP;
public class HelloRWorld {
Rengine rengine; // initialized in constructor or autowired
public void helloRWorld() {
rengine.eval(String.format("greeting <- '%s'", "Hello R World"));
REXP result = rengine.eval("greeting");
System.out.println("Greeting from R: "+result.asString());
}
}
这是控制台给我的错误。
Exception in thread "main" java.lang.NullPointerException
at org.roulette.games.HelloRWorld.helloRWorld(HelloRWorld.java:10)
at org.roulette.games.HelloRWorld.main(HelloRWorld.java:17)
据我所知,我正确地将外部JRI 2014-10-19 jar附加到项目中。 我安装了R 3.1.2并安装了rJava 0.9-6软件包。
第10行是&#34; rengine.eval(String.format(&#34;问候&lt; - &#39;%s&#39;&#34;,&#34; Hello R World&#34; ));&#34;
第17行是&#34; hello.helloRWorld();&#34;。
如果你知道它为什么会抛出这些例外,请告诉我....:)
答案 0 :(得分:1)
试试这个:是有效的:
package stackoverflow;
import org.rosuda.JRI.REXP;
import org.rosuda.JRI.Rengine;
/**
*
* @author yschellekens
*/
public class StackOverflow {
public static void main(String[] args) throws Exception {
String[] Rargs = {"--vanilla"};
Rengine rengine = new Rengine( Rargs, false, null);
rengine.eval("greeting <- '%Hello R World'");
REXP result = rengine.eval("greeting");
System.out.println("Greeting from R: "+result.asString());
}
}
请注意,您有多个错误,包括不包括主类,而没有创建正确的构造函数。
答案 1 :(得分:1)
连接Java和R有两种不同的方法。
如果要使用 JRI ,则必须使用指向包含JRI库的文件夹的JVM参数-Djava.library.path
启动java程序。
例如:
$JAVA_HOME/bin/java -Djava.library.path=/app/vendor/R/lib/R/library/rJava/jri/ -jar target/myapp.jar
如果您在查找JRI安装目录时遇到问题,请尝试查找JRI SO库:
find / -name "libjri.*"
此外,请确保在您的环境中创建了R_HOME和LD_LIBRARY_PATH:
以下是一个例子:
@Test
public void testMeanFunction() {
// just making sure we have the right version of everything
if (!Rengine.versionCheck()) {
System.err.println("** Version mismatch - Java files don't match library version.");
fail(String.format("Invalid versions. Rengine must have the same version of native library. Rengine version: %d. RNI library version: %d", Rengine.getVersion(), Rengine.rniGetVersion()));
}
// Enables debug traces
Rengine.DEBUG = 1;
System.out.println("Creating Rengine (with arguments)");
// 1) we pass the arguments from the command line
// 2) we won't use the main loop at first, we'll start it later
// (that's the "false" as second argument)
// 3) no callback class will be used
engine = REngine.engineForClass("org.rosuda.REngine.JRI.JRIEngine", new String[] { "--no-save" }, null, false);
System.out.println("Rengine created...");
engine.parseAndEval("rVector=c(1,2,3,4,5)");
REXP result = engine.parseAndEval("meanVal=mean(rVector)");
// generic vectors are RVector to accomodate names
assertThat(result.asDouble()).isEqualTo(3.0);
}
另一方面,如果您想使用 Rserve ,则需要在单独进程中启动Rserve,然后从Java进程创建RConnection。 / p>
例如:
if(LOGGER.isInfoEnabled()) {
LOGGER.info("Starting RServe process...");
}
ProcessBuilder builder = new ProcessBuilder("/bin/sh", "-c", String.format("echo 'library(Rserve);Rserve(FALSE,args=\"--no-save --slave --RS-conf %s\")'|%s --no-save --slave", rserveConf, rexe));
builder.inheritIO();
Process rProcess = builder.start();
if(LOGGER.isInfoEnabled()) {
LOGGER.info("Waiting for Rserve to start...");
}
int execCodeResult = rProcess.waitFor();
if(execCodeResult != SUCCESS_CODE) {
LOGGER.error(String.format("Unexpected error code starting RServe: %d", execCodeResult));
} else {
LOGGER.error("RServe started successfully");
}
if(LOGGER.isInfoEnabled()) {
LOGGER.info("Opening connection to RServe daemon....");
}
REngine engine = new RConnection();
if(LOGGER.isInfoEnabled()) {
LOGGER.info(String.format("Obtaining R server version: %d", ((RConnection)engine).getServerVersion()));
}
//Perform some engine.parseAndEval("....");
rserveConf 是Rserv配置文件的路径, rexe 是R可执行文件的完整路径。
例如,在我的MacOS计算机中,我可以启动Rserve执行此行:
/bin/sh -c "echo 'library(Rserve);Rserve(FALSE,args=\"--slave --RS-conf /Users/me/Documents/test/rserve.conf\")'|/Library/Frameworks/R.framework/Versions/3.2/Resources/bin/exec/R --no-save --slave"
此命令输出如下内容:
Starting Rserve:
/Library/Frameworks/R.framework/Resources/bin/R CMD /Library/Frameworks/R.framework/Versions/3.2/Resources/library/Rserve/libs//Rserve --slave
Rserv started in daemon mode.
确保指定&#34; - 奴隶&#34;启动Rserve时的参数。
如果你想看到更多的例子,我有一个演示项目,在我的github中使用JRI和RServe两种方法:
答案 2 :(得分:0)
您应该使用以下命令
导出LD_LIBRARY_PATHexport LD_LIBRARY_PATH=/usr/lib/jvm/jre/lib/amd64:/usr/lib/jvm/jre/lib/amd64/default
从R执行
install.packages("rJava")
这肯定会使您的REngine从您的Java应用程序中成功运行