A nice video on RCaller is in Youtube now. The original link is in Quantlabs.net. Thanks for the uploader.
Showing posts with label calling r from java. Show all posts
Showing posts with label calling r from java. Show all posts
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.
Labels
calling r from java,
java,
r,
rcaller
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!
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!
Labels
calling r from java,
java,
r,
rcaller
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:
This class simply includes five publicly defined variables with basic data types. Our other class inherits the TestClass and defines some additional arrays:
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:
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:
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
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...
Labels
calling r from java,
java,
r,
rcaller
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
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/
- type of projects that RCaller used in
- frequently used functions of R
- new functionality required.
- Bug reports
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/
Labels
calling r from java,
java,
r,
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:
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
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
Labels
calling r from java,
java,
r,
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.
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 "
Another way of handling the variable names is to use "ROutputParser.getNames()". This function simply returns an ArrayList
Labels
calling r from java,
java,
r,
rcaller
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:
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:
#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); }
Labels
calling r from java,
java,
r,
rcaller
Subscribe to:
Posts (Atom)