Saturday, May 14, 2011

Handling plots with rcaller

Using RCaller is a simple way of calling R scripts from Java. Unfortunately image handling routines was not implemented so user must handle this stuff himself. In R the result of a plot object can be saved in a simple way like this:











#Data generating process
x<-rnorm(100, 0, 2)
#we generated a normal sample with mean 0 and standard deviation 2

png("path/to/file.png")
plot.ts(x)
dev.off()

After running this short script, no screen output is produced but path/to/file.png is created as a png image. After calling this script from Java, produced image can be loaded like this:

ImageIcon myIcon = new ImageIcon("/path/to/file.png");
 
This image can be easly drawn on a swing container using

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    myIcon.paintIcon(this, g, 0, 0);
}
 

9 comments:

  1. Great library!

    I've noticed that dev.off() will RCaller will produce parse error:


    Parse error at line 1, column 6. Encountered: device
    at bsh.Parser.generateParseException(Unknown Source)
    at bsh.Parser.jj_consume_token(Unknown Source)
    at bsh.Parser.Statement(Unknown Source)
    at bsh.Parser.BlockStatement(Unknown Source)
    at bsh.Parser.Line(Unknown Source)
    at bsh.Interpreter.Line(Unknown Source)
    at bsh.Interpreter.eval(Unknown Source)
    at bsh.Interpreter.eval(Unknown Source)
    at bsh.Interpreter.eval(Unknown Source)
    at rcaller.StreamReader.run(RCaller.java:79)


    My solution was to just leave it out, but this could be a problem in some situation.

    Is the error message from dev.off() common?

    ReplyDelete
  2. this method of plotting is deprecated in version 2.0.

    Please use the internal plotting functionality of RCaller 2.0 instead this way.

    ReplyDelete
  3. i want to call rscript from java but not able to load...

    RCaller caller=new RCaller();
    caller.setRScriptExecutableFile("C:\\Program Files\\R1.4...\\R\\Rscript\\Rscript.exe");

    "setRScriptExecutableFile" itself gives the error...
    can anyone help in this ??

    ReplyDelete
  4. which version of RCaller are you using and what is the error throwed?

    ReplyDelete
  5. using rcaller: 2.0
    code is workign on eclipse 9.0
    yestarday was not workign on eclipse 3.6 or i might be doing somethign wrong,code i am usin is:

    RCaller caller = new RCaller();
    //caller.setRscriptExecutable("/usr/bin/Rscript");
    caller.setRscriptExecutable("C:\\local\\R\\R-2.13.1\\bin\\Rscript.exe");
    caller.cleanRCode();
    double[] numbers = new double[] {1,4,3,5,6,10};

    // Graph
    caller.addDoubleArray("x", numbers);
    File file = caller.startPlot();
    System.out.println(file);
    caller.addRCode("plot.ts(x)");
    caller.endPlot();
    caller.runOnly();
    caller.showPlot(file);

    ReplyDelete
  6. will run on eclipse 3.6 in eve again once i am at home.. will post the error..

    ReplyDelete
  7. Hi can you elaborate with example code... I am facing the same problem

    My code snip goes like this:

    code.addRCode("png(filename = \"c:/h31.png\",width = 700, height = 700, units = \"px\", pointsize = 12,bg = \"white\", res = NA, family = \"\", restoreConsole = TRUE,type = c(\"windows\", \"cairo\", \"cairo-png\"))");
    code.addRCode("prot_heatmap <- heatmap(prot_matrix, scale=\"column\", margins=c(3,3))");
    code.addRCode("dev.off()");

    This returns an empty string

    ReplyDelete
  8. Hi, I am generating a plot using RCaller. I am clicking on a button from class A_JFrame. This button call B_JCaller class and open plot in a new window. when I close plot window, parent window also getting close. Parent window should not be close. If any solution, kindly share.

    ReplyDelete

Thanks