org.rosuda.REngine.REngineException Java Examples

The following examples show how to use org.rosuda.REngine.REngineException. 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: RFilesExecutor.java    From Knowage-Server with GNU Affero General Public License v3.0 6 votes vote down vote up
protected String evalFilesNeeded(HashMap paramsFilled) throws IOException, REngineException, REXPMismatchException {
	logger.debug("IN");
	if (re != null && dataminingInstance.getFiles() != null && !dataminingInstance.getFiles().isEmpty()) {
		for (Iterator fIt = dataminingInstance.getFiles().iterator(); fIt.hasNext();) {
			DataMiningFile f = (DataMiningFile) fIt.next();

			// Save file
			String strPathUploadedFile = DataMiningUtils.getUserResourcesPath(profile) + f.getFileName();
			File file = new File(strPathUploadedFile);
			BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(file));
			writer.write(f.getContent());
			writer.flush();
			writer.close();

			// put file path into variable
			String stringToEval = f.getAlias() + "='" + strPathUploadedFile + "'\n";
			re.parseAndEval(stringToEval);

		}
	}
	logger.debug("OUT");
	return "";
}
 
Example #2
Source File: RDatasetsExecutor.java    From Knowage-Server with GNU Affero General Public License v3.0 6 votes vote down vote up
protected void updateDataset(DataMiningDataset ds) throws IOException, REngineException, REXPMismatchException {
	logger.debug("IN");
	File fileDSDir = new File(DataMiningUtils.getUserResourcesPath(profile) + ds.getName());
	// /find file in dir
	File[] dsfiles = fileDSDir.listFiles();
	if (dsfiles != null) {
		String fileDSPath = dsfiles[0].getPath();

		fileDSPath = fileDSPath.replaceAll("\\\\", "/");
		logger.debug("File ds path " + fileDSPath);

		String stringToEval = null;
		if (ds.getSubstituteLabel() != null && ds.getSubstituteLabel() != "") // functionsCatalog executeWithNewData
		{
			stringToEval = ds.getSubstituteLabel() + "<-read." + ds.getReadType() + "(\"" + fileDSPath + "\"," + ds.getOptions() + ");";
		} else // dataminingEngine e functionsCatalog executeDemo
		{
			stringToEval = ds.getName() + "<-read." + ds.getReadType() + "(\"" + fileDSPath + "\"," + ds.getOptions() + ");";

		}
		logger.debug("R code to eval " + stringToEval);
		re.parseAndEval(stringToEval);
	}
	logger.debug("OUT");
}
 
Example #3
Source File: DataMiningRExecutor.java    From Knowage-Server with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Prepare REngine and user workspace environmet
 *
 * @param dataminingInstance
 * @param userProfile
 * @throws IOException
 * @throws REXPMismatchException
 * @throws REngineException
 * @throws NamingException
 */
private void setupEnvonmentForExternal() throws IOException, REngineException, REXPMismatchException, NamingException {
	logger.debug("IN");
	// new R-engine
	re = createREngineInstanceWithWork();
	if (re == null) {
		logger.error("Cannot load R");
		return;
	}
	re.parseAndEval("setwd(\"" + DataMiningUtils.UPLOADED_FILE_PATH + DataMiningConstants.DATA_MINING_EXTERNAL_CODE_PATH + "\")");
	logger.debug("Set working directory");
	// commandsExecutor.setRe(re);
	datasetsExecutor.setRe(re);
	outputExecutor.setRe(re);
	scriptExecutor.setRe(re);
	fileExecutor.setRe(re);

	logger.debug("OUT");
}
 
Example #4
Source File: DataMiningPythonExecutor.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
private void setupEnvonmentForExternal() throws IOException, REngineException, REXPMismatchException, NamingException {
	logger.debug("IN");

	String str = "" + DataMiningUtils.UPLOADED_FILE_PATH + DataMiningConstants.DATA_MINING_EXTERNAL_CODE_PATH;
	if (!PyLib.isPythonRunning()) {
		PyLib.startPython();
	}
	PyLib.execScript("import os\n" + "os.chdir(r'" + str + "')\n");

	logger.debug("Set working directory");
	logger.debug("OUT");
}
 
Example #5
Source File: RDatasetsExecutor.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
protected boolean getAndEvalDefaultDataset(DataMiningDataset ds) throws IOException, REngineException, REXPMismatchException {
	logger.debug("IN");
	// checks relative path
	String relPathToDefDS = ds.getDefaultDS();
	if (relPathToDefDS == null || relPathToDefDS.equals("")) {
		return false;
	}
	if (relPathToDefDS.startsWith("/") || relPathToDefDS.startsWith("\\\\")) {
		relPathToDefDS = relPathToDefDS.substring(1);
	}
	String defDSRelPath = DataMiningUtils.UPLOADED_FILE_PATH + relPathToDefDS;

	defDSRelPath = defDSRelPath.replaceAll("\\\\", "/");
	logger.debug("Default path " + defDSRelPath);
	File fileDSDefault = new File(defDSRelPath);
	if (!fileDSDefault.exists()) {
		logger.debug("Default file doesn't exist");
		return false;
	}
	String stringToEval = ds.getName() + "<-read." + ds.getReadType() + "(\"" + defDSRelPath + "\"," + ds.getOptions() + ");";

	logger.debug("R code to eval " + stringToEval);
	re.parseAndEval(stringToEval);
	logger.debug("OUT");
	return true;

}
 
Example #6
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 #7
Source File: RserveMeanIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
/**
 * Test for {@link RserveMeanIntegrationTest#mean(int[])}.
 * 
 * @throws REXPMismatchException if an error occurs
 * @throws REngineException      if an error occurs
 */
@Test
public void givenValues_whenMean_thenCorrect() throws REngineException, REXPMismatchException {
    int[] input = { 1, 2, 3, 4, 5 };
    double result = rserveMean.mean(input);
    Assert.assertEquals(3.0, result, 0.000001);
}
 
Example #8
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();
}