IRConnection

Rserve is a de facto standard of how to connect to R. Apart from using a server JRI provides another tool that allows you to connect to java.

Both packages provide a very similar API but are not interchangeable once you use one of both for your projects. IRConnection is ment to provide a wrapper interface so you can interchange both of those.

Alas IRConnection does not facilitate the installation of neither Rserve nor JRI. The one you want to use needs to be installed correctly first and how to acchieve is best read from the manual of this software.

One thing can be acchieved more easily though, and that is the starting/stopping of the Rserve once you need connections. My objective with the following spring-based approach is to reuse a running Rserve (when available) or else to create a running server.

Preconditions

You must have R installed and an Rserve that fits your current system. On my current windows 7 intel 64bit platform I run R x86_64-pc-mingw32/x64 (64-bit) with Rserve Rserve_x64.exe. This configuration works, but on your system you might have another version of R installed and another brand of Rserve must be used. When you start Rserve manually you should face the following output:

C:\Program Files\R\R-2.12.1\bin\x64>R CMD Rserve_x64.exe
Rserve: Ok, ready to answer queries.

Now when you are developing java application you do not want to start Rserve manually again every new day. So you can use a bit of spring configuration to support automatic start, or participation on a running process:

spring configuration (as found in r-service)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<bean name="rConnectionFactory" class="org.rosuda.rengine.REngineConnectionFactory" factory-method="getInstance"/>
	
	<bean name="setup.rStarterFactory" class="org.rosuda.util.r.impl.RStartContext">
		<property name="connectionFactory" ref="rConnectionFactory" />
	</bean>
	
	<bean name="rStarterFactory" class="org.rosuda.util.r.impl.RStarterFactory">
		<property name="context" ref="setup.rStarterFactory" />
	</bean>
	
	<bean name="rStarterProcess" class="org.rosuda.util.process.ProcessService" factory-bean="rStarterFactory" factory-method="createService"/>
</beans>
Now when you need R running in any java class you can use spring annotation in that class like
	@Autowired
	@Qualifier("rStarterProcess")
	private ProcessService service;
This will use the rStarterProcess as defined above. When you need R you use
	service.start();
and to shut down Rserve
	service.stop();