Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples of R embedded in Java-Processing #71

Open
jeremydouglass opened this issue May 19, 2017 · 7 comments
Open

Examples of R embedded in Java-Processing #71

jeremydouglass opened this issue May 19, 2017 · 7 comments

Comments

@jeremydouglass
Copy link
Member

jeremydouglass commented May 19, 2017

This came up in a discussion on the Renjin listserv: an example of using renjin inside a Processing(Java) sketch, rather than the other way around (using Renjin to power a Processing mode).

It isn't directly relevant to Processing.R as a mode -- but someday it might actually be interesting to include an example of that in the Processing.R example set. Install the R mode in PDE, switch to a Java sketch, then import renjin from the R mode. Not the core goal of this project, but potentially of interest -- and it expands the options for community engagement!

@gaocegege
Copy link
Member

gaocegege commented May 20, 2017

Yes, I had the idea to make java and R work together. I am not sure in what form it may take, but it is really interesting 😄

We should do some design research about how they integrate before we implement the interesting pattern.

@gaocegege
Copy link
Member

gaocegege commented May 23, 2017

If we could hide renjin from the code, it will be awesome:

void setup() {
  double x = 19;
  $begin
  y <- rpois(100,lambda=3)
  $end
  for (int i = 0; i < y.size(); i++) {
    point(x, y[i])
  }
}

We could define some tags such as $begin and $end, to allow integrate Java and R.

But to be fair, it seems impossible to implement.

@jeremydouglass
Copy link
Member Author

jeremydouglass commented May 23, 2017

Not impossible to implement -- Jupyter notebooks, Beaker, and other notebook-paradigm multi-language systems implement mixing of R into scripting languages like Python or Javascript use an approach that is something like your example above, using "magic commands".

However, I think this would be way, way too much to take on for GSOC.

A more reasonable thing would be to create an example that simply re-implements the basic mjkallen example, only pointing to the Processing.R mode's local jar (which is the same "jar-with-dependencies" as the example). Once that worked, we might choose to develop more examples in the future, or not.

For a future post-GSOC project, another way of embedding R into Java using the Python.R mode might be something like the equivalent of the way that PShaders work in Processing. https://processing.org/tutorials/pshader/ I haven't checked yet, but this may already exist in Renjin -- an Object that wraps a Renjin ScriptEngine up with a particular piece of code, either imported as a string or loaded from an external .R file.

@gaocegege
Copy link
Member

Jupyter doesn't allow the interaction between Java and R, IMO. For example, using variable in Java as a parameter in a function in R code.

And mjkallen's example is more like Java code, not R code. If we support Java in Processing.R, it is a little weird, IMO.

The post-GSoC idea seems cool and feasible, I will have a look on pshader.

@jeremydouglass
Copy link
Member Author

That's right, I was giving Jupyter magicks as an example of mixing R and Python, not R and Java. I believe that Beaker notebooks allow mixing R and Java. Still, those are huge projects.

I'm not suggesting that Processing.R should change. I'm pointing out that installing Processing.R installs renjin. Processing.R could come with a bunch of R mode examples AND a single Java mode "embedded R" example. While in Java mode, that Embedded R example would show how to call the locally installed renjin as per mjkallen's sketch.

@gaocegege
Copy link
Member

That is a good idea 🤔

@jeremydouglass
Copy link
Member Author

jeremydouglass commented Jul 22, 2017

I created a test Processing(Java) sketch that implements this idea, based on a Java example by mjkallen. It works, but it requires that the sketch /code folder contain a renjin jar file:

/code/renjin-script-engine-0.8.2413-jar-with-dependencies.jar

It would be nice if the sketch could simply import the part of renjin that it needs directly from the Processing.R mode! Then installing Processing.R would also simultaneously enable basic R-in-Java mode. However, I think that the modes directory is not in the Java classpath for import. I am not sure if there could be a workaround.

// https://gist.github.com/mjkallen/8299891
import grafica.*;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
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();
}

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;
}

@jeremydouglass jeremydouglass added this to the Someday milestone Aug 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants