Java Code Examples for org.rosuda.REngine.REXP#asInteger()

The following examples show how to use org.rosuda.REngine.REXP#asInteger() . 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: Rsession.java    From mzmine3 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Check for package loaded in R environment.
 * 
 * @param pack R package name
 * @return package loading status
 */
public boolean isPackageLoaded(String pack) {
  silentlyVoidEval(loadedpacks + "<-.packages()", false);
  boolean isloaded = false;
  try {
    REXP i = silentlyEval("is.element(set=" + loadedpacks + ",el='" + pack + "')");
    if (i != null) {
      isloaded = i.asInteger() == 1;
    }
  } catch (REXPMismatchException ex) {
    log(HEAD_ERROR + ex.getMessage() + "\n  isPackageLoaded(String pack=" + pack + ")",
        Level.ERROR);
  }
  if (isloaded) {
    log(_PACKAGE_ + pack + " is loaded.", Level.INFO);
  } else {
    log(_PACKAGE_ + pack + " is not loaded.", Level.INFO);
  }

  // silentlyEval("rm(" + loadedpacks + ")");
  return isloaded;
}
 
Example 2
Source File: Rsession.java    From mzmine3 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 
 * @param robject R object name
 * @return R type of object
 */
public String typeOf(String robject) {
  if (robject == null) {
    return "NULL";
  }
  for (String t : types) {
    REXP is = silentlyEval("is." + t + "(" + robject + ")");
    try {
      if (is != null && is.asInteger() == 1) {
        return t;
      }
    } catch (REXPMismatchException ex) {
      log(HEAD_ERROR + "[typeOf] " + robject + " type unknown.", Level.ERROR);
      return null;
    }
  }
  return "unknown";
}
 
Example 3
Source File: Rsession.java    From mzmine2 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Check for package loaded in R environment.
 * 
 * @param pack R package name
 * @return package loading status
 */
public boolean isPackageLoaded(String pack) {
  silentlyVoidEval(loadedpacks + "<-.packages()", false);
  boolean isloaded = false;
  try {
    REXP i = silentlyEval("is.element(set=" + loadedpacks + ",el='" + pack + "')");
    if (i != null) {
      isloaded = i.asInteger() == 1;
    }
  } catch (REXPMismatchException ex) {
    log(HEAD_ERROR + ex.getMessage() + "\n  isPackageLoaded(String pack=" + pack + ")",
        Level.ERROR);
  }
  if (isloaded) {
    log(_PACKAGE_ + pack + " is loaded.", Level.INFO);
  } else {
    log(_PACKAGE_ + pack + " is not loaded.", Level.INFO);
  }

  // silentlyEval("rm(" + loadedpacks + ")");
  return isloaded;
}
 
Example 4
Source File: Rsession.java    From mzmine2 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 
 * @param robject R object name
 * @return R type of object
 */
public String typeOf(String robject) {
  if (robject == null) {
    return "NULL";
  }
  for (String t : types) {
    REXP is = silentlyEval("is." + t + "(" + robject + ")");
    try {
      if (is != null && is.asInteger() == 1) {
        return t;
      }
    } catch (REXPMismatchException ex) {
      log(HEAD_ERROR + "[typeOf] " + robject + " type unknown.", Level.ERROR);
      return null;
    }
  }
  return "unknown";
}
 
Example 5
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 6
Source File: Rsession.java    From mzmine3 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * cast R object in java object
 * 
 * @param eval REXP R object
 * @return java object
 * @throws org.rosuda.REngine.REXPMismatchException
 */
public static Object cast(REXP eval) throws REXPMismatchException {
  if (eval == null) {
    return null;
  }

  /*
   * int[] dim = eval.dim(); String dims = "["; if (dim == null) { dims = "NULL"; } else { for
   * (int i : dim) { dims += (i + " "); } dims += "]"; }
   * 
   * System.out.println(eval.toString() + "\n  isComplex=     " + (eval.isComplex() ? "TRUE" :
   * "    false") + "\n  isEnvironment= " + (eval.isEnvironment() ? "TRUE" : "    false") +
   * "\n  isExpression=  " + (eval.isExpression() ? "TRUE" : "    false") + "\n  isFactor=      "
   * + (eval.isFactor() ? "TRUE" : "    false") + "\n  isFactor=      " + (eval.isFactor() ?
   * "TRUE" : "    false") + "\n  isInteger=     " + (eval.isInteger() ? "TRUE" : "    false") +
   * "\n  isLanguage=    " + (eval.isLanguage() ? "TRUE" : "    false") + "\n  isList=        " +
   * (eval.isList() ? "TRUE" : "    false") + "\n  isLogical=     " + (eval.isLogical() ? "TRUE" :
   * "    false") + "\n  isNull=        " + (eval.isNull() ? "TRUE" : "    false") +
   * "\n  isNumeric=     " + (eval.isNumeric() ? "TRUE" : "    false") + "\n  isRaw=         " +
   * (eval.isRaw() ? "TRUE" : "    false") + "\n  isRecursive=   " + (eval.isRecursive() ? "TRUE"
   * : "    false") + "\n  isString=      " + (eval.isString() ? "TRUE" : "    false") +
   * "\n  isSymbol=      " + (eval.isSymbol() ? "TRUE" : "    false") + "\n  isVector=      " +
   * (eval.isVector() ? "TRUE" : "    false") + "\n  length=  " + (eval.length()) + "\n  dim=  " +
   * dims);
   */
  if (eval.isNumeric()) {
    if (eval.dim() == null || eval.dim().length == 1) {
      double[] array = eval.asDoubles();
      if (array.length == 0) {
        return null;
      }
      if (array.length == 1) {
        return array[0];
      }
      return array;
    } else {
      // System.err.println("eval.dim()="+eval.dim()+"="+cat(eval.dim()));
      double[][] mat = eval.asDoubleMatrix();
      if (mat.length == 0) {
        return null;
      } else if (mat.length == 1) {
        if (mat[0].length == 0) {
          return null;
        } else if (mat[0].length == 1) {
          return mat[0][0];
        } else {
          return mat[0];
        }
      } else {
        if (mat[0].length == 0) {
          return null;
        } else if (mat[0].length == 1) {
          double[] dmat = new double[mat.length];
          for (int i = 0; i < dmat.length; i++) {
            dmat[i] = mat[i][0];
          }
          return dmat;
        } else {
          return mat;
        }
      }
    }
  }

  if (eval.isString()) {
    String[] s = eval.asStrings();
    if (s.length == 1) {
      return s[0];
    } else {
      return s;
    }
  }

  if (eval.isLogical()) {
    return eval.asInteger() == 1;
  }

  if (eval.isList()) {
    return eval.asList();
  }

  if (eval.isNull()) {
    return null;
  } else {
    System.err.println("Unsupported type: " + eval.toDebugString());
  }
  return eval.toString();
}
 
