本文整理汇总了Java中org.rosuda.REngine.REXP.asString方法的典型用法代码示例。如果您正苦于以下问题:Java REXP.asString方法的具体用法?Java REXP.asString怎么用?Java REXP.asString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.rosuda.REngine.REXP
的用法示例。
在下文中一共展示了REXP.asString方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: getString
import org.rosuda.REngine.REXP; //导入方法依赖的package包/类
@Override
public String getString(final String variable) {
try {
final REXP rexp = engine.unwrap().eval(variable);
final boolean[] na = rexp.isNA();
Assertions.checkEquals(na.length, 1);
if (na[0]) {
return null;
} else {
return rexp.asString();
}
} catch (final REXPMismatchException e) {
throw new RuntimeException(e);
}
}
示例4: 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;
}
}
示例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;
}