A nice video on RCaller is in Youtube now. The original link is in Quantlabs.net. Thanks for the uploader.
Showing posts with label rcaller. Show all posts
Showing posts with label rcaller. 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
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:
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...
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...
Labels
base64,
rcaller,
rcallerphp
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
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!
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!
Labels
php,
r,
rcaller,
rcallerphp
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
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:
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!
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:
hope works!
@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
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, 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.
Labels
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
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...
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.
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
Friday, July 8, 2011
Rcaller 2.0 - Calling R from Java
NEWS:
2016-05-15: New Release - RCaller 3.0
2015-03-13: New Release - RCaller 2.5
2014-07-15: New Release - RCaller 2.4
2014-06-07: New Research Paper on RCaller
2014-05-15: New Release - RCaller 2.3
2014-04-15: New Release - RCaller 2.2
I have received too many e-mails since i had first submitted the early versions of the RCaller. Some users found it usable so i was planning to develop a newer and enhanced version of this library. Now, i think, it is ready for testing. The 2.0.0 version of the RCaller is downloadable from
http://code.google.com/p/rcaller/downloads/list
with both compiled jar file and the source file with the directory structure of NetBeans 7.
The use of RCaller is changed after version 1.0 but it is still easy to implement, it does not need extra libraries, it is platform independent and compatible with the recent R versions.
Some new features in version 2.0.0 are:
1) Support for plots
2) Easier code generation
3) Enhanced interaction with R
Before anything, install the R package "Runiversal" by typing
install.packages ( "Runiversal" )
in R interactive interpreter. If installation is success, you are ready for calling R from Java.
Let me explain them with some examples. Suppose that we have a double array with values of {1,4,3,5,6,10} and we want to show a time series plot with this. Firstly we import the needed libraries:
import java.io.File; import rcaller.RCaller;
We are declaring the double array:
double[] numbers = new double[] {1,4,3,5,6,10};
Creating an object of Rcaller class:
RCaller caller = new RCaller();
RCaller needs the Rscript executable file (Rscript.exe in windows) which is shipped with the R. You must tell the full path of this file in RCaller like this:
caller.setRscriptExecutable("/usr/bin/Rscript");
This is the location of my Rscript file in my Ubuntu Linux. We didn't do much thing, but this code initializes the whole thing:
caller.cleanRCode();
the cleanRCode() function of RCaller class cleans the code buffer and puts some code in it. You can browse the source code if you want to know more about the initialization. Now, we can add our double array to our R code:
caller.addDoubleArray("x", numbers);
Now we have 'x' with the value of numbers[]. Now we are creating the time series plot:
File file = caller.startPlot(); caller.addRCode("plot.ts(x)"); caller.endPlot();
Finally we are sending the whole code to R interpreter:
caller.runOnly();
With this code, Rscript runs our code but it does not return anything. After all, if we have'nt got any errors, we can handle the generated image using
ImageIcon ii=caller.getPlot(file);
or we can show it directly using
caller.showPlot(file);
The generated plot is shown below:
The source code of entire example is given below:
package test; import java.io.File; import javax.swing.ImageIcon; import rcaller.RCaller; public class Test1 { public static void main(String[] args) { new Test1(); } /* * Test for simple plots */ public Test1() { try { RCaller caller = new RCaller(); caller.setRscriptExecutable("/usr/bin/Rscript"); caller.cleanRCode(); double[] numbers = new double[]{1, 4, 3, 5, 6, 10}; caller.addDoubleArray("x", numbers); File file = caller.startPlot(); caller.addRCode("plot.ts(x)"); caller.endPlot(); caller.runOnly(); ImageIcon ii = caller.getPlot(file); caller.showPlot(file); } catch (Exception e) { System.out.println(e.toString()); } } }
This example shows how to send vectors to R, call R function from Java and handle the result from Java. We are generating the x and the y vectors in Java and sending an "ordinary least squares" command to R. After running process, we handle the calculated residuals, fitted values and residuals from Java. I hope the example is clear enough to understand.
package test; import rcaller.RCaller; public class Test2 { public static void main(String[] args){ new Test2(); } public Test2(){ try{ //Creating an instance of class RCaller RCaller caller = new RCaller(); //Important. Where is the Rscript? //This is Rscript.exe in windows caller.setRscriptExecutable("/usr/bin/Rscript"); //Generating x and y vectors double[] x = new double[]{1,2,3,4,5,6,7,8,9,10}; double[] y = new double[]{2,4,6,8,10,12,14,16,18,30}; //Generating R code //addDoubleArray() method converts Java arrays to R vectors caller.addDoubleArray("x", x); caller.addDoubleArray("y", y); //ols<-lm(y~x) is totally R Code //but we send the x and the y vectors from Java caller.addRCode("ols<-lm(y~x)"); //We are running the R code but //we want code to send some result to us (java) //We want to handle the ols object generated in R side // caller.runAndReturnResult("ols"); //We are printing the content of ols System.out.println("Available results from lm() object:"); System.out.println(caller.getParser().getNames()); //Parsing some objects of lm() //Residuals, coefficients and fitted.values are some elements of lm() //object returned by the R. We parsing those elements to use in Java double[] residuals = caller.getParser().getAsDoubleArray("residuals"); double[] coefficients = caller.getParser().getAsDoubleArray("coefficients"); double[] fitteds = caller.getParser().getAsDoubleArray("fitted_values"); //Printing results System.out.println("Coefficients:"); for (int i=0;i< span=""> System.out.println("Beta "+i+" = "+coefficients[i]); } System.out.println("Residuals:"); for (int i=0;i< span=""> System.out.println(i+" = "+residuals[i]); } }catch (Exception e){ System.out.println(e.toString()); } } }
I hope it works for you.
CodeProject
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)