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

The following examples show how to use org.rosuda.REngine.REXP#isNull() . 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: RScriptExecutor.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
private void loadLibrariesFromRLocal(String libraryNames) throws REngineException, REXPMismatchException {
	logger.debug("IN");
	if (this.re != null) {
		re.parseAndEval("installed_packages = rownames(installed.packages())");
		REXP rHome = re.parseAndEval("try(libdir<-paste(R.home(),\"library\", sep=\"/\"))");
		if (!rHome.inherits("try-error") && !rHome.isNull()) {
			if (libraryNames != null && !libraryNames.isEmpty()) {
				setRProxy();
				String[] libs = libraryNames.split(",");
				for (int i = 0; i < libs.length; i++) {
					String lib = libs[i].trim();
					if (!lib.isEmpty()) {
						// check if the library is present in the workspace, if not try to install that package
						REXP libIsPresent = re.parseAndEval("\"" + lib + "\" %in% installed_packages");
						if (libIsPresent.isNull() || libIsPresent.asString().equalsIgnoreCase("false")) {
							// re.parseAndEval("try(install.packages(\"" + lib + "\",destdir=libdir))");
							logger.error("Libray '" + lib + "' is not present. Please, install the library in R before");
						}
						REXP rLibrary = re.parseAndEval("library(" + lib + ",lib.loc=libdir)");
						if (rLibrary.inherits("try-error")) {
							logger.error("Impossible to load library: " + lib);
						}
					}
				}
			}
		}
	}
	logger.debug("OUT");
}
 
Example 2
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 3
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();
}