我试图运行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日罐子。 我具有R 3.1.2安装和我已经安装了rJava 0.9-6包。
10行是 “rengine.eval(的String.format(” 问候< - '%s' 的 “ ”你好ř世界“));”。
而第17行是 “hello.helloRWorld();”。
请让我知道,如果你有一个想法,为什么它抛出这些异常.... :)
试试这个:是的作品:
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());
}
}
请注意,你有几个错误,包括不包括一个主类,而不是建立一个合适的构造函数。
有两种不同的方法来连接Java和R.
如果你想使用JRI,您必须使用JVM参数来启动你的java程序-Djava.library.path
在包含JRI库的文件夹指向。
例如:
$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在您的环境:
- R_HOME:指向您的本地研发安装(EJ:/Library/Frameworks/R.framework/Resources)
- LD_LIBRARY_PATH:指向至R lib目录以及JRI目录(EJ:$ LD_LIBRARY_PATH:/应用/供应商/ R / LIB / R / lib中:/应用/供应商/ R / LIB / R / bin)中
下面是一个例子:
@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。
例如:
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 CONF文件和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.
确保当你开始Rserve指定“--slave”参数。
如果你想看到更多的例子,我有使用这两种方法,JRI和RServe,在我github上演示项目:
https://github.com/jfcorugedo/RJavaServer
你应该使用下面的命令导出LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/lib/jvm/jre/lib/amd64:/usr/lib/jvm/jre/lib/amd64/default
而从R内执行
install.packages("rJava")
这肯定会使你的REngine从Java applicaition成功的作品