if (dataX.length != dataY.length)
throw new RemoteException("Unable to run computation on two arrays with different lengths (" + dataX.length
+ " != " + dataY.length + ").");
RConnection rConnection = null;
HierarchicalClusteringResult hclresult = new HierarchicalClusteringResult();
try
{
rConnection = getRConnection();
String[] agglomerationMethod = new String[7];
agglomerationMethod[0] = "ward";
agglomerationMethod[1] = "average";
agglomerationMethod[2] = "centroid";
agglomerationMethod[3] = "single";
agglomerationMethod[4] = "complete";
agglomerationMethod[5] = "median";
agglomerationMethod[6] = "mcquitty";
String agglomerationMethodType = new String("ward");
// Push the data to R
rConnection.assign("x", dataX);
rConnection.assign("y", dataY);
// checking for user method match
for (int j = 0; j < agglomerationMethod.length; j++)
{
if (agglomerationMethod[j].equals(agglomerationMethodType))
{
rConnection.assign("method", agglomerationMethod[j]);
}
}
// Performing the calculations
rConnection.eval("dataframe1 <- data.frame(x,y)");
rConnection.eval("HCluster <- hclust(d = dist(dataframe1), method)");
// option for drawing the hierarchical tree and storing the image
rConnection.assign(".tmp.", docrootPath + rFolderName + "/Hierarchical_Clustering.jpg");
rConnection.eval("jpeg(.tmp.)");
rConnection.eval("plot(HCluster, main = \"Hierarchical Clustering\")");
rConnection.eval("dev.off()");
// Get the data from R
hclresult.setClusterSequence(rConnection.eval("HCluster$merge").asDoubleMatrix());
hclresult.setClusterMethod(rConnection.eval("HCluster$method").asStrings());
// hclresult.setClusterLabels(rConnection.eval("HCluster$labels").asStrings());
hclresult.setClusterDistanceMeasure(rConnection.eval("HCluster$dist.method").asStrings());
}
catch (Exception e)
{
e.printStackTrace();