Examples of RConnection


Examples of org.rosuda.REngine.Rserve.RConnection

      throw new RemoteException("Unable to run computation on zero-length arrays.");
    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();
      throw new RemoteException("Unable to run R Hierarchical Clustering script", e);
    }
    finally
    {
      if (rConnection != null)
        rConnection.close();
    }
    return hclresult;
  }
View Full Code Here

Examples of org.rosuda.REngine.Rserve.RConnection


  //this function does not take in a script from the as3 side for imputation, but the script is built in
  public static RResult[] handlingMissingData(String[] inputNames, Object[][] inputValues, String[] outputNames, boolean showIntermediateResults, boolean showWarnings, boolean completeProcess) throws RemoteException
  {
    RConnection rConnection = null;
    RResult[] mdResult = null;
    try
    {
      rConnection = getRConnection()
      requestScriptAccess(rConnection); // doing this because the eval() call below is not safe
     
      String script= "";
      REXP evalValue;
      String names = "";
     
//      We have to send columns to R and receive them back to be sent once again to R
      for (int i = 0; i < inputNames.length; i++)
      {
        String name = inputNames[i];
        if(names.length() != 0){
          names = names + "," + name;}
        else{
          names = name;
        }
        double[] value = ListUtils.copyDoubleArray(inputValues[i], new double[inputValues[i].length]);
        rConnection.assign(name, value)
      }
     
      evalValue = rConnection.eval("Bind <- cbind(" + names + ")"); // NOT SAFE - bindingInput was built with string concat using user-specified strings
     
      //Built in script
      if(completeProcess = false)
      {
        script = "library(norm) \n pre <- prelim.norm(Bind)";
       
      }
      else
      {
        script = "library(norm) \n pre <- prelim.norm(Bind) \n eeo <- em.norm(pre) \n rngseed(12345) \n" +
        "imputed <- imp.norm(pre, eeo,Bind)";
      }
     
     
         
      evalScript(rConnection, script, showWarnings);
   
      int i = 0;
      int iterationTimes = outputNames.length;
   
      mdResult = new RResult[outputNames.length];
      for (; i < iterationTimes; i++)
      {
        String name;
        // Boolean addedTolist = false;
        if (iterationTimes == outputNames.length + 1){
          name = outputNames[i - 1];
        }
        else{
          name = outputNames[i];
        }
        // Script to get R - output
        evalValue = evalScript(rConnection, name, showWarnings);       
//        System.out.println(evalValue);
        if (evalValue.isVector()){
          if (evalValue instanceof REXPString)
            mdResult[i] = new RResult(name, evalValue.asStrings());
          else if (evalValue instanceof REXPInteger)
            mdResult[i] = new RResult(name, evalValue.asIntegers());
          else if (evalValue instanceof REXPDouble){
            if (evalValue.dim() == null)
              mdResult[i] = new RResult(name, evalValue.asDoubles());
            else
              mdResult[i] = new RResult(name, evalValue.asDoubleMatrix());
          }
          else{
            // if no previous cases were true, return debug String
            mdResult[i] = new RResult(name, evalValue.toDebugString());
          }
        }
        else{
          mdResult[i] = new RResult(name, evalValue.toDebugString());
        }
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
      throw new RemoteException("Unable to run R imputation script", e);
    }
    finally
    {
      if (rConnection != null)
        rConnection.close();
    }
    return mdResult;
  }
View Full Code Here

Examples of org.rosuda.REngine.Rserve.RConnection

    }

    private RConnection getConnection() throws UDFArgumentException {
      if (rconnection == null || !rconnection.isConnected()) {
        try {
          rconnection = new RConnection("127.0.0.1");
        } catch (Exception e) {
          throw new UDFArgumentException(e.toString());
        }
      }
View Full Code Here

Examples of org.rosuda.REngine.Rserve.RConnection

  }
 
  private RConnection getConnection() throws UDFArgumentException {
    if (rconnection == null || !rconnection.isConnected()) {
      try {
        rconnection = new RConnection("127.0.0.1");
      } catch (Exception e) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        e.printStackTrace(new PrintStream(output));
        throw new UDFArgumentException(new String(output.toByteArray()));
      }
View Full Code Here

Examples of org.rosuda.REngine.Rserve.RConnection

    /**
     *
     */
    public RServerConnector() {
        try {
            rConnection = new RConnection();
        } catch (RserveException ex) {
            Logger.getLogger(RServerConnector.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.