Example 7
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 8
Source File: Rsession.java    From mzmine2 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * cast R object in java object
 * 
 * @param eval REXP R object
 * @return java object
 * @throws org.rosuda.REngine.REXPMismatchException
 */
public static Object cast(REXP eval) throws REXPMismatchException {
  if (eval == null) {
    return null;
  }

  /*
   * int[] dim = eval.dim(); String dims = "["; if (dim == null) { dims = "NULL"; } else { for
   * (int i : dim) { dims += (i + " "); } dims += "]"; }
   * 
   * System.out.println(eval.toString() + "\n  isComplex=     " + (eval.isComplex() ? "TRUE" :
   * "    false") + "\n  isEnvironment= " + (eval.isEnvironment() ? "TRUE" : "    false") +
   * "\n  isExpression=  " + (eval.isExpression() ? "TRUE" : "    false") + "\n  isFactor=      "
   * + (eval.isFactor() ? "TRUE" : "    false") + "\n  isFactor=      " + (eval.isFactor() ?
   * "TRUE" : "    false") + "\n  isInteger=     " + (eval.isInteger() ? "TRUE" : "    false") +
   * "\n  isLanguage=    " + (eval.isLanguage() ? "TRUE" : "    false") + "\n  isList=        " +
   * (eval.isList() ? "TRUE" : "    false") + "\n  isLogical=     " + (eval.isLogical() ? "TRUE" :
   * "    false") + "\n  isNull=        " + (eval.isNull() ? "TRUE" : "    false") +
   * "\n  isNumeric=     " + (eval.isNumeric() ? "TRUE" : "    false") + "\n  isRaw=         " +
   * (eval.isRaw() ? "TRUE" : "    false") + "\n  isRecursive=   " + (eval.isRecursive() ? "TRUE"
   * : "    false") + "\n  isString=      " + (eval.isString() ? "TRUE" : "    false") +
   * "\n  isSymbol=      " + (eval.isSymbol() ? "TRUE" : "    false") + "\n  isVector=      " +
   * (eval.isVector() ? "TRUE" : "    false") + "\n  length=  " + (eval.length()) + "\n  dim=  " +
   * dims);
   */
  if (eval.isNumeric()) {
    if (eval.dim() == null || eval.dim().length == 1) {
      double[] array = eval.asDoubles();
      if (array.length == 0) {
        return null;
      }
      if (array.length == 1) {
        return array[0];
      }
      return array;
    } else {
      // System.err.println("eval.dim()="+eval.dim()+"="+cat(eval.dim()));
      double[][] mat = eval.asDoubleMatrix();
      if (mat.length == 0) {
        return null;
      } else if (mat.length == 1) {
        if (mat[0].length == 0) {
          return null;
        } else if (mat[0].length == 1) {
          return mat[0][0];
        } else {
          return mat[0];
        }
      } else {
        if (mat[0].length == 0) {
          return null;
        } else if (mat[0].length == 1) {
          double[] dmat = new double[mat.length];
          for (int i = 0; i < dmat.length; i++) {
            dmat[i] = mat[i][0];
          }
          return dmat;
        } else {
          return mat;
        }
      }
    }
  }

  if (eval.isString()) {
    String[] s = eval.asStrings();
    if (s.length == 1) {
      return s[0];
    } else {
      return s;
    }
  }

  if (eval.isLogical()) {
    return eval.asInteger() == 1;
  }

  if (eval.isList()) {
    return eval.asList();
  }

  if (eval.isNull()) {
    return null;
  } else {
    System.err.println("Unsupported type: " + eval.toDebugString());
  }
  return eval.toString();
}