Search code examples
javapackageioexceptionrenjin

Renjin IOException while loading package Rweka, could not find function '.jfield'


I´m trying to execute a R script inside a Java program, all dependencies are installed with Maven but the problem still there.

In my research to fix the problem I found that jfield is a function provided by rJava, but I am not sure if rJava (Renjin package) implemented it.

¿Is there a way to check this? ¿Or I am missing something?

My program uses this libraries, all supported by Renjin:

  • rJava
  • RWeka
  • RWekajars

And I´m using betadriven public repo trough Maven to install them. Here it´s the main class:

package org.dfont.renjin;

import org.renjin.script.RenjinScriptEngineFactory;
import javax.script.ScriptEngine;


public class TryRenjin {
    public static void main(String[] args) throws Exception {
        // create a script engine manager:
        RenjinScriptEngineFactory factory = new RenjinScriptEngineFactory();
        // create a Renjin engine:
        ScriptEngine engine = factory.getScriptEngine();

        // ... put your Java code here ...
        if(engine == null) {
            throw new RuntimeException("Renjin Script Engine not found on the classpath.");
        }

        else{
            engine.eval(new java.io.FileReader("/home/dfont/renjinTest/pruebaCalculo.R"));

        }
    }
}

And here my pom.xml,

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.dfont.renjin</groupId>
    <artifactId>renjin</artifactId>
    <version>0.1</version>
    <dependencies>
        <dependency>
            <groupId>org.renjin</groupId>
            <artifactId>renjin-script-engine</artifactId>
            <version>RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.renjin.cran</groupId>
            <artifactId>rJava</artifactId>
            <version>0.9-8-renjin-5</version>
        </dependency>
        <dependency>
            <groupId>org.renjin.cran</groupId>
            <artifactId>RWekajars</artifactId>
            <version>3.9.1-3-b3</version>
        </dependency>
        <dependency>
            <groupId>org.renjin.cran</groupId>
            <artifactId>RWeka</artifactId>
            <version>0.4-34-b5</version>
        </dependency>


    </dependencies>
    <repositories>
        <repository>
            <id>bedatadriven</id>
            <name>bedatadriven public repo</name>
            <url>https://nexus.bedatadriven.com/content/groups/public/</url>
        </repository>
    </repositories>
    <build>
        <plugins>
            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>org.dfont.renjin.TryRenjin</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

The first line output when I run the program:

Exception in thread "main" org.renjin.eval.EvalException: IOException while loading package org.renjin.cran:RWeka: could not find function '.jfield'

Thanks in advance!


Solution

  • Renjin doesn't yet have full support for rJava packages. One of things that seems to be missing is a wrapper for .jfield. You can find the compatability layer here:

    https://github.com/bedatadriven/renjin-rjava/blob/master/src/main/R/rJava.R

    Pull requests welcome!