org.rosuda.REngine.Rserve.RConnection Java Examples

The following examples show how to use org.rosuda.REngine.Rserve.RConnection. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractKiekerRTest.java    From kieker with Apache License 2.0 6 votes vote down vote up
/**
 * Check whether the SystemProperty is set that states, that Kieker R-related tests
 * should be executed and Check whether a connection to the Rserve server can be established.
 */
@Before
public void preCheckForRSysPropertyAndConnection() {
	final String messageWhenPropertyNotSet = "Skipping " + this.getClass() + " because system property " + PROPERTY_NAME_KIEKER_R_TEST + " not true";
	Assume.assumeTrue(messageWhenPropertyNotSet, this.isTestKiekerRTestsSet());

	try {
		final RConnection rConnection = new RConnection();
		Assume.assumeTrue(rConnection.isConnected());
		rConnection.close();
	} catch (final RserveException e) { // thrown on new RConnection();
		if (this.isTestKiekerRTestsSet()) {
			Assert.fail("You chose to execute KiekerRTests, but no connection to Rserve can be established.");
		}
	}
}
 
Example #2
Source File: Rdaemon.java    From mzmine3 with GNU General Public License v2.0 5 votes vote down vote up
public void stop() {
  println("stopping R daemon... " + conf, Level.INFO);
  if (!conf.isLocal()) {
    throw new UnsupportedOperationException(
        "Not authorized to stop a remote R daemon: " + conf.toString());
  }

  try {
    RConnection s = conf.connect();
    if (s == null || !s.isConnected()) {
      println("R daemon already stoped.", Level.INFO);
      return;
    }
    s.shutdown();

  } catch (Exception ex) {
    // ex.printStackTrace();
    println(ex.getMessage(), Level.ERROR);
  }

  /*
   * if (br != null) { try { br.close(); } catch (IOException ex) { ex.printStackTrace();
   * println(ex.getMessage()); } }
   */

  // if (pw != null) {
  // pw.close();
  // }

  // process.destroy();

  println("R daemon stoped.", Level.INFO);

  log = null;
}
 
Example #3
Source File: StartRserve.java    From mzmine3 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * check whether Rserve is currently running (on local machine and default port).
 * 
 * @return <code>true</code> if local Rserve instance is running, <code>false</code> otherwise
 */
public static boolean isRserveRunning() {
  try {
    RConnection c = new RConnection();
    System.err.println("Rserve is running.");
    c.close();
    return true;
  } catch (Exception e) {
    System.err.println("First connect try failed with: " + e.getMessage());
  }
  return false;
}
 
Example #4
Source File: StartRserve.java    From mzmine3 with GNU General Public License v2.0 5 votes vote down vote up
/** just a demo main method which starts Rserve and shuts it down again */
public static void main(String[] args) {
  System.err.println("result=" + checkLocalRserve());
  try {
    RConnection c = new RConnection();
    c.shutdown();
  } catch (Exception x) {
  }
}
 
Example #5
Source File: Rdaemon.java    From mzmine2 with GNU General Public License v2.0 5 votes vote down vote up
public void stop() {
  println("stopping R daemon... " + conf, Level.INFO);
  if (!conf.isLocal()) {
    throw new UnsupportedOperationException(
        "Not authorized to stop a remote R daemon: " + conf.toString());
  }

  try {
    RConnection s = conf.connect();
    if (s == null || !s.isConnected()) {
      println("R daemon already stoped.", Level.INFO);
      return;
    }
    s.shutdown();

  } catch (Exception ex) {
    // ex.printStackTrace();
    println(ex.getMessage(), Level.ERROR);
  }

  /*
   * if (br != null) { try { br.close(); } catch (IOException ex) { ex.printStackTrace();
   * println(ex.getMessage()); } }
   */

  // if (pw != null) {
  // pw.close();
  // }

  // process.destroy();

  println("R daemon stoped.", Level.INFO);

  log = null;
}
 
Example #6
Source File: StartRserve.java    From mzmine2 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * check whether Rserve is currently running (on local machine and default port).
 * 
 * @return <code>true</code> if local Rserve instance is running, <code>false</code> otherwise
 */
public static boolean isRserveRunning() {
  try {
    RConnection c = new RConnection();
    System.err.println("Rserve is running.");
    c.close();
    return true;
  } catch (Exception e) {
    System.err.println("First connect try failed with: " + e.getMessage());
  }
  return false;
}
 
Example #7
Source File: StartRserve.java    From mzmine2 with GNU General Public License v2.0 5 votes vote down vote up
/** just a demo main method which starts Rserve and shuts it down again */
public static void main(String[] args) {
  System.err.println("result=" + checkLocalRserve());
  try {
    RConnection c = new RConnection();
    c.shutdown();
  } catch (Exception x) {
  }
}
 
