2

This is basically the inverse to this question: How to plot a graph using R, Java and JRI? . I want to create a plot in R from a Java program and store it on the harddrive. This is not about displaying an R plot in a java window. I'm using JRE as part of the rJava package. Performing calculations in R from java works just fine.

Executing this in R produces a nice plot:

pdf(file="qqplot.pdf")
x <- rnorm(100)
qqnorm(x); qqline(x)
dev.off()

Nevertheless, executing the same from Java produces the same file, but it is empty. Here's the java code:

private String createNormQQPlot(double[] samples, File filename){

    try{
        // Pass array to R
        engine.assign("samples", samples);
        engine.eval(String.format("pdf(file='%s')",filename.getPath()));
        engine.eval("qqnorm(samples)");
        engine.eval("qqline(samples)");
        engine.eval("dev.off()");
    }catch(Exception e) {
        e.printStackTrace();
        return "";
    }
    return filename.getPath();
}

Any ideas on this are highly appreciated!

Community
  • 1
  • 1
Chris
  • 721
  • 1
  • 10
  • 23
  • Can you please show us the Java code? – Yehoshaphat Schellekens Aug 24 '14 at 13:38
  • @YehoshaphatSchellekens: Updated my question with the code! Thanks. – Chris Aug 25 '14 at 06:49
  • Your concept should work, try to find out if Java passes correctly "samples" data to R. a similar thing worked fine in here: http://stackoverflow.com/questions/24493113/exporting-plotted-variable-shows-blank-image/24528683#24528683 , i think you might have some problems with the direction of the "\" as it is opposite in R – Yehoshaphat Schellekens Aug 25 '14 at 07:36
  • Seems to work for png/jpg, but not for the pdf/postscript device. Probably a system issue: I'm running Ubuntu 14.04 / R 3.0.2. – Chris Oct 13 '14 at 21:33

1 Answers1

1

If you are using some variant of Linux (for example Ubuntu) the solution is to use cairo_pdf instead of pdf:

cairo_pdf(file="qqplot.pdf")
x <- rnorm(100)
qqnorm(x); qqline(x)
dev.off()

There are drawbacks to this approach documented here: R: Cairographics-based SVG, PDF and PostScript Graphics Devices, but at least non-empty pdf files are produced.

mitakas
  • 11
  • 2