Wednesday, May 30, 2012

An informative video on RCaller

Somebody on the Internet submitted an informative video on how to use RCaller for calling R from withing Java applications in YouTube.

It is nice to see RCaller has an higher usage rates after its 2.0 version.

You can see the embedded video in this entry. Have a nice training!

4 comments:

  1. Maybe this is obvious, but is there a way to mix remote and interactive R calling, e.g. use the R console to source/format data and Rcaller to send plotting commands to the same console ?
    Thanks.
    Jerome

    ReplyDelete
  2. Hi guys,
    Rcaller is easy to use, but my problem is how do i save a heatmap generated with R in my local machine.

    When i tried the following it returns nothing but an empty image :(
    <<---------------------------------------------------------------->>
    code.addRCode("postscript(filename = \"c:/h33.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()");
    ----------------------------------------------------------------------------
    Please help if you know the work around

    ReplyDelete
    Replies
    1. Hi Deepak! I have the same problem - if I get a solution I'll let you know!!!

      Delete
    2. Hi again Deepak!

      Here's some code that works - it's a JPanel, with button that launches the heatmap-generating code. It loads data from a .csv file and builds a heatmap from it. The .csv file is a "counts" file: there are 20 species listed in the first row, and in the second row was the number of animals of that species. Let me know if you can get it to work:

      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      import javax.swing.event.*;
      import java.io.*;
      import rcaller.*;

      public class HeatMap extends JPanel implements ActionListener
      {
      public JButton button;

      public HeatMap()
      {
      button = new JButton("HeatMap");
      button.addActionListener(this);
      add(button);
      }

      public void actionPerformed(ActionEvent e)
      {
      try
      {
      RCaller caller = new RCaller();
      caller.setRscriptExecutable("C:/Program Files/R/R-2.14.1/bin/Rscript.exe");

      RCode code = new RCode();
      code.clear();

      File file = code.startPlot();

      System.out.println("Plot will be saved to : " + file);



      code.addRCode("require(gplots);");
      code.addRCode("require(RColorBrewer);");
      code.addRCode("setwd(\'c:/stats_data/\');");
      code.addRCode("md <- read.csv(\'your_two_column_file.csv\');");
      code.addRCode("cn <- 22");
      code.addRCode("md <- md[order(md[,2]),]; ");
      code.addRCode("n <- length(md[,1]);");
      code.addRCode("n <- n + 0;");
      code.addRCode("names <- md[1:n, 1];");
      code.addRCode("row.names(md) <- names;");
      code.addRCode("md <- md[,2:3];");
      code.addRCode("mdmatrix <- data.matrix(md);");
      code.addRCode("jpeg(\"theImage.jpg\")");
      code.addRCode("heatmap(mdmatrix, Rowv=NA, Colv=NA, col=(colorpanel(cn, low=\"dark blue\", mid=\"gray\", high=\"red\")), scale=\"column\", margins=c(6,22));");
      code.addRCode("dev.off() ");
      code.addRCode("mdhm <- heatmap(mdmatrix, Rowv=NA, Colv=NA, col=(colorpanel(cn, low=\"dark blue\", mid=\"gray\", high=\"red\")), scale=\"column\", margins=c(6,22));");



      code.endPlot();

      caller.setRCode(code);
      caller.runOnly();
      code.showPlot(file);
      }
      catch(Exception err)
      {
      System.out.println("There was a problem with HeatMap");
      }
      }
      }

      Delete

Thanks