Run R from Java I want to run rJava

Introduction

About half a year ago, a newcomer said, "I want to run R from Java," and I wrote a memo when I responded. It was okay to kick the process, but there was a module called ** JRI ** called ** Java / R Interface **, so I decided to deal with it.

What is rJava?

rJava is a simple R-to-Java interface.
It is comparable to the .C/.Call C interface.
rJava provides a low-level bridge between R and Java (via JNI).
It allows to create objects, call methods and access fields of Java objects from R.

http://www.rforge.net/rJava/index.html

In short, it's an interface that calls R via JNI. Since JNI is used, it was a little troublesome to set the dll file.

Get JRI.jar

I will drop JRI.jar.

Start the R GUI and execute the installation command.

install.packages('rJava')

This will download the JAR and DLL under C: \ Program Files \ R \ R-3.3.1 \ library \ rJava \. It seems that JAR version control suddenly becomes chaotic. Only JRI.jar seems to be able to manage the repository.

Click here to use Gradle.

// https://mvnrepository.com/artifact/org.nuiton.thirdparty/JRI
compile group: 'org.nuiton.thirdparty', name: 'JRI', version: '0.9-6'

Click here to use Maven.

<dependency>
	<groupId>org.nuiton.thirdparty</groupId>
	<artifactId>JRI</artifactId>
	<version>0.9-6</version>
</dependency>

… But I absolutely need the dll file included in rJava. It seems that it is necessary to take individual measures such as dropping it from the original site without hitting the command. http://www.rforge.net/rJava/files/

dll settings

Get jri.dll and set it. Either of C: \ Program Files \ R \ R-3.3.1 \ library \ rJava \ jri.

64bit is x64, 32bit is ʻi386`.

This time, I deployed it in the folder directly under the execution. If you want to deploy it in a different folder, you need to put it in your PATH.

How to run in a standalone app

You have to do it through your PATH.

How to run Windows environment


set PATH=%PATH%;C:/Program Files/R/R-3.3.1/bin;C:/Program Files/R/R-3.3.1/bin/i386;C:\Program Files\R\R-3.3.1\library\rJava\jri\i386

java ^
-classpath "./classes;C:/Program Files/R/R-3.3.1/library/rJava/jri/JRI.jar" ^
example.MyTest

It worked without the settings below.

Unnecessary settings?


set R_DOC_DIR="C:/Program Files/R/R-3.3.1/doc"
set R_INCLUDE_DIR="C:/Program Files/R/R-3.3.1/include"
set R_SHARE_DIR="C:/Program Files/R/R-3.3.1/share"

Execution method when running with a server application

Add JRI.jar to WEB-INF / lib (not necessary if managed by Maven or Gradle).

You also need to set the path for jri.dll. Green Run Button> Run Configuration> Arguments> VM Arguments

Runtime arguments


-Djava.library.path="C:\Program Files\R\R-3.3.1\library\rJava\jri\i386"

Add

Common errors

Forget to pass the jri.dll path

I often get this error.

Cannot find JRI native library!
Please make sure that the JRI native library is in a directory listed in java.library.path.

java.lang.UnsatisfiedLinkError: no jri in java.library.path
	at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1864)
	at java.lang.Runtime.loadLibrary0(Runtime.java:870)
	at java.lang.System.loadLibrary(System.java:1122)
	at org.rosuda.JRI.Rengine.<clinit>(Rengine.java:19)
	at example.MyTest(MyTest.java:11)

Don't forget to set the path of jri.dll.

Get angry if you don't put variables in R's path

If you break through the above, the following will appear.

Cannot find JRI native library!
Please make sure that the JRI native library is in a directory listed in java.library.path.

java.lang.UnsatisfiedLinkError: C:\dll\jri.dll: Can't find dependent libraries
	at java.lang.ClassLoader$NativeLibrary.load(Native Method)
	at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1938)
	at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1854)
	at java.lang.Runtime.loadLibrary0(Runtime.java:870)
	at java.lang.System.loadLibrary(System.java:1122)
	at org.rosuda.JRI.Rengine.<clinit>(Rengine.java:19)
	at example.MyTest(MyTest.java:11)

Even if it is set, I get angry when the dll dependency is wrong. jri.dll depends on bin / i386 or bin / x86 dll, so don't forget to set it as well.

Summary

Recommended Posts

Run R from Java I want to run rJava
I want to write quickly from java to sqlite
[Java] I want to calculate the difference from the date
I want to stop Java updates altogether
Changes from Java 8 to Java 11
Sum from Java_1 to 100
I want to send an email in Java.
I want to graduate from npm install properly [2020]
I want to use java8 forEach with index
rsync4j --I want to touch rsync in Java.
From Java to Ruby !!
I want to play with Firestore from Rails
I translated the grammar of R and Java [Updated from time to time]
I want to do something like "cls" in Java
I want to use ES2015 in Java too! → (´ ・ ω ・ `)
6 features I missed after returning to Java from Scala
I want to transition screens with kotlin and java!
I want to get along with Map [Java beginner]
I want to redirect sound from Ubuntu with xrdp
I want to connect to Heroku MySQL from a client
Migration from Cobol to JAVA
I want to convert characters ...
New features from Java7 to Java8
Connect from Java to PostgreSQL
From Ineffective Java to Effective Java
[Java] I want to convert a byte array to a hexadecimal number
[JDBC] I tried to access the SQLite3 database from Java.
I want to build Java Applet without using an IDE
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
Run R from a tomcat-powered Java process on Amazon Linux
I want to use the Java 8 DateTime API slowly (now)
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I want to implement various functions with kotlin and java!
[Java] I want to test standard input & standard output with JUnit
I want to simplify the conditional if-else statement in Java
In Java, I want to trim multiple specified characters from only the beginning and end.
I want to return a type different from the input element with Java8 StreamAPI reduce ()
Swift: I want to chain arrays
I want to use FormObject well
Run node.js from android java (processing)
Run a batch file from Java
I want to convert InputStream to String
I tried to interact with Java
I want to docker-compose up Next.js!
Java to be involved from today
From Java to VB.NET-Writing Contrast Memo-
Java, interface to start from beginner
I tried to summarize Java learning (1)
The road from JavaScript to Java
I tried to summarize Java 8 now
[Java] Conversion from array to List
I just want to write Java using Eclipse on my Mac
I want to return to the previous screen with kotlin and java!
If you want to change the Java development environment from Eclipse
[Java] I want to perform distinct with the key in the object
[Android] I want to get the listener from the button in ListView
What I thought about when I started migrating from Java to Kotlin
I want to develop a web application!
I want to write a nice build.gradle
I want to eliminate duplicate error messages