Ejemplo n.º 1
0
  /**
   * Inits the R engine.
   *
   * @param args The command-line argument list from main.
   */
  private void initREngine(String[] args) {
    out("Creating R engine");

    // start the R engine
    re = new Rengine(args, false, null);
    out("R engine created, waiting for R...");

    // Waits until R is ready for a new thread. If it returns false, R has died.
    if (!re.waitForR()) {
      out("Cannot load R");
      return;
    }

    // loading rJava
    String[] szLib = sendCommand2R("library(rJava)");
    if (szLib != null) {
      for (int cnt = 0; cnt < szLib.length; ++cnt) {
        out(szLib[cnt]);
      }
    }

    // init rJava
    String[] szJinit = sendCommand2R(".jinit()");
    if (szJinit != null) {
      for (int cnt = 0; cnt < szJinit.length; ++cnt) {
        out(szJinit[cnt]);
      }
    }

    // ready for input
    out("READY.");
  }
Ejemplo n.º 2
0
  private static REXP runCommand(String command, boolean convert) {
    boolean obtainedLock = engine.getRsync().safeLock();
    REXP value;

    try {
      value = engine.eval(command, convert);
    } finally {
      if (obtainedLock) engine.getRsync().unlock();
    }
    if (!convert) engine.waitForR();
    return value;
  }
Ejemplo n.º 3
0
  // Constructor for new RController
  private RController() {
    logger = Logger.getInstance();
    if (!Rengine.versionCheck()) {
      logger.logError("** Version mismatch - Java files don't match library version.");
    } else {
      logger.log("Creating Rengine");
      this.re = new Rengine(new String[] {}, false, new TextConsole());
      logger.log("Rengine created, waiting for R");

      // Wait for REngine new thread to finish loading
      if (!re.waitForR()) {
        logger.logError("Cannot load R");
      } else {
        this.hasLoaded = true;
      }
    }
  }
Ejemplo n.º 4
0
 public static void start(String... args) {
   if (!Rengine.versionCheck()) {
     System.err.println("** Version mismatch " + "- Java files don't match library version. **");
     System.exit(1);
   }
   System.out.println("Creating R Engine (with arguments)");
   // 1) we pass the arguments from the command line
   // 2) we won't use the main loop at first, we'll start it later
   //    (that's the "false" as second argument)
   // 3) the callbacks are implemented by the TextConsole class above
   engine = new Rengine(args, false, null);
   // the engine creates R is a new thread, so we should wait until it's ready
   if (!engine.waitForR()) {
     System.out.println("Cannot load R.");
     return;
   }
   engine.eval("{library(rJava);.jinit()}", false);
 }
Ejemplo n.º 5
0
  public static Rengine getEngine() {
    if (engine == null) {

      engine =
          new Rengine(new String[] {"--no-save"}, false, null) {

            @Override
            public REXP eval(String s, boolean convert) {

              return super.eval(s, convert);
            }
          };

      engine.addMainLoopCallbacks(
          new RMainLoopCallbacks() {

            @Override
            public void rBusy(Rengine arg0, int arg1) {
              // TODO Auto-generated method stub

            }

            @Override
            public String rChooseFile(Rengine arg0, int arg1) {
              // TODO Auto-generated method stub
              return null;
            }

            @Override
            public void rFlushConsole(Rengine arg0) {
              // TODO Auto-generated method stub

            }

            @Override
            public void rLoadHistory(Rengine arg0, String arg1) {
              // TODO Auto-generated method stub

            }

            @Override
            public String rReadConsole(Rengine arg0, String arg1, int arg2) {
              logger.info("read console " + arg1 + " arg2 " + arg2);
              return null;
            }

            @Override
            public void rSaveHistory(Rengine arg0, String arg1) {
              // TODO Auto-generated method stub

            }

            @Override
            public void rShowMessage(Rengine arg0, String arg1) {
              // System.out.println("[Rengine message]: "+arg1);

            }

            @Override
            public void rWriteConsole(Rengine arg0, String arg1, int arg2) {
              logger.info("write console 1");
              //	if (arg2==0){
              //	System.out.println("[Rengine write type "+arg2+"]: "+arg1);
              // }

            }
          });

      engine.waitForR();
    }
    return engine;
  }