Example #8
Source File: RSessionWrapper.java    From mzmine3 with GNU General Public License v2.0 4 votes vote down vote up
public void loadPackage(String packageName) throws RSessionWrapperException {

    String loadCode = "library(" + packageName + ", logical.return = TRUE)";

    if (TRY_MODE)
      loadCode = "try(" + loadCode + ", silent=TRUE)";

    String errorMsg = "The \"" + this.callerFeatureName + "\" requires " + "the \"" + packageName
        + "\" R package, which couldn't be loaded - is it installed in R?";

    if (this.rEngineType == REngineType.RSERVE) {

      if (this.session != null && !this.userCanceled) {
        logger.log(logLvl, "Loading package '" + packageName + "'...");
        int loaded = 0;
        try {

          REXP r = ((RConnection) this.rEngine).eval(loadCode);
          if (r.inherits("try-error")) {
            logger.severe("R Error [0]: " + r.asString());
            logger.severe("R eval attempt [0]: " + loadCode);
          }
          loaded = r.asInteger();
          logger.log(logLvl, "Load status: '" + (loaded != 0) + "'.");

        } catch (RserveException | REXPMismatchException e) {
          logger.log(logLvl, "Loaded package KO: '" + e.getMessage() + "'.");
          // Remain silent if eval KO ("server down").
          loaded = Integer.MIN_VALUE;
        }

        // Throw loading failure only if eval OK, but return FALSE
        // (package not loaded).
        // ("server down" case will be handled soon enough).
        if (loaded == 0)
          if (!this.userCanceled)
            throw new RSessionWrapperException(errorMsg);

        logger.log(logLvl, "Loaded package: '" + packageName + "'.");
      }

    } else { // RCaller

      ((RCaller) rEngine).getRCode().addRCode(loadCode);
      // try {
      //
      // ((RCallerScriptEngine2) rEngine).eval(loadCode);
      // } catch (ScriptException e) {
      //
      // if (!this.userCanceled)
      // throw new RSessionWrapperException(errorMsg);
      // }

    }
  }
 
Example #9
Source File: RSessionWrapper.java    From mzmine2 with GNU General Public License v2.0 4 votes vote down vote up
public void loadPackage(String packageName) throws RSessionWrapperException {

    String loadCode = "library(" + packageName + ", logical.return = TRUE)";

    if (TRY_MODE)
      loadCode = "try(" + loadCode + ", silent=TRUE)";

    String errorMsg = "The \"" + this.callerFeatureName + "\" requires " + "the \"" + packageName
        + "\" R package, which couldn't be loaded - is it installed in R?";

    if (this.rEngineType == REngineType.RSERVE) {

      if (this.session != null && !this.userCanceled) {
        LOG.log(logLvl, "Loading package '" + packageName + "'...");
        int loaded = 0;
        try {

          REXP r = ((RConnection) this.rEngine).eval(loadCode);
          if (r.inherits("try-error")) {
            LOG.severe("R Error [0]: " + r.asString());
            LOG.severe("R eval attempt [0]: " + loadCode);
          }
          loaded = r.asInteger();
          LOG.log(logLvl, "Load status: '" + (loaded != 0) + "'.");

        } catch (RserveException | REXPMismatchException e) {
          LOG.log(logLvl, "Loaded package KO: '" + e.getMessage() + "'.");
          // Remain silent if eval KO ("server down").
          loaded = Integer.MIN_VALUE;
        }

        // Throw loading failure only if eval OK, but return FALSE
        // (package not loaded).
        // ("server down" case will be handled soon enough).
        if (loaded == 0)
          if (!this.userCanceled)
            throw new RSessionWrapperException(errorMsg);

        LOG.log(logLvl, "Loaded package: '" + packageName + "'.");
      }

    } else { // RCaller

      ((RCaller) rEngine).getRCode().addRCode(loadCode);
      // try {
      //
      // ((RCallerScriptEngine2) rEngine).eval(loadCode);
      // } catch (ScriptException e) {
      //
      // if (!this.userCanceled)
      // throw new RSessionWrapperException(errorMsg);
      // }

    }
  }
 
Example #10
Source File: RserveMean.java    From tutorials with MIT License 3 votes vote down vote up
/**
 * Connects to the Rserve istance listening on 127.0.0.1:6311 and invokes the
 * customMean R function passing the given values as arguments.
 * 
 * @param values the input to the mean script
 * @return the result of the R script
 * @throws REngineException      if any error occurs
 * @throws REXPMismatchException if any error occurs
 */
public double mean(int[] values) throws REngineException, REXPMismatchException {
    RConnection c = new RConnection();
    c.assign("input", values);
    return c.eval("customMean(input)")
        .asDouble();
}
 
Example #11
Source File: RSessionWrapper.java    From mzmine3 with GNU General Public License v2.0 2 votes vote down vote up
private void checkConnectivity() throws RSessionWrapperException {

    checkConnectivity(((RConnection) this.rEngine));
  }
 
Example #12
Source File: RSessionWrapper.java    From mzmine2 with GNU General Public License v2.0 2 votes vote down vote up
private void checkConnectivity() throws RSessionWrapperException {

    checkConnectivity(((RConnection) this.rEngine));
  }