R is powerful language for statistical analysis and Machine Learning. Java is best suited to develop enterprise applications. Integrating both could lead to developing powerful analytic applications. In this tutorial we are going to call a R script from java, then pass the parameters from java to R and fetch back the results from R to Java.
Problem Statement: In this tutorial we have a csv file 'data.csv' having 4 columns and we need to calculate mean of few user defined columns. To calculate the mean we will be using and then we will pass the calculated mean back to Java.
Pre-requisites:
1. You should know how to integrate R with Java. For that you could consult the tutorial http://rajeev0401.blogspot.in/2013/10/using-r-programming-language-inside-java.html
Code Files:
We will be needing following files:
1. data.csv: file having data that will be used in R script.
2. config.R: this file contains the configuration (vector having column index) to be used in 'test.R'.
3. test.R: file having R script. 3
4. Main.java: file having Java code to integrate R with Java.
The structure of the above mentioned files is mentioned below:
1. data.csv : This file contains the actual data that will be used in R script. This file contains 4 columns as follows:
0.556,0.949,8.302,10.000
0.152,2.893,6.585,2.000
0.162,2.901,3.760,8.000
1.975,1.720,1.141,8.000
2. config.R : Any variable that needs to be used in R script, called from Java code, has to be predefined.
Before we execute 'test.R' from java we need to define the name 'colVector' that will hold the indicies
of columns for which mean is to be calculated. To do that we will define a 'colVector' in 'config.R'
colVector=c()
Just copy this line to an empty notepad and save it with name 'config.R'.
3. test.R : This file contains the main logic of calculating mean of user defines columns.
# Include this library to use function 'fread', that is used to read data from a csv file.
library(data.table)
# This variable will hold the mean of selected columns.
result=c()
#Lets say the data file 'data.csv' is stored at location 'D:\dataFolder\data.csv'. Read this file in R.
# Also note the double slash used in file path.
data=data.frame(fread('D:\\dataFolder\\data.csv'))
#Calculate mean for each column.
for(i in 1:length(colVector)){
# Fetch current column and calculate its mean.
colMean=mean(data[,i])
result=c(result,mean)
}
4. Main.java: This is the starting point of the program. In this file user will define the columns for which mean is to computed. After getting back the results from 'test.R' this class will display the result back to the console.
I hope this will help in creating apps that need to pass data between Java and R & vice-versa.
Thanks
Problem Statement: In this tutorial we have a csv file 'data.csv' having 4 columns and we need to calculate mean of few user defined columns. To calculate the mean we will be using and then we will pass the calculated mean back to Java.
Pre-requisites:
1. You should know how to integrate R with Java. For that you could consult the tutorial http://rajeev0401.blogspot.in/2013/10/using-r-programming-language-inside-java.html
Code Files:
We will be needing following files:
1. data.csv: file having data that will be used in R script.
2. config.R: this file contains the configuration (vector having column index) to be used in 'test.R'.
3. test.R: file having R script. 3
4. Main.java: file having Java code to integrate R with Java.
The structure of the above mentioned files is mentioned below:
1. data.csv : This file contains the actual data that will be used in R script. This file contains 4 columns as follows:
0.556,0.949,8.302,10.000
0.152,2.893,6.585,2.000
0.162,2.901,3.760,8.000
1.975,1.720,1.141,8.000
2. config.R : Any variable that needs to be used in R script, called from Java code, has to be predefined.
Before we execute 'test.R' from java we need to define the name 'colVector' that will hold the indicies
of columns for which mean is to be calculated. To do that we will define a 'colVector' in 'config.R'
colVector=c()
Just copy this line to an empty notepad and save it with name 'config.R'.
3. test.R : This file contains the main logic of calculating mean of user defines columns.
# Include this library to use function 'fread', that is used to read data from a csv file.
library(data.table)
# This variable will hold the mean of selected columns.
result=c()
#Lets say the data file 'data.csv' is stored at location 'D:\dataFolder\data.csv'. Read this file in R.
# Also note the double slash used in file path.
data=data.frame(fread('D:\\dataFolder\\data.csv'))
#Calculate mean for each column.
for(i in 1:length(colVector)){
# Fetch current column and calculate its mean.
colMean=mean(data[,i])
result=c(result,mean)
}
4. Main.java: This is the starting point of the program. In this file user will define the columns for which mean is to computed. After getting back the results from 'test.R' this class will display the result back to the console.
package rInsideJava;
import org.rosuda.JRI.REXP;
import org.rosuda.JRI.Rengine;
import org.rosuda.REngine.REngineException;
import org.rosuda.REngine.JRI.JRIEngine;
public class Main {
public static void main(String a[]) throws REngineException
{
Rengine re = new Rengine (new String[]{"--no-save"}, false, null);
// Define the index of columns for which mean is to be calculated (in R index starts at 1, unlike Java where it starts at 0)
//Let's say we need to compute mean for column 1 and column 3. For that we need to construct a R vector in form of Java String.
String columns="c(1,3)";
// All the inputs that are to be supplied to R script, must be supplied to the context before we execute the actual R file having analytics logic.
// For that we have already create 'config.R'. So firstly we will call 'config.R', pass the value of columns and then call actual 'test.R'
// Let's say 'test.R' and 'config.R' are saved directly on D:\ drive.
// Also node four slashes used in the file path.
re.eval("D:\\\\config.R");
//After we have called 'config.R', we have variable 'colVector' available. Now let's populate 'colVector' with user defined column indicies.
re.eval("colVector="+columns);
//Now call the file 'test.R' that will compute the mean of selected columns.
re.eval("D:\\\\test.R");
//Now we have supplied column indicies to R script and R script 'temp.R' will store final means in vector 'result'.
//We will get back result from R and display it in Java.
double[] resultMean=re.eval("result").asDoubles();
//Print results.
for(double mean: resultMean){
System.out.println(mean);
}
}
}
I hope this will help in creating apps that need to pass data between Java and R & vice-versa.
Thanks
All your Effective Works Gives These Kind Of Valuable Information's...Thanks for Spending your Good Time.This Unique Information's about Python is very useful
ReplyDeletepython training in chennai | python training in annanagar | python training in omr | python training in porur | python training in tambaram | python training in velachery