4

I have successfully drawn a single graph using Java, JavaGD and R. I followed this tutorial .

Now, I have an R-script, which reads a CSV file, does some calculations. At the end, it plots 8 different graphs. When I run this script using Java/JavaGD, only 1st and 8th plot are visible. 2nd through 7th are on "inactive" windows, which are blank. I am using the exact same code as in the above mentioned link/tutorial. So I guess something is getting overwritten.

How can I draw them on proper windows? Also, the first window, if re-sized, becomes blank. How to solve this issue?

Please don't hesitate to ask for clarification, if needed. I am not sure how well I have explained the problem.

Any help/reading material is greatly appreciated.

Update 1:

Currently, I am using this code:

public static void main(String[] args) {
    // TODO Auto-generated method stub
    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'='test/MyJavaGD1')");

    re.eval("JavaGD()");
//      re.eval("plot(c(1,5,3,8,5), type='l', col=2)");
//      re.eval("source(\"C:\\Documents and Settings\\username\\My Documents\\Test Data\\BoxPlot.r\");");
        re.eval("source(\"C:\\\\Documents and Settings\\\\username\\\\My Documents\\\\sampleRScript.R\")");
        re.end();
        System.out.println("Done!");
    }

Part of the script:

par(las=2,mfrow=c(2,1))
PlotData <- subset (m4, select=c(LotNo,def,cavity,Lift), subset=(cavity=="1"))
boxplot(Lift ~ def, data=PlotData, main="Number 1")
hist(PlotData$Lift,50, main="", xlab="Lift", ylab="Frequency")
win.graph()
par(las=2,mfrow=c(2,1))
PlotData <- subset (m4, select=c(LotNo,def,cavity,Lift), subset=(cavity=="2"))
boxplot(Lift ~ def, data=PlotData, main="Number 2")
hist(PlotData$Lift,50, main="", xlab="Lift", ylab="Frequency")
win.graph()
par(las=2,mfrow=c(2,1))
PlotData <- subset (m4, select=c(LotNo,def,cavity,Lift), subset=(cavity=="3"))
boxplot(Lift ~ def, data=PlotData, main="Number 3")
hist(PlotData$Lift,50, main="", xlab="Lift", ylab="Frequency")
.
.
.
Bhushan
  • 18,329
  • 31
  • 104
  • 137
  • We see some code in the link for drawing one plot, but you do not show the code you used to plot the 8 plots. Try to boil this down to a small piece of R code which reproduces your problem. That would make it much easier to help us. – Paul Hiemstra Jan 03 '12 at 22:09
  • @PaulHiemstra: I am using the same code as in the link. I have updated the question. Please have a look. – Bhushan Jan 04 '12 at 15:01

1 Answers1

2

You'll need to tell the R instance about your initialized JRI using .jengine(), otherwise it can't issue callbacks , e.g. to resize the window. As for blanked windows you'll need to provide the code that you use.

(You may want to use stats-rosuda-devel to discuss rJava/JRI/JavaGD-related issues there.)

Simon Urbanek
  • 13,842
  • 45
  • 45
  • can you give some more details about .jengine()? any example or link? – Bhushan Jan 04 '12 at 14:47
  • It's a command in rJava - see `?.jengine`. Suggested use would be something like `re.eval("{library(rJava); .jengine()}")`. Can you post your `sampleScript.R` code so we can look at the blanking issue? – Simon Urbanek Jan 04 '12 at 18:03
  • .. that's incomplete - what is `win.graph()`?? – Simon Urbanek Jan 04 '12 at 19:34
  • You can ignore the `...` and consider that as complete. The same set of 4 lines (from `par(` to `hist(`) repeats after each `win.graph();`, total 8 number of times, hence I removed the repetitions. As I mentioned before, it plots 8 graphs. The `win.graph()` is an R function which is used to open another graphics window. It is not written by me, hence I am not sure if I can share the entire script or not. Please let me know if you need any more clarification. – Bhushan Jan 04 '12 at 19:45
  • Well, then ask the author (why it doesn't work). It's not part of R - why don't you just use `dev.new()`?? – Simon Urbanek Jan 04 '12 at 20:04
  • I tried `dev.new()` as well. I followed this question: http://stackoverflow.com/questions/1801064/how-to-separate-two-plots-in-r . Same problem. – Bhushan Jan 04 '12 at 20:06