Showing posts with label rcaller. Show all posts
Showing posts with label rcaller. Show all posts

Monday, June 16, 2014

RCaller 2.4 has just been released

Rcaller turtle The key properties of this release:
  • Added deleteTempFiles() method in class RCaller for deleting temporary files that are created by RCaller at any time. 
  • runiversal.r is now more compact
  • StopRCallerOnline() method in class RCaller now stops the R instances in the memory which are created in runAndReturnResultOnline(). Click to see the example for RCaller.stopRCallerOnline() method.
The next release 2.5 will be submitted in 15th July 2014.


Get informed using the formal blog http://stdioe.blogspot.com.tr/search/label/rcaller

 Download page: https://drive.google.com/?authuser=0#folders/0B-sn_YiTiFLGZUt6d3gteVdjTGM

 Source code: https://code.google.com/p/rcaller/

 Home page: http://mhsatman.com/tag/rcaller/

 Journal Documentation: http://www.sciencedomain.org/abstract.php?iid=550&id=6&aid=4838#.U59D8_mSy1Y

Friday, June 13, 2014

R GUI written in Java using RCaller


This video demonstrates how the Java version of R GUI based on RCaller is now faster after the speed improvements. This simple gui is available in the source tree. Typed commands are passed to R using the online call mechanism of RCaller and there is a single active R process at the background. 

Please follow the rcaller label in this blog site to achive latest RCaller news, updates, examples and other materials. 

Have a nice watching!



Scholarly papers, projects and thesis that cite RCaller

paperRCaller is now in its 4th year with its version of 2.3 and it is considerable mature now. It is used in many commercial projects as well as scholarly papers and thesis. Here is the list of scholarly papers, projects and thesis that I stumbled upon in Google Scholar.  




 
  • Niya Wang, Fan Meng, Li Chen, Subha Madhavan, Robert Clarke, Eric P. Hoffman, Jianhua Xuan, and Yue Wang. 2013. The CAM software for nonnegative blind source separation in R-Java. J. Mach. Learn. Res. 14, 1 (January 2013), 2899-2903. http://dl.acm.org/citation.cfm?id=2567753
 
  • Meng, Fan. Design and Implementation of Convex Analysis of Mixtures Software Suite, Master's Thesis, 2012. Abstract: Various convex analysis of mixtures (CAM) based algorithms have been developed to address real world blind source separation (BSS) problems and proven to have good performances in previous papers. This thesis reported the implementation of a comprehensive software CAM-Java, which contains three different CAM based algorithms, CAM compartment modeling (CAM-CM), CAM non-negative independent component analysis (CAM-nICA), and CAM non-negative well-grounded component analysis (CAM-nWCA). The implementation works include: translation of MATLAB coded algorithms to open-sourced R alternatives. As well as building a user friendly graphic user interface (GUI) to integrate three algorithms together, which is accomplished by adopting Java Swing API.In order to combine R and Java coded modules, an open-sourced project RCaller is used to handle the establishment of low level connection between R and Java environment. In addition, specific R scripts and Java classes are also implemented to accomplish the tasks of passing parameters and input data from Java to R, run R scripts in Java environment, read R results back to Java, display R generated figures, and so on. Furthermore, system stream redirection and multi-threads techniques are used to build a simple R messages displaying window in Java built GUI.The final version of the software runs smoothly and stable, and the CAM-CM results on both simulated and real DCE-MRI data are quite close to the original MATLAB version algorithms. The whole GUI based open-sourced software is easy to use, and can be freely distributed among the communities. Technical details in both R and Java modules implementation are also discussed, which presents some good examples of how to develop software with both complicate and up to date algorithms, as well as decent and user friendly GUI in the scientific or engineering research fields. http://scholar.lib.vt.edu/theses/available/etd-08202012-162249/
 
  • Emanuel Gonçalves, Julio Saez-Rodriguez. Cyrface: An interface from Cytoscape to R that provides a user interface to R packages, F1000Research 2013, 2:192 Last updated: 20 JAN 2014, http://f1000research.com/articles/2-192/v1/pdf
 
 
 
 
   

Monday, June 9, 2014

New Documentation for RCaller

As a new documentation and brief introduction, the research paper "RCaller: A Software Library for Calling R from Java" has just been published in the scholarly journal "British Journal of Mathematics and Computer Science".

The aim and the motivation underlying this paper is to give a brief introduction to RCaller, how to use it in relatively small projects by means of calling R scripts and commands from Java, generating plots and images, running commands online and converting and sending plain Java objects to R.

