• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Java REXP类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Java中org.rosuda.REngine.REXP的典型用法代码示例。如果您正苦于以下问题:Java REXP类的具体用法?Java REXP怎么用?Java REXP使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



REXP类属于org.rosuda.REngine包,在下文中一共展示了REXP类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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;
    }
}
 
开发者ID:mzmine,项目名称:mzmine2,代码行数:20,代码来源:Rsession.java


示例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;
    }
}
 
开发者ID:52North,项目名称:movingcode,代码行数:36,代码来源:RServerController.java


示例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;
}
 
开发者ID:subes,项目名称:invesdwin-context-r,代码行数:24,代码来源:RserveScriptTaskResultsR.java


示例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;
}
 
开发者ID:subes,项目名称:invesdwin-context-r,代码行数:24,代码来源:RserveScriptTaskResultsR.java


示例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);
    }
}
 
开发者ID:subes,项目名称:invesdwin-context-r,代码行数:19,代码来源:RserveScriptTaskResultsR.java


示例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);
    }
}
 
开发者ID:subes,项目名称:invesdwin-context-r,代码行数:24,代码来源:RserveScriptTaskResultsR.java


示例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;
}
 
开发者ID:pengchengluo,项目名称:Peking-University-Open-Research-Data-Platform,代码行数:22,代码来源:RRequest.java


示例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;
}
 
开发者ID:mzmine,项目名称:mzmine2,代码行数:31,代码来源:Rsession.java


示例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;
}
 
开发者ID:mzmine,项目名称:mzmine2,代码行数:27,代码来源:Rsession.java


示例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";
}
 
开发者ID:mzmine,项目名称:mzmine2,代码行数:25,代码来源:Rsession.java


示例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;
}
 
开发者ID:mzmine,项目名称:mzmine2,代码行数:30,代码来源:Rsession.java


示例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;
    }
}
 
开发者ID:52North,项目名称:movingcode,代码行数:29,代码来源:RServerController.java


示例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);
	}
}
 
开发者ID:jfcorugedo,项目名称:RJavaServer,代码行数:23,代码来源:JRIEngineProviderService.java


示例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);
	}
}
 
开发者ID:jfcorugedo,项目名称:RJavaServer,代码行数:23,代码来源:JRIEngineProviderService.java


示例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);
	}
}
 
开发者ID:jfcorugedo,项目名称:RJavaServer,代码行数:26,代码来源:RServeEngineProviderService.java


示例16: 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(format("\"values%d\",", i));
		data.setKeyAt(i+1, format("values%d",i));
	}
	
	REngine engine = null;
	String variableNames = valueNames.substring(0, valueNames.length()-1);
	try {
		engine = rConnectionFactory.getConnection();
		engine.assign("data", REXP.createDataFrame(data));
		return engine.parseAndEval(format("blockDiscreteFunction(data,c(\"ids\"),c(%s))", variableNames));
	}catch(Exception e) {
		throw new REngineException("Unexpected error while executing blockDiscreteFunction", e);
	} finally {
		rConnectionFactory.releaseConnection(engine);
	}
}
 
开发者ID:jfcorugedo,项目名称:RJavaServer,代码行数:25,代码来源:RServeEngineProviderService.java


示例17: groupMultipleValues

import org.rosuda.REngine.REXP; //导入依赖的package包/类
@Override
public List<int[]> groupMultipleValues(List<String[]> discreteValues, List<double[]> continuousValues) {
	
	REXPInteger rIds = new REXPInteger(generateIds(discreteValues.get(0).length));
	List<REXPString> discreteNativeValues = discreteValues.stream()
													.map(values -> new REXPString(values))
													.collect(Collectors.toList()); 
	List<REXPDouble> continuousNativeValues = continuousValues.stream()
														.map(values -> new REXPDouble(values))
														.collect(Collectors.toList());
	
	REXP result = engineProvider.blockGeneralFunction(rIds, discreteNativeValues, continuousNativeValues);
			
	if(LOGGER.isDebugEnabled()) {
		LOGGER.debug("blockGeneralFunction:\n{}", blockResultToString(result));
	}
	return formatResult(result);
}
 
开发者ID:jfcorugedo,项目名称:RJavaServer,代码行数:19,代码来源:RServiceImpl.java


示例18: formatResult

import org.rosuda.REngine.REXP; //导入依赖的package包/类
/**
 * Format the results from Rserve objects to java standard objects.
 * If the results are odd, the last element of the list will contain only one number
 * @param result
 * @return
 */
private List<int[]> formatResult(REXP result) {
	REXPGenericVector vector = (REXPGenericVector) result;
	
	Integer[] unit1 = getNumbers((REXP)vector.asList().get(0));
	Integer[] unit2 = getNumbers((REXP)vector.asList().get(1));
	
	List<int[]> resultFormated = new ArrayList<>(unit1.length);
	for(int index = 0 ; index < unit2.length -1 ; index++) {
		resultFormated.add(new int[]{unit1[index], unit2[index]});
	}
	if(unit2[unit2.length - 1] == null){
		resultFormated.add(new int[]{unit1[unit2.length - 1]});
	} else {
		resultFormated.add(new int[]{unit1[unit2.length - 1], unit2[unit2.length - 1]});
	}
	
	return resultFormated;
}
 
开发者ID:jfcorugedo,项目名称:RJavaServer,代码行数:25,代码来源:RServiceImpl.java


示例19: getNumbers

import org.rosuda.REngine.REXP; //导入依赖的package包/类
private Integer[] getNumbers(REXP ids) {
	if(ids.isString()) {
		return Arrays.stream(((REXPString)ids).asStrings())//NOSONAR: This stream doesn't need to be closed
					.map(
							id -> {
								if(id != null) {
									return Integer.valueOf(id);
								} else {
									return null;
								}
							}
					).toArray(Integer[]::new);
	} else {
		int[] idsAsArray = ((REXPInteger)ids).asIntegers();
		return (Integer[])Arrays.stream(idsAsArray).boxed().toArray(Integer[]::new);//NOSONAR: This stream doesn't need to be closed
	}
}
 
开发者ID:jfcorugedo,项目名称:RJavaServer,代码行数:18,代码来源:RServiceImpl.java


示例20: testAssignDataFrame

import org.rosuda.REngine.REXP; //导入依赖的package包/类
@Test
public void testAssignDataFrame() throws Exception{
	REXPDouble rIds = new REXPDouble(new double[]{1d,2d});
	REXPDouble rValues = new REXPDouble(new double[]{10000d,20000d});
	
	RList data = new RList();
	data.add(rIds);
	data.add(rValues);
	data.setKeyAt(0, "id");
	data.setKeyAt(1, "values");
	
	engine.assign("data", REXP.createDataFrame(data));
	
	REXP result = engine.parseAndEval("blockFunction(data,c(\"id\"),c(\"values\"))");
	
	
	System.out.println(result.toDebugString());
}
 
开发者ID:jfcorugedo,项目名称:RJavaServer,代码行数:19,代码来源:UseREngineInFrontOfJRIEngineIT.java



注:本文中的org.rosuda.REngine.REXP类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java StatementCreatorUtils类代码示例发布时间:2022-05-21
下一篇:
Java DetailsOverviewRowPresenter类代码示例发布时间:2022-05-21
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap