Including a jar resource from another mode?

The scenario:

  1. Processing.R mode is installed in PDE.
  2. The R mode includes a resource ( renjin-script-engine.jar / renjin-script-engine-0.8.2413-jar-with-dependencies.jar)
  3. This could be used to run R in a Java mode sketch.

I want to write an example sketch in Java mode demonstrating how to import resources from Processing.R mode and use them to integrate an R process into a Java mode sketch. The Java sketch would include a line like this:

import org.renjin.sexp.*

However this does not work because the jars of Processing.R mode are not in the Java mode path.

Is there a workaround?

Partial solutions I can think of:

  1. include renjin-script-engine.jar again in the RinJava.pde example sketch path. However this needs to be copied to every new sketch -- and the user already had this jar installed in their system in the Processing.R mode!
  2. write a separate library for PDE Contributions Manager called "RinJava" that includes renjin-script-engine.jar, and have users install the library and import it to a Java sketch. This is better, but it still requires two installs of the same jar, and it doesn't allow the Java crossover example to be distributed directly with the R mode.
  3. use dark magic to place renjin-script-engine.jar directly in the existing Java mode, perhaps during Processing.R install. This seems like a bad idea, because (among other potential problems) any update to Processing(Java mode) could remove the change.

Ideally there would be a way to access Processing.R mode jars from a Java mode sketch -- then if the R mode is installed, a Java sketch could also use it.

Any suggestions?

Answers

  • edited September 2017

    In case you are curious, here is the example Java sketch that would be importing from Processing.R -- but the details of the sketch don't matter, only getting the import statement to work.

    // github.com/gaocegege/Processing.R/issues/71
    // gist.github.com/mjkallen/8299891
    
    import grafica.*;
    import javax.script.ScriptEngine;
    import javax.script.ScriptEngineManager;
    
    // import Processing.R mode resource into Java mode sketch
    import org.renjin.sexp.*;
    
    void setup() {
      size(500, 350);
      background(150);
      // Prepare the points for the plot
      int nPoints = 100;
    
      GPointsArray points = new GPointsArray(nPoints);
      try {
        points = Random(nPoints);
      } catch (Exception e) {
        e.printStackTrace();
      }
    
      // Create a new plot and set its position on the screen
      GPlot plot = new GPlot(this);
      plot.setPos(25, 25);
    
      // Set the plot title and the axis labels
      plot.setPoints(points);
      plot.getXAxis().setAxisLabelText("x axis");
      plot.getYAxis().setAxisLabelText("y axis");
      plot.setTitleText("A very simple example");
    
      // Draw it!
      plot.defaultDraw();
    }
    
    
    // use R to generate random values from the uniform distribution
    
    GPointsArray Random(int nPoints) throws Exception {
       GPointsArray points = new GPointsArray(nPoints);
       // create a script engine manager
       ScriptEngineManager factory = new ScriptEngineManager();
       // create a Renjin engine
       ScriptEngine engine = factory.getEngineByName("Renjin");
       // evaluate R code from String
       DoubleVector res = (DoubleVector)engine.eval("runif(100)");
       for(int i = 0; i < nPoints; i++){
         points.add(i, (float)(10*res.getElementAsDouble(i)));
       }
       return points;
     }
    
  • So far it looks like the answer is "this just isn't possible" ...

    ...unless Processing's core Java mode enabled it as a feature. That doesn't seem to make a lot of sense: it would create a lot of headaches and complexity for an extremely narrow use case.

    It looks like the best solution would be a separate Processing(Java) library e.g. "RInJava" to compliment the Processing.R mode.

    Drawbacks: because that library would use a separate duplicate jar from the mode, they would take up twice the space, and the library and the mode could get out of sync on the features supported. But otherwise a library should work.

Sign In or Register to comment.