Other two important projects, rJava and Rserve, are compared to RCaller by means of time efficiency. As a result of this, it is shown that, rJava and Rserve outperforms the RCaller in time complexity, but RCaller seems to be easier to learn and requires less setting-up effort.

The paper is freely available for downloading at

 http://www.sciencedomain.org/abstract.php?iid=550&id=6&aid=4838#.U5VvkPl_t2M

and the author's page is

http://mhsatman.com/research-paper-rcaller-a-software-library-for-calling-r-from-java/ .

Have a nice read!






Thursday, May 15, 2014

New Release: RCaller 2.3.0

New version of RCaller has just been uploaded in the Google Drive repository.

The new version includes basic bug fixes, new test files and speed enhancements.

XML file structure is now smaller in size and this makes RCaller a little bit faster than the older versions.

The most important issue in this release is the method

public int[] getDimensions(String name)

which reports the dimensions of a given object with 'name'. Here is an example:

int n = 21;
        int m = 23;
        double[][] data = new double[n][m];
        for (int i=0;i<data.length;i++){
            for (int j=0;j<data[0].length;j++){
                data[i][j] = Math.random();
            }
        }
        RCaller caller = new RCaller();
        Globals.detect_current_rscript();
        caller.setRscriptExecutable(Globals.Rscript_current);
       
        RCode code = new RCode();
        code.addDoubleMatrix("x", data);
        caller.setRCode(code);
       
        caller.runAndReturnResult("x");
       
        int[] mydim = caller.getParser().getDimensions("x");
       
        Assert.assertEquals(n, mydim[0]);
        Assert.assertEquals(m, mydim[1]);

In the code above, a matrix with dimensions 21 and 23 is passed to R and got back to Java. The variable mydim holds the number of rows and columns and they are as expected as 21 and 23.

Please use the download link

https://drive.google.com/?tab=mo&authuser=0#folders/0B-sn_YiTiFLGZUt6d3gteVdjTGM

to access compiled jar files of RCaller.

Good luck!

Monday, April 21, 2014

Matrix Inversion with RCaller 2.2

Here is the example of passing a double[][] matrix from Java to R, making R calculate the inverse of this matrix and handling the result in Java. Note that code is current for 2.2 version of RCaller.


RCaller caller = new RCaller(); 
Globals.detect_current_rscript(); 
caller.setRscriptExecutable(Globals.Rscript_current); 

RCode code = new RCode(); 
double[][] matrix = new double[][]{{6, 4}, {9, 8}};

