Reputation: 18747
I am trying to plot some graphs using Java and R (JRI and Rengine). When I write something like this in my code it works fine:
re.eval("plot(c(1,5,3,8,5), type='l', col=2)");
But if I write this:
re.eval("source(\"C:\\Documents and Settings\\abc\\My Documents\\Test Data\\BoxPlot.r\");");
the output window flashes for a moment and vanishes. This specified file contains only 1 command, as the first one, i.e. re.eval("plot(c(1,5,3,8,5), type='l', col=2)");
I am very new to R, so this may be a very basic question. But I am not able to find it out. Can anyone help me out?
Thanks.
EDIT 1
I am using Windows XP.
Here is the full code:
import org.rosuda.JRI.Rengine;
public class JavaGDExample1 {
public static void main(String[] args) {
Rengine re;
String[] dummyArgs = new String[1];
dummyArgs[0] = "--vanilla";
re = new Rengine(dummyArgs, false, null);
re.eval("library(JavaGD)");
// This is the critical line: Here, we tell R that the JavaGD() device that
// it is supposed to draw to is implemented in the class MyJavaGD. If it were
// in a package (say, my.package), this should be set to
// my/package/MyJavaGD1.
re.eval("Sys.putenv('JAVAGD_CLASS_NAME'='MyJavaGD1')");
re.eval("JavaGD()");
// re.eval("plot(c(1,5,3,8,5), type='l', col=2)");
re.eval("source(\"C:\\Documents and Settings\\abc\\My Documents\\Test Data\\BoxPlot.r\");");
// re.eval("source(\"C:\\Documents and Settings\\abc\\My Documents\\Test Data\\testPlot.r\")");
re.end();
}
}
import javax.swing.JFrame;
import org.rosuda.javaGD.GDCanvas;
import org.rosuda.javaGD.GDInterface;
/**
* This is a minimal reimplementation of the GDInterface. When the device is opened,
* it just creates a new JFrame, adds a new GDCanvas to it (R will plot to this GDCanvas)
* and tells the program to exit when it is closed.
*/
public class MyJavaGD1 extends GDInterface {
public JFrame f;
public void gdOpen(double w, double h) {
f = new JFrame("JavaGD");
c = new GDCanvas(w, h);
f.add((GDCanvas) c);
f.pack();
f.setVisible(true);
f.setTitle("Naked R plot");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Upvotes: 3
Views: 1624
Reputation: 31
re.eval("Sys.putenv('JAVAGD_CLASS_NAME'='MyJavaGD1')");
should be now
re.eval("Sys.Sys.setenv('JAVAGD_CLASS_NAME'='MyJavaGD1')");
Sys.setenv() is a new (and preferred) synonym for Sys.putenv() and is now deprecated in favour of Sys.setenv().
Upvotes: 0
Reputation: 13932
You path is wrong - you're entering the quoting hell, what you sent to R is
source("C:\Documents and Settings\abc\My Documents\Test Data\BoxPlot.r");
which is invalid:
> source("C:\Documents and Settings\abc\My Documents\Test Data\BoxPlot.r");
Error: '\D' is an unrecognized escape in character string starting "C:\D"
To avoid the quoting hell, it is safer to assign file names:
re.assign("fn", "c:\\foo\\bar");
re.eval("source(fn)");
or use /
instead of \\
PS: use stats-rosuda-devel for rJava/JRI questions
Upvotes: 3