本文整理汇总了Java中org.rosuda.REngine.REXP.inherits方法的典型用法代码示例。如果您正苦于以下问题:Java REXP.inherits方法的具体用法?Java REXP.inherits怎么用?Java REXP.inherits使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.rosuda.REngine.REXP
的用法示例。
在下文中一共展示了REXP.inherits方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: executeStringFunction
import org.rosuda.REngine.REXP; //导入方法依赖的package包/类
/**
* Executes an R function (return: String)
*
* @param function
* name of the R function
* @param parameters
* array of parameters required by the function
*
* TODO: make more generic - not just for Strings ...
*/
public static String executeStringFunction(RConnection conn, String function, String[] parameters) {
try {
// create request
String request = "try(" + function + "(";
for (String parameter : parameters) {
request += parameter + ",";
}
// remove last ","
request = request.substring(0, request.length() - 1);
request += "))";
// execute function
REXP xp = conn.parseAndEval(request);
if (xp.inherits("try-error")) {
close(conn);
throw new IOException("failed to execute function '" + function + "'; \nrequest: " + request
+ "; \nError: " + xp.asString());
}
String retval = xp.asString();
return retval;
}
catch (Exception e) {
return null;
}
}
示例2: getRServeConnection
import org.rosuda.REngine.REXP; //导入方法依赖的package包/类
/**
* Get an R connection
*
* @param host
* @param workspace
* @return {@link RConnection} rConnection - a connection to RServer
*/
private static RConnection getRServeConnection(String host, String workspace) {
try {
// establish R connection
RConnection conn = new RConnection(host);
// check connection
if (conn == null || !conn.isConnected())
throw new IOException("Failed to establish RServe connection");
// set workspace
if (workspace != null) {
REXP xp = conn.parseAndEval("try(setwd('" + workspace.replace("\\", "\\\\") + "'))");
if (xp.inherits("try-error"))
throw new IOException("Failed to load R workspace; \nError: " + xp.asString());
}
return conn;
}
catch (Exception e) {
return null;
}
}
示例3: executeVoidFunction
import org.rosuda.REngine.REXP; //导入方法依赖的package包/类
/**
* Executes an R function (return: void)
*
* @param function
* name of the R function
* @param Parameters
* array of parameters required by the function
*/
public static boolean executeVoidFunction(RConnection conn, String function, String[] parameters) {
// create request
try {
String request = "try(" + function + "(";
if (parameters.length != 0) {
for (String parameter : parameters) {
request += parameter + ",";
}
// remove last ","
request = request.substring(0, request.length() - 1);
request += "))";
}
// execute function
REXP xp = conn.parseAndEval(request);
if (xp.inherits("try-error")) {
close(conn);
throw new IOException("failed to execute function '" + function + "'; \nrequest: " + request
+ "; \nError: " + xp.asString());
}
return true;
}
catch (Exception e) {
return false;
}
}
示例4: loadRDataWorkspace
import org.rosuda.REngine.REXP; //导入方法依赖的package包/类
/**
* Load RData workspace
*
* @param RData
* Name of the RData file, relative to R workspace
*/
private static boolean loadRDataWorkspace(RConnection conn, String rData) {
// load specified RData file from workspace
try {
REXP xp = conn.parseAndEval("try(load(paste(getwd(),'" + rData + "',sep='/')))");
if (xp.inherits("try-error")) {
throw new IOException("failed to load RData workspace; \nError: " + xp.toString());
}
return true;
}
catch (Exception e) {
return false;
}
}
示例5: close
import org.rosuda.REngine.REXP; //导入方法依赖的package包/类
/**
* Close R connection
*/
private static void close(RConnection conn) {
try {
REXP xp = conn.parseAndEval("gc()");
if (xp.inherits("try-error")) {
throw new IOException("failed to load R workspace; \nError: " + xp.asString());
}
conn.close();
}
catch (Exception e) {
// do nothing
// should not occur
// TODO: ...
}
}
示例6: eval
import org.rosuda.REngine.REXP; //导入方法依赖的package包/类
@Override
public <T> T eval(String command) throws FOSException {
try {
if(logger.isTraceEnabled()) {
logger.trace(command);
}
connection.assign("trycodeblock", command);
REXP result = connection.parseAndEval("try(eval(parse(text=trycodeblock)),silent=TRUE)");
if (result != null && result.inherits("try-error")) {
throw new FOSException(result.toDebugString());
}
if (result == null || result.isNull()) {
return null;
} else if( result.isVector() && result.isNumeric()) {
return (T) result.asDoubles();
} else if (result.isInteger()) {
return (T) new Integer(result.asInteger());
} else if (result.isNumeric()) {
return (T) new Double(result.asDouble());
} else if (result.isString()) {
return (T) result.asString();
}
return null;
} catch (Exception e) {
throw new FOSException("Error executing R script.", e);
}
}
示例7: tryEval
import org.rosuda.REngine.REXP; //导入方法依赖的package包/类
public REXP tryEval(RConnection c, String s) throws RserveException,
RException, REXPMismatchException {
// silent: logical: should the report of error messages be suppressed
REXP r = c.eval("try({" + s + "}, silent=TRUE)");
if (r.inherits("try-error")) throw new RException(r.asString());
return r;
}
示例8: loadPackage
import org.rosuda.REngine.REXP; //导入方法依赖的package包/类
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);
// }
}
}
示例9: getStream
import org.rosuda.REngine.REXP; //导入方法依赖的package包/类
public InputStream getStream() {
try {
semaphore.acquire();
// System.out.println("Semaphore acquired");
// System.out.flush();
/* The R Cairo package is used for high-quality graphics */
boolean CairoOK = rc.parseAndEval(
"suppressWarnings(require('Cairo',quietly=TRUE))")
.asInteger() > 0;
if (!CairoOK) {
System.err.println("RVaadin: Could not find Cairo package");
rc.parseAndEval("cat('RVaadin: Error: "
+ "Could not find Cairo package.\n')");
}
/*
* Observe the usage of R's try and error mechanism in Java, and
* remember that device == 'png', 'pdf', ...
*/
REXP xp = rc.parseAndEval("try(Cairo" + device.toUpperCase()
+ "('tmp." + device + "', " + "width=" + width
+ ", height=" + height + "))");
if (xp.inherits("try-error")) {
System.err.println("Can't open " + device
+ " graphics device:\n" + xp.asString());
/* Plot the first warning into the error console */
REXP w = rc.eval("if (exists('last.warning') && "
+ "length(last.warning)>0) "
+ "names(last.warning)[1] else 0");
if (w.isString()) {
System.err.println(w.asString());
}
}
/*
* Now, state the actual plot command and then close the Cairo
* device to produce the graphics
*/
rc.parseAndEval(RPlotCall);
rc.parseAndEval("dev.off()");
/*
* The author of RServe claims that there is no I/O API for REngine
* as it's actually more efficient to use R.
*/
String fileName = "tmp." + device;
xp = rc.parseAndEval("r <- readBin('" + fileName
+ "','raw', file.info('" + fileName + "')$size); "
+ "unlink('" + fileName + "'); r");
return new ByteArrayInputStream(xp.asBytes());
} catch (RserveException rse) {
/* RserveException (transport layer - e.g. Rserve is not running */
System.out.println(rse);
return null;
} catch (REXPMismatchException mme) {
/* REXP mismatch exception (we got something else as expected) */
System.out.println(mme);
mme.printStackTrace();
return null;
} catch (Exception e) {
/* something else */
System.err.println("Errors in RPLotCall: " + e.getMessage());
e.printStackTrace();
return null;
} finally {
semaphore.release();
// System.out.println("Semaphore released");
// System.out.flush();
}
}