code.addDoubleMatrix("x", matrix); 
code.addRCode("s<-solve font="" x="">); 

caller.setRCode(code); 

caller.runAndReturnResult("s"); 

double[][] inverse = caller.getParser().getAsDoubleMatrix("s"
                                       matrix.length, matrix[0].length); 
        
for (int i = 0; i < inverse.length; i++) {
    for (int j = 0; j < inverse[0].length; j++) {
        System.out.print( inverse[i][j] + " ");
    System.out.println();
} 
 

Saturday, April 12, 2014

RCaller 2.2.0 has just been released

We have just uploaded the new compiled jar file of latest Rcaller, the simple library for calling R from within Java.

We plan to clean recently reported bugs, but the most important one was having some errors about the R package Runiversal, which is required by the library for generating XML files. The basic issue underlying this problem was the package storing policy of R which depends on the user that installed the package.

In the most recent version 2.2, users do not need to pre-install the R package Runiversal. Simply add RCaller-2.2.0-SNAPSHOT.jar to your classpath and go!

The download link of the compiled library is here [Google Driver Link]

The library is tested in a pc with Ubuntu OS installed and the usual test scenarios are success in all cases. The library has not been tested on Windows machines. 

Please use the link of Google code page at http://code.google.com/p/rcaller/issues/list and enter your problems in issues part and do not hesitate to contribute our library. 

Saturday, August 17, 2013

A User Document For RCaller

A new research paper as a RCaller documentation is freely available at http://www.sciencedomain.org/abstract.php?iid=550&id=6&aid=4838#.U5YSoPmSy1Y




RCaller: A library for calling R from Java

by M.Hakan Satman

August 17, 2013

Contents


Abstract

RCaller is an open-source, compact, and easy-to-use library for calling R from Java. It offers not only an elegant solution for the task but its simplicity is key for non-programmers or programmers who are not familier with the internal structure of R. Since R is not only a statistical software but an enormous collection of statistical functions, accessing its functions and packages is of tremendous value. In this short paper, we give a brief introduction on the most widely-used methods to call R from Java and highlight some properties of RCaller with short examples. User feedback has shown that RCaller is an important tool in many cases where performance is not a central concern.

1 Introduction


R [R Development Core Team(2011)] is an open source and freely distributed statistics software package for which hundreds of external packages are available. The core functionality of R is written mostly in C and wrapped by R functions which simplify parameter passing. Since R manages the exhaustive dynamic library loading tasks in a clever way, calling an external compiled function is easy as calling an R function in R. However, integration with JVM (Java Virtual Machine) languages is painful.
The R package rJava [Urbanek(2011a)] provides a useful mechanism for instantiating Java objects, accessing class elements and passing R objects to Java methods in R. This library is convenient for the R packages that rely on external functionality written in Java rather than C, C++ or Fortran.
The library JRI, which is now a part of the package rJava, uses JNI (Java Native Interface) to call R from Java [Urbanek(2009)]. Although JNI is the most common way of accessing native libraries in Java, JRI requires that several system and environment variables are correctly set before any run, which can be difficult for inexperienced users, especially those who are not computer scientists.
The package Rserve [Urbanek(2011b)] uses TCP sockets and acts as a TCP server. A client establishes a connection to Rserve, sends R commands, and receives the results. This way of calling R from the other platforms is more general because the handshaking and the protocol initializing is fully platform independent.
Renjin (http://code.google.com/p/renjin) is an other interesting project that addresses the problem. It solves the problem of calling R from Java by re-implementing the R interpreter in Java! With this definition, the project includes the tasks of writing the interpreter and implementing the internals. Renjin is intended to be 100% compatible with the original. However, it is under development and needs help. After all, an online demo is available which is updated simultaneously when the source code is updated.
Finally, RCaller [RCaller Development Team(2011)] is an LGPL’d library which is very easy to use. It does not do much but wraps the operations well. It requires no configuration beyond installing an R package (Runiversal) and locating the Rscript binary distributed with R. Altough it is known to be relatively inefficient compared to other options, its latest release features significant performance improvements.

2 Calling R Functions


Calling R code from other languages is not trivial. R includes a huge collection of math and statistics libraries with nearly 700 internal functions and hundreds of external packages. No comparable library exists in Java. Although libraries such as the Apache Commons Math [Commons Math Developers(2010)] do provide many classes for those calculations, its scope is quite limited compared to R. For example, it is not easy to find such a library that calculates quantiles and probabilities of non-central distributions. [Harner et al.(2009)Harner, Luo, and Tan] affirms that using R’s functionality from Java prevents the user from writing duplicative codes in statistics softwares.
RCaller is an other open source library for performing R operations from within Java applications in a wrapped way. RCaller prepares R code using the user input. The user input is generally a Java array, a plain Java object or the R code itself. It then creates an external R process by running the Rscript executable. It passes the generated R code and receives the output as XML documents. While the process is alive, the output of the standard input and the standard error streams are handled by an event-driven mechanism. The returned XML document is then parsed and the returned R objects are extracted to Java arrays.
The short example given below creates two double vectors, passes them to R, and returns the residuals calculated from a linear regression estimation.
RCaller caller = new RCaller();
RCode code = new RCode();
double[] xvector = new double[]{1,3,5,3,2,4};
double[] yvector = new double[]{6,7,5,6,5,6};

caller.setRscriptExecutable("/usr/bin/Rscript");

code.addDoubleArray("X", xvector);
code.addDoubleArray("Y", yvector);
code.addRCode("ols <- lm ( Y ~ X )");

caller.setRCode(code);

caller.runAndReturnResult("ols");

double[] residuals =
   caller.getParser().
     getAsDoubleArray("residuals");  

The lm function returns an R list with a class of lm whose elements are accessible with the $ operator. The method runAndReturnResult() takes the name of an R list which contains the desired results. Finally, the method getAsDoubleArray() returns a double vector with values filled from the vector residuals of the list ols.
RCaller uses the R package Runiversal [Satman(2010)] to convert R lists to XML documents within the R process. This package includes the method makexml() which takes an R list as input and returns a string of XML document. Although some R functions return the results in other types and classes of data, those results can be returned to the JVM indirectly. Suppose that obj is an S4 object with members member1 and member2. These members are accessible with the @ operator like obj@member1 and obj@member2. These elements can be returned to Java by constructing a new list like result\A1-list(m1=obj@member1, m2=obj@member2).

3 Handling Plots


Although the graphics drivers and the internals are implemented in C, most of the graphics functions and packages are written in the R language and this makes the R unique with its graphics library. RCaller handles a plot with the function startPlot() and receives a java.io.File reference to the generated plot. The function getPlot() returns an instance of the javax.swing.ImageIcon class which contains the generated image in a fully isolated way. A Java example is shown below:
RCaller caller = new RCaller();
RCode code = new RCode();
File plotFile = null;
ImageIcon plotImage = null;

caller.
setRscriptExecutable("/usr/bin/Rscript");

code.R_require("lattice");

try{
 plotFile = code.startPlot();
 code.addRCode("
      xyplot(rnorm(100)~1:100, type=’l’)
      ");
}catch (IOException err){
 System.out.println("Can not create plot");
}

caller.setRCode(code);
caller.runOnly();

plotImage = code.getPlot(plotFile);
code.showPlot(plotFile);

The method runOnly() is quite different from the method RunAndReturnResult(). Because the user only wants a plot to be generated, there is nothing returned by R in the example above. Note that more than one plots can be generated in a single run.
Handling R plots with a java.io.File reference is also convenient in web projects. Generated content can be easly sent to clients using output streams opened from the file reference. However, RCaller uses the temp directory and does not delete the generated files automatically. This may be a cause of a too many files OS level error which can not be caught by a Java program. However, cleaning the generated output using a scheduled task solves this problem.

4 Live Connection


Each time the method runAndReturnResult() is called, an Rscript instance is created to perform the operations. This is the main source of the inefficiency of RCaller. A better approach in the cases that R commands are repeatedly called is to use the method runAndReturnResultOnline(). This method creates an R instance and keeps it running in the background. This approach avoids the time required to create an external process, initialize the interpreter, and load packages in subsequent calls.
The example given below returns the determinants of a given matrix and its inverse in sequence, that is, it uses a single external instance to perform more than one operation.
double[][] matrix =
    new double[][]{{5,4,5},{6,1,0},{9,-1,2}};
caller.setRExecutable("/usr/bin/R");
caller.setRCode(code);

code.clear();
code.addDoubleMatrix("x", matrix);
code.addRCode("result<-list(d=det(x))");
caller.runAndReturnResultOnline("result");

System.out.println(
"Determinant is " +
  caller.getParser().
   getAsDoubleArray("d")[0]
   );

code.addRCode("result<-list(t=det(solve(x)))");
caller.runAndReturnResultOnline("result");

System.out.println(
"Determinant of inverse is " +
  caller.getParser().
   getAsDoubleArray("t")[0]
   );

This use of RCaller is fast and convenient for repeated commands. Since R is not thread-safe, its functions can not be called by more than one threads. Therefore, each single thread must create its own R process to perform calculations simultaneously in Java.

5 Monitoring the Output


RCaller receives the desired content as XML documents. The content is a list of the variables of interest which are manually created by the user or returned automatically by a function. Apart from the generated content, R produces some output to the standard output (stdout) and the standard error (stderr) devices. RCaller offers two options to handle these outputs. The first one is to save them in a text file. The other is to redirect all of the content to the standard output device. The example given below shows a conditional redirection of the outputs generated by R.
if(console){
 caller.redirectROutputToConsole();
}else{
 caller.redirectROutputToFile(
     "output.txt" /* filename */,
     true  /* append? */);
}

6 Conclusion


In addition to being a statistical software, R is an extendable library with its internal functions and external packages. Since the R interpreter was written mostly in C, linking to custom C/C++ programs is relatively simple. Unfortunately, calling R functions from Java is not straightforward. The prominent methods use JNI and TCP sockets to solve this problem. In addition, renjin offers a different perspective to this issue. It is a re-implementation of R in Java which is intended to be 100% compatible with the original. However, it is under development and needs help. Finally, RCaller is an alternative way of calling R from Java. It is packaged in a single jar and it does not require setup beyond the one-time installation of the R package Runiversal. It supports loading external packages, calling functions, handling plots and debugging the output generated by R. It is not the most efficient method compared to the alternatives, but users report that performance improvements in the latest revision and its simplicity of use make it an important tool in many applications.

References


[Commons Math Developers(2010)]   Commons Math Developers. Apache Commons Math, Release 2.1. Available from http://commons.apache.org/math/download_math.cgi, Apr. 2010. URL http://commons.apache.org/math.
[Harner et al.(2009)Harner, Luo, and Tan]   E. Harner, D. Luo, and J. Tan. JavaStat: A Java/R-based statistical computing environment. Computational Statistics, 24(2):295–302, May 2009.
[R Development Core Team(2011)]   R Development Core Team. R: A Language and Environment for Statistical Computing. R Foundation for Statistical Computing, Vienna, Austria, 2011. URL http://www.R-project.org/. ISBN 3-900051-07-0.
[RCaller Development Team(2011)]   RCaller Development Team. RCaller: A library for calling R from Java, 2011. URL http://code.google.com/p/rcaller.
[Satman(2010)]   M. H. Satman. Runiversal: A Package for converting R objects to Java variables and XML., 2010. URL http://CRAN.R-project.org/package=Runiversal. R package version 1.0.1.
[Urbanek(2009)]   S. Urbanek. How to talk to strangers: ways to leverage connectivity between R, Java and Objective C. Computational Statistics, 24(2):303–311, May 2009.
[Urbanek(2011a)]   S. Urbanek. rJava: Low-level R to Java interface, 2011a. URL http://CRAN.R-project.org/package=rJava. R package version 0.9-2.
[Urbanek(2011b)]   S. Urbanek. Rserve: Binary R server, 2011b. URL http://CRAN.R-project.org/package=Rserve. R package version 0.6-5.





A new research paper as a RCaller documentation is freely available at http://www.sciencedomain.org/abstract.php?iid=550&id=6&aid=4838#.U5YSoPmSy1Y






























Friday, August 17, 2012

A nice video on RCaller 2.0


A nice video on RCaller is in Youtube now. The original link is in Quantlabs.net. Thanks for the uploader.

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!

Monday, October 31, 2011

Handling Plots with RCallerPhp Edition

Now we have RCaller Php edition, named RCallerPhp, which is able to handle images generated by R. I hope it will bring an other easy solution for calling R from other platforms.

With this feature, web developers that use Php as their main language and need calculations done by R will easly use this library.

It is distributed as its big brother, RCaller Java Edition, with the LGPL (Lesser GNU Public License).
RCallerPhp is intended to be compatible as much as possible with the Java version. So, investigating the old examples may be helpful for understanding this new release.

In a time of less than a week, we released this version without the plotting support. By now, generating R plots and showing them in a browser is implemented. Generated plots are stored in the temp directory instead www directory. That is why we are encoding generated plots inside the img src tags with base64 encoding. You can have a look at the source code at RCaller source code at Google project hosting.

Handling plots with RCallerPhp is quite easy. Let's have a look at the code below:

<?php

require_once("../RCode.php");
require_once("../RCaller.php");
require_once("simpletest.php");

$caller = new RCaller();
$code = new RCode("");

$plot = $code->startPlot();
$code->addRCode("plot.ts(rnorm(10))");
$code->endPlot();

$caller->setRscriptExecutable("/usr/bin/Rscript");
$caller->setRCode($code);
$caller->runOnly();

print($caller->getPlot($plot));
?>

Here is the generated output, which is copied from the web browser:


Nothing is easier than this! Do not hasitate to ask anything about RCaller.
We hope you enjoy...
Have fun...

Sunday, October 30, 2011

RCallerPhp is ready for testing

Hey web guys! RCaller now supports Php and we are planning to carry RCaller to other platforms and languages. The first step of our attack plan was to implement a Php edition and it is ready for testing now.

The second step is to implement RCaller for Perl and Python. We have now our Perl developer and he is in progress. Python is not our primary language and we are waiting for your helps. If you are familier with R and a developer of one of those languages below, join us. We are planning to carry RCaller to

  • Python
  • .Net
at first.

And... How it looks like.. Let's give up Java and speak Php for a minute:

 1 <?php
 2 include_once ("RCaller.php");
 3 
 4 $rcaller = new RCaller();
 5   $rcaller->setRscriptExecutable("/usr/bin/Rscript");
 6   $rcode = new RCode("");
 7   $rcode->clear();
 8   $rcode->addRCode("mylist <- list(x=1:3, y=c(7,8,9))");
 9 
10   $rcaller->setRCode($rcode);
11   $rcaller->runAndReturnResult("mylist");
12 
13   $x = $rcaller->getParser()->getAsStringArray("x");
14   $y = $rcaller->getParser()->getAsStringArray("y");
15 
16   echo ("X is <br>");
17   print_r ($x);
18 
19   echo ("<br><br>Y is <br>");
20   print_r ($y);
21 ?>


Waaav! Nothings changes! When you run this code, you will see values of x as 1, 2 and 3 and values of y as 7, 8, 9... The code above seems 100% compatible with the original library...

If you have used RCaller (Java edition) before, you will probably
understand the whole code. If not, lets have a look at the page RCaller 2.0 - Calling R from Java.


Note that, it is as in-efficient as the original version. Because RCaller creates external Rscript processes in each time RunAndReturnResult() thingies called. Be careful before using it in big and critical projects. Another note is about using it with too many users. RCaller uses temp directory to store its R codes and outputs. You need to clear this directory periodically. Otherwise you can have a "too many files" error.


Finally, source of is ready for use and development. Please visit the RCaller source code and downloads page. Php codes are stored as a separate project with name RCallerPhp.

Test it and do not hasitate to ask us!

Wednesday, September 28, 2011

Passing plain Java objects to R using RCaller

Well, you are using RCaller for your statistical calculations. Probably, you are passing your double arrays to R and type some R commands in order to get the desired outputs. After a calculation process, you handle the returned arrays through the parser. This is the general use of RCaller.

Suppose that you have got a Java class which has got some variables with data types int, short, long, float, double and String. This class also includes some arrays of types int[], double[], ..., String[]. Of course it may include some functions, constructors or anything else. But we don't care about this for now.  How about passing this class with its publicly defined variables to R? Yeah! It is possible in its last submitted revision.

Have a look at the Java class below:


class TestClass {

  public int i = 9;
  public float f = 10.0f;
  public double d = 3.14;
  public boolean b = true;
  public String s = "test";
}

This class simply includes five publicly defined variables with basic data types. Our other class inherits the TestClass and defines some additional arrays:


class TestClassWithArrays extends TestClass {

  public int[] ia = new int[]{1, 2, 3, 4, 5};
  public double[] da = new double[]{1.0, 2.0, 3.0, 4.0, 9.9, 10.1};
  public String[] sa = new String[]{"One", "Two", "Three"};
  public boolean[] ba = new boolean[]{true, true, false};
}

Ok, they are very simple but there is no reason those classes not to have any methods. Whatever those classes have methods, we consider them as data structures.

Lets pass this data structure to R:


TestClassWithArrays tcwa = new TestClassWithArrays();
    JavaObject jo = new JavaObject("tcwa", tcwa);

    RCaller rcaller = new RCaller();
    rcaller.setRscriptExecutable("/usr/bin/Rscript");
    rcaller.cleanRCode();

    rcaller.addRCode(jo.produceRCode());
    rcaller.runAndReturnResult("tcwa");


Well, if there is no expection we have the results in a R list named "tcwa". This R list includes all of the elements that included in TestClassWithArrays and TestClass with their values.

This is an example of proof, the related @Test method is ready for browsing in the Google Code:

@Test
  public void TestClassWithArrays() throws IllegalAccessException, IOException {
    TestClassWithArrays tcwa = new TestClassWithArrays();
    JavaObject jo = new JavaObject("tcwa", tcwa);

    RCaller rcaller = new RCaller();
    rcaller.setRscriptExecutable("/usr/bin/Rscript");
    rcaller.cleanRCode();

    rcaller.addRCode(jo.produceRCode());
    rcaller.runAndReturnResult("tcwa");

    int[] expectedIntArray = rcaller.getParser().getAsIntArray("ia");
    for (int i = 0; i < tcwa.ia.length; i++) {
      assertEquals(expectedIntArray[i], tcwa.ia[i]);
    }


    double[] expectedDoubleArray = rcaller.getParser().getAsDoubleArray("da");
    for (int i = 0; i < tcwa.da.length; i++) {
      assertEquals(expectedDoubleArray[i], tcwa.da[i], delta);
    }

    String[] expectedStringArray = rcaller.getParser().getAsStringArray("sa");
    for (int i = 0; i < tcwa.sa.length; i++) {
      assertEquals(expectedStringArray[i], tcwa.sa[i]);
    }

  }

It is shown that in examples, in R side, we can access to elements with their original names that defined in the Java class. That sounds good.

Finally, we can pass our Java objects with defined contents. This use of RCaller narrows the code of addDoubleArray, addIntArray and reduce all of them to simple command of

 JavaObject jo = new JavaObject("tcwa", tcwa);
.
.
.
rcaller.addRCode ( jo.produceRCode() );
 

It is simplicity...

Saturday, September 17, 2011

RCaller: Support for sequential commands with a single process

I think, this revision will be the foundation of the version  2.1. RCaller is supposed to be slow but the easiest way of calling R from Java.

Finally I have implemented the method runAndReturnResultOnline() for running sequential commands in a single process. What does this stand for? Let me give an example to explain this:

Suppose that you want to perform a simulation study to measure the success of your new procedure. For this, you decide to draw random numbers from a distribution and calculate something and handle the results in Java. RCaller creates  Rscript processes for each single iteration. This cause to too many operating system calls.

Latest release of RCaller includes the method for this. Lets have a look at the Test file:


@Test
  public void onlineCalculationTest() {
    RCaller rcaller = new RCaller();
    rcaller.setRExecutable("/usr/bin/R");
    rcaller.cleanRCode();
    rcaller.addRCode("a<-1:10");
    rcaller.runAndReturnResultOnline("a");
    assertEquals(rcaller.getParser().getAsIntArray("a")[0], 1);

    rcaller.cleanRCode();
    rcaller.addRCode("b<-1:10");
    rcaller.addRCode("m<-mean(b)");
    rcaller.runAndReturnResultOnline("m");
    assertEquals(rcaller.getParser().getAsDoubleArray("m")[0], 5.5, 0.000001);

    rcaller.cleanRCode();
    rcaller.addRCode("a<-1:99");
    rcaller.addRCode("k<-median(a)");
    rcaller.runAndReturnResultOnline("k");
    assertEquals(rcaller.getParser().getAsDoubleArray("k")[0], 50.0, 0.000001);
  }
  }
 

In first stage,we are creating an integer vector and getting the first element. In the second one, we are creating the same integer vector with a different name and calculating the arithmetic mean. In the last one, we are recreating the vector a and getting the median, which is equal to 50.

This example uses the same RCaller object. In first stage, the R executable file (it is /usr/bin/R in my Ubuntu Linux) is created once. In second stage the same R file is used and no longer processes are created again. In this stage, the vector a is accessible and still remains alive. At the last stage, b is alive again and a is recreated. So this example does not cause the R to open and close three times but only once.

This modification speeds up the RCaller, but it can be still considered as slow.
However, it is still easy to implement and much more faster than the previous implementation.

Have Fun!


Thursday, September 15, 2011

Handling R lists with RCaller 2.0

Since RCaller creates an Rscript process for each single run, it is said to be in-efficient for most cases. But there are useful non-hack methods to improve the method. Suppose that your aim is to calculate medians of two double vector like this:












@Test
  public void singleResultTest() {
    double delta = 0.0000001;
    RCaller rcaller = new RCaller();
    rcaller.setRscriptExecutable("/usr/bin/Rscript");
    rcaller.cleanRCode();
    rcaller.addRCode("x <- c(6 ,8, 3.4, 1, 2)");
    rcaller.addRCode("med <- median(x)");

    rcaller.runAndReturnResult("med");

    double[] result = rcaller.getParser().getAsDoubleArray("med");

    assertEquals(result[0], 3.4, delta);
  }

However, this example considers only computing the median of x, effort for computing medians of three variables needs three process which is very slow. Lists are "vector of vector" objects but they are different from matrices. A list object in R can handle several types of vector with their names. For example


alist <- list (
s = c("string1", "string2", "string3") , 
i = c(5,4,7,6),
d = c(5.5, 6.7, 8.9)
)
 

the list object alist is formed by three different kind of vectors: string vector s, integer vector i and double vector d. Also their names are s, i and d, respectively. Accessing elements of this list is straightforward. There are two ways to access to elements. First one is conventional way using indices. When the example above runs, strvec is set to String vector s.



alist <- list (
strvec <- alist[1]
While a list object can handle R objects with their names, we can handle more than more result in a single RCaller run. Back to our example, we wanted to calculate medians of three double vectors in a single run.
@Test
  public void TestLists2()throws Exception {
    double delta = 0.0000001;
    RCaller rcaller = new RCaller();
    rcaller.setRscriptExecutable("/usr/bin/Rscript");
    rcaller.cleanRCode();
    rcaller.addRCode("x <- c(6 ,8, 3.4, 1, 2)");
    rcaller.addRCode("med1 <- median(x)");

    rcaller.addRCode("y <- c(16 ,18, 13.4, 11,12)");
    rcaller.addRCode("med2 <- median(y)");

    rcaller.addRCode("z <- c(116 ,118, 113.4,111,112)");
    rcaller.addRCode("med3 <- median(z)");

    rcaller.addRCode("results <- list(m1 = med1, m2 = med2, m3 = med3)");

    rcaller.runAndReturnResult("results");

    double[] result = rcaller.getParser().getAsDoubleArray("m1");
    assertEquals(result[0], 3.4, delta);

    result = rcaller.getParser().getAsDoubleArray("m2");
    assertEquals(result[0], 13.4, delta);

    result = rcaller.getParser().getAsDoubleArray("m3");
    assertEquals(result[0], 113.4, delta);
  }
This code passes the tests. By the result at hand, we have three medians of three different vectors with one pass calculation. With this way, an huge number of vectors can be accepted as a result from R and this method may be considered efficient... these test files were integrated to source structure of project in http://code.google.com/p/rcaller/

hope works!

Thursday, August 25, 2011

More contributors needed for the project RCaller

We need new contributors to enhance the functionality of RCaller. We need also feedbacks about
  • type of projects that RCaller used in
  • frequently used functions of R
  • new functionality required.
  • Bug reports
We also need a web page, rather then http://www.mhsatman.com/rcaller. A Logo would be good.

We need developers, testers, documenters which may have skills on Java, R, LaTeX or HTML.

We can enlarge the space spanned by RCaller, say that, PhpCaller, CCaller or something derivative may be included for Php and C, respectively. Note that, there are already some libraries for calling R from other languages. RCaller has lesser efficiency on run time but higher speed on development time.

Please join the project.
google code page: https://code.google.com/p/rcaller/

Friday, August 19, 2011

RCaller is now in ohloh.net

RCaller is now in ohloh.net as a project entry. The address is https://www.ohloh.net/p/rcaller . It would be a good meeting point of developers and users of RCaller.

Friday, July 22, 2011

Random Number Generation with RCaller 2.0

Java has two standard libraries for generating random numbers. The java.lang.Math class has a random method with is used for generating uniform distributed random numbers. The second one is the java.util.Random class which has got several functions for generating random numbers. We can draw random numbers from several distribution using the probability integral transform. But R has many internal functions for random number generation from several probability distributions including the gamma, the binomial, the normal etc.


RCaller has a wrapper class, under the package statistics, for generating random number for those distributions. The class statistics. RandomNumberGenerator has these functions:


public double[] randomNormal(int n, double mean, double standardDeviation)
public double[] randomLogNormal(int n, double logmean, double logStandardDeviation) 
public double[] randomUniform(int n, double min, double max) 
public double[] randomBeta(int n, double shape1, double shape2)
public double[] randomCauchy(int n, double location, double scale) 
public double[] randomT(int n, int df) 
public double[] randomChisqyare(int n, int df)
public double[] randomF(int n, int df1, int df2)
public double[] randomPoisson(int n, double lambda) 
public double[] randomBinom(int n, int size, double p)
public double[] randomNegativeBinom(int n, int size, double p)
public double[] randomMultinomial(int n, int size, double p)
public double[] randomGeometric(int n, double p) 
public double[] randomWeibull(int n, double shape, double scale) throws 
public double[] randomHyperGeometric(int amount, int n, int m, int k) 
public double[] randomExponential(int n, double theta) throws Exception 
public double[] randomGamma(int n, double shape, double rate, double scale) 


One can see the usage of class in the Test5 class in the source of RCaller 2.0.
http://code.google.com/p/rcaller/source/browse/RCaller/src/test/Test5.java

Sunday, July 17, 2011

About the licence of RCaller

The licence of RCaller 2.0 was changed to LGPL . That means you can use it in commercial projects without distributing the source code.

For our users who like RCaller...

Tuesday, July 12, 2011

Debugging the R output of RCaller

RCaller 2.0 has been submitted to Google Code before two or three days. Many RCaller users are testing it and except a Windows installation it seems not to be so problematic.

RCaller 2.0 receives the R outputs as XML files. If the user does not know the returned variable names or if there was a problem with the results some debugging stuff is needed.

Now the function 'getXMLFileAsString()' is implemented in RCaller, by using it, the converted R output can be investigated.

Suppose that we want to run some R code from Java and we want to have a look at the returned XML content. Have a look at these codes:



package test;

import rcaller.RCaller;

/**
 *
 * @author Mehmet Hakan Satman
 */

public class Test4 {
   
    public static void main (String[] args){
        new Test4();
    }
   
    public Test4(){
        try{
            /*
             * Creating an instance of RCaller
             */
            RCaller caller = new RCaller();
           
            /*
             * Defining the Rscript executable
             */
            caller.setRscriptExecutable("/usr/bin/Rscript");
           
            /*
             * Some R Stuff
             */
            caller.addRCode("set.seed(123)");
            caller.addRCode("x<-rnorm(10)");
            caller.addRCode("y<-rnorm(10)");
            caller.addRCode("ols<-lm(y~x)");
           
            /*
             * We want to handle the object 'ols'
             */
            caller.runAndReturnResult("ols");
           
            /*
             * Getting R results as XML
             * for debugging issues.
             */
            System.out.println(caller.getParser().getXMLFileAsString());
        }catch(Exception e){
            System.out.println(e.toString());
        }
    }
}



Because of the "set.seed()"  function of R, this code should produce the same results for all machines. The XML output is






This structure of this XML file is simple and one can see that each single variable is encapsulated within a "" and a "" tag.

Another way of handling the variable names is to use "ROutputParser.getNames()". This function simply returns an ArrayList which includes the variable names returned by R.