本文整理汇总了Java中org.rosuda.REngine.REXP类的典型用法代码示例。如果您正苦于以下问题:Java REXP类的具体用法?Java REXP怎么用?Java REXP使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
REXP类属于org.rosuda.REngine包,在下文中一共展示了REXP类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: listCommands
import org.rosuda.REngine.REXP; //导入依赖的package包/类
/**
* @return available R commands
*/
public String[] listCommands() {
silentlyEval(".keyWords <- function() {n <- length(search());result <- c();for (i in 1:n) {result <- c(result,ls(pos=i,all.names=TRUE))}; result}");
REXP rexp = silentlyEval(".keyWords()");
String as[] = null;
try {
if (rexp != null && (as = rexp.asStrings()) != null) {
return as;
} else {
return null;
}
} catch (REXPMismatchException ex) {
log(HEAD_ERROR + ex.getMessage() + "\n listCommands()",
Level.ERROR);
return null;
}
}
示例2: 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;
}
}
示例3: asStringMatrix
import org.rosuda.REngine.REXP; //导入依赖的package包/类
private String[][] asStringMatrix(final REXP rexp) throws REXPMismatchException {
final String[] ct = rexp.asStrings();
final REXP dim = rexp.getAttribute("dim");
if (dim == null) {
throw new REXPMismatchException(rexp, "matrix (dim attribute missing)");
}
final int[] ds = dim.asIntegers();
if (ds.length != 2) {
throw new REXPMismatchException(rexp, "matrix (wrong dimensionality)");
}
final int m = ds[0], n = ds[1];
final String[][] r = new String[m][n];
// R stores matrices as matrix(c(1,2,3,4),2,2) = col1:(1,2), col2:(3,4)
// we need to copy everything, since we create 2d array from 1d array
int k = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
r[j][i] = ct[k++];
}
}
return r;
}
示例4: asIntegerMatrix
import org.rosuda.REngine.REXP; //导入依赖的package包/类
private int[][] asIntegerMatrix(final REXP rexp) throws REXPMismatchException {
final int[] ct = rexp.asIntegers();
final REXP dim = rexp.getAttribute("dim");
if (dim == null) {
throw new REXPMismatchException(rexp, "matrix (dim attribute missing)");
}
final int[] ds = dim.asIntegers();
if (ds.length != 2) {
throw new REXPMismatchException(rexp, "matrix (wrong dimensionality)");
}
final int m = ds[0], n = ds[1];
final int[][] r = new int[m][n];
// R stores matrices as matrix(c(1,2,3,4),2,2) = col1:(1,2), col2:(3,4)
// we need to copy everything, since we create 2d array from 1d array
int k = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
r[j][i] = ct[k++];
}
}
return r;
}
示例5: getBooleanVector
import org.rosuda.REngine.REXP; //导入依赖的package包/类
@Override
public boolean[] getBooleanVector(final String variable) {
try {
final REXP rexp = engine.unwrap().eval(variable);
if (allIsNa(rexp)) {
return null;
} else {
final int[] ints = rexp.asIntegers();
final boolean[] booleanVector = new boolean[ints.length];
for (int i = 0; i < ints.length; i++) {
booleanVector[i] = ints[i] > 0;
}
return booleanVector;
}
} catch (final REXPMismatchException e) {
throw new RuntimeException(e);
}
}
示例6: getBooleanMatrix
import org.rosuda.REngine.REXP; //导入依赖的package包/类
@Override
public boolean[][] getBooleanMatrix(final String variable) {
try {
final REXP rexp = engine.unwrap().eval(variable);
if (allIsNa(rexp)) {
return null;
} else {
final double[][] matrix = rexp.asDoubleMatrix();
final boolean[][] booleanMatrix = new boolean[matrix.length][];
for (int i = 0; i < matrix.length; i++) {
final double[] vector = matrix[i];
final boolean[] booleanVector = new boolean[vector.length];
for (int j = 0; j < vector.length; j++) {
booleanVector[j] = vector[j] > 0;
}
booleanMatrix[i] = booleanVector;
}
return booleanMatrix;
}
} catch (final REXPMismatchException e) {
throw new RuntimeException(e);
}
}
示例7: eval
import org.rosuda.REngine.REXP; //导入依赖的package包/类
/**
* Evaluate script
* @return R-Expression
*/
public REXP eval () {
REXP result = null;
try {
open();
result = mRC.eval(mScript);
}
catch (RserveException e) {
LOG.warning(String.format("RRequest: %s", e.getMessage()));
}
finally {
close();
}
return result;
}
示例8: isPackageLoaded
import org.rosuda.REngine.REXP; //导入依赖的package包/类
/**
* 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;
}
示例9: eval
import org.rosuda.REngine.REXP; //导入依赖的package包/类
/**
* Launch R command and return value.
*
* @param expression
* R expresison to evaluate
* @param tryEval
* encapsulate command in try() to cacth errors
* @return REXP R expression
*/
public REXP eval(String expression, boolean tryEval) {
// GLG HACK: Less verbosity when no error.
// log(HEAD_EVAL + (tryEval ? HEAD_TRY : "") + expression, Level.INFO);
REXP e = silentlyEval(expression, tryEval);
for (UpdateObjectsListener b : updateObjects) {
b.update();
}
// GLG HACK: Less verbosity when no error.
// if (e != null) {
// log(__ + e.toDebugString(), Level.INFO);
// }
return e;
}
示例10: typeOf
import org.rosuda.REngine.REXP; //导入依赖的package包/类
/**
*
* @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";
}
示例11: set
import org.rosuda.REngine.REXP; //导入依赖的package包/类
/**
* Set R list in R env.
*
* @param varname
* R list name
* @param data
* numeric data in list
* @param names
* names of columns
*/
public boolean set(String varname, double[][] data, String... names) {
RList list = buildRList(data, names);
log(HEAD_SET + varname + " <- " + toString(list), Level.INFO);
try {
synchronized (connection) {
connection.assign(varname, REXP.createDataFrame(list));
}
} catch (REXPMismatchException re) {
log(HEAD_ERROR + " RList " + list.toString()
+ " not convertible as dataframe.", Level.ERROR);
return false;
} catch (RserveException ex) {
log(HEAD_EXCEPTION + ex.getMessage() + "\n set(String varname="
+ varname + ",double[][] data, String... names)",
Level.ERROR);
return false;
}
return true;
}
示例12: 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;
}
}
示例13: blockDiscreteFunction
import org.rosuda.REngine.REXP; //导入依赖的package包/类
@Override
public REXP blockDiscreteFunction(REXPInteger ids, REXPString... values) {
RList data = new RList();
data.add(ids);
data.setKeyAt(0, "ids");
StringBuilder valueNames = new StringBuilder();
for(int i = 0 ; i < values.length ; i++) {
data.add(values[i]);
valueNames.append("\"values"+i+"\",");
data.setKeyAt(i+1, "values"+i);
}
String variableNames = valueNames.substring(0, valueNames.length()-1);
try{
synchronized(engine){
engine.assign("data", REXP.createDataFrame(data));
return engine.parseAndEval("blockDiscreteFunction(data,c(\"ids\"),c(" + variableNames + "))");
}
}catch(Exception e) {
throw new REngineException("Unexpected error while executing blockDiscreteFunction", e);
}
}
示例14: blockFunction
import org.rosuda.REngine.REXP; //导入依赖的package包/类
@Override
public REXP blockFunction(REXPInteger ids, REXPDouble... values) {
RList data = new RList();
data.add(ids);
data.setKeyAt(0, "ids");
StringBuilder valueNames = new StringBuilder();
for(int i = 0 ; i < values.length ; i++) {
data.add(values[i]);
valueNames.append("\"values"+i+"\",");
data.setKeyAt(i+1, "values"+i);
}
try{
synchronized(engine){
engine.assign("data", REXP.createDataFrame(data));
return engine.parseAndEval("blockFunction(data,c(\"ids\"),c("+ valueNames.substring(0, valueNames.length()-1) +"))");
}
}catch(Exception e) {
throw new REngineException("Unexpected error while executing blockFunction", e);
}
}
示例15: blockFunction
import org.rosuda.REngine.REXP; //导入依赖的package包/类
@Override
public REXP blockFunction(REXPInteger ids, REXPDouble... values) {
RList data = new RList();
data.add(ids);
data.setKeyAt(0, "ids");
StringBuilder valueNames = new StringBuilder();
for(int i = 0 ; i < values.length ; i++) {
data.add(values[i]);
valueNames.append(format("\"values%d\",", i));
data.setKeyAt(i+1, format("values%d",i));
}
String variableNames = valueNames.substring(0, valueNames.length()-1);
REngine engine = null;
try {
engine = rConnectionFactory.getConnection();
engine.assign("data", REXP.createDataFrame(data));
return engine.parseAndEval(format("blockFunction(data,c(\"ids\"),c(%s))", variableNames));
}catch(Exception e) {
throw new REngineException("Unexpected error while executing blockFunction", e);
} finally {
rConnectionFactory.releaseConnection(engine);
}
}