/** * Stats.java * * --Copyright notice-- * * Author: Andy Evans. * http://www.geog.leeds.ac.uk/people/a.evans/ * This code relies on the GPL2-protected library rJava * http://www.rforge.net/rJava/ * This code also relies on the GPL2-protected software R * http://www.r-project.org/ * This code is also licensed under GPL2 which can be found at * the Open Source Initiative website at... * http://cran.r-project.org/web/licenses/GPL-2 * * --End of Copyright notice-- * **/ import java.awt.*; import java.awt.event.*; import org.rosuda.JRI.REXP; import org.rosuda.JRI.Rengine; /** * This class shows how to connect to R and send commands without callback using rJava. * The associated practical and data files can be found at * http://www.geog.leeds.ac.uk/courses/postgrad/geog5790/lecture7/practical/index.html * @author Andy Evans * @version 1.0 **/ public class Stats { /** * Opens a link to a datafile, reads in some data, plots it, generates a linear regression. * Note that this class is purposely broken - see practical. **/ public Stats(String[] args) { // Rengine run like this opens up a GUI-less version of R. // For options to open a java-based GUI and interact with it, see the // Examples that come with rJava in rJava\jri\examples\ Rengine re=new Rengine(args, false, null); // Recommended, though not needed as such. if (!Rengine.versionCheck()) {System.exit(0);} // This just reports whether R was running and we connected to it, or whether we started it. if (re.isStandAlone()) System.out.println("R initialised by java"); // We're going to use R to read the file but we'll get all the data back out from it // into java, including the predicted points from our linear regression. // Original data. double[] x1 = null; double[] y1 = null; // Prediction data. double[] x2 = null; double[] y2 = null; try { // The easiest way to deal with R is to use the Rengine eval method. This takes in standard R commands as Strings. // Note in the file string that the original text, which needed escaping, is now within a larger String, meaning all the // escape characters need escaping themselves. Easier to use forward-slashes, but I've used back-slashes here to show the point. REXP a = re.eval("data1 <- read.csv(\"M:\\\\yourDirectory\\\\data.tab\", header = TRUE)"); // The eval method returns a very useful object of REXP class. This can be converted to // various things to extract data. Here were just convert it to a String, more than anything so you can see it working. System.out.println("1: " + a.toString()); a = re.eval("attach(data1)"); // The problem with toString is that it can give you a whole complicated object. // The nice thing about the following command is that, while it doesn't have any output in R, in // rJava it sets up the REXP with a copy of the data asked for. a = re.eval("data1$Age"); // We can then use the following method to get the data as a double array. x1 = a.asDoubleArray(); for (int i = 0; i < x1.length; i++) System.out.println(x1[i]); a = re.eval("data1$Desperation"); y1 = a.asDoubleArray(); for (int i = 0; i < y1.length; i++) System.out.println(y1[i]); // That's got our two original data sets out. Now we plot that data. // Note that this opens up the R plot window. This will hang until the // application is terminated, which you can do on the Eclipse output window by // pushing the square red icon. This causes some issues, as we'll see. a = re.eval("plot(Age, Desperation, main=\"Age vs. Desperation\")"); // Do the regression. a = re.eval("lineeq <- lm(Desperation ~ Age, data=data1)"); a = re.eval("x <- seq(min(Age), max(Age), by=10.0)"); // Get the novel x-axis data. x2 = a.asDoubleArray(); for (int i = 0; i < x2.length; i++) System.out.println(x2[i]); a = re.eval("newData <- data.frame(Age = x)"); a = re.eval("predictions <- predict(lineeq, newdata = newData)"); // Get the predicted y-axis data. y2 = a.asDoubleArray(); for (int i = 0; i < y2.length; i++) System.out.println(y2[i]); // Add to the plot. Or so we'd think. a = re.eval("lines(Age, predictions)"); a = re.eval("title(main=\"Autos\", col.main=\"red\", font.main=4)"); // Clean up. a = re.eval("detach(data1)"); a = re.eval("rm (data1, lineeq, newData, predictions, x)"); } catch(Exception e) { System.out.println(e.getMessage()); } } /** * Runs the whole shebang. **/ public static void main(String[] args) { new Stats(args); } }