/** * * @param variable * variable to R * @throws InvalidREvaluationResultException */ public void toTS(final String variable) throws InvalidREvaluationResultException { if (variable != null) { final StringBuffer buf = new StringBuffer(); buf.append(variable); buf.append(" <- ts("); buf.append(variable); buf.append(')'); this.evalWithR(buf.toString()); } }
/** * * @param variable * variable to R * @throws InvalidREvaluationResultException */ public void toTS(final String variable) throws InvalidREvaluationResultException { if (variable != null) { final StringBuffer buf = new StringBuffer(); buf.append(variable) .append(" <- ts(") .append(variable) .append(')'); this.evalWithR(buf.toString()); } }
/** * * @param variable * variable to R * @param frequency * frequency to R * @throws InvalidREvaluationResultException */ public void toTS(final String variable, final long frequency) throws InvalidREvaluationResultException { if (variable != null) { final StringBuffer buf = new StringBuffer(21); buf.append(variable) .append(" <- ts(") .append(variable) .append(", frequency=") .append(frequency) .append(')'); this.evalWithR(buf.toString()); } }
/** * * @param variable * variable to R * @param frequency * frequency to R * @throws InvalidREvaluationResultException */ public void toTS(final String variable, final long frequency) throws InvalidREvaluationResultException { if (variable != null) { final StringBuffer buf = new StringBuffer(21); buf.append(variable); buf.append(" <- ts("); buf.append(variable); buf.append(", frequency="); buf.append(frequency); buf.append(')'); this.evalWithR(buf.toString()); } }
/** * * @param variable * string * * @param values * assign value * @throws InvalidREvaluationResultException */ public void assign(final String variable, final double[] values) throws InvalidREvaluationResultException { final StringBuffer buf = new StringBuffer(); buf.append(variable); buf.append(" <- c("); boolean first = true; for (final double item : values) { if (!first) { buf.append(','); } else { first = false; } buf.append(item); } buf.append(')'); this.evalWithR(buf.toString()); }
/** * * @param variable * string * @param values * assign vaules * @throws InvalidREvaluationResultException */ public void assign(final String variable, final Long[] values) throws InvalidREvaluationResultException { final StringBuffer buf = new StringBuffer(); buf.append(variable) .append(" <- c("); boolean first = true; for (final Long item : values) { if (!first) { buf.append(','); } else { first = false; } buf.append(item); } buf.append(','); this.evalWithR(buf.toString()); }
/** * * @param variable * string * @param values * assign vaules * @throws InvalidREvaluationResultException */ public void assign(final String variable, final Long[] values) throws InvalidREvaluationResultException { final StringBuffer buf = new StringBuffer(); buf.append(variable); buf.append(" <- c("); boolean first = true; for (final Long item : values) { if (!first) { buf.append(','); } else { first = false; } buf.append(item); } buf.append(','); this.evalWithR(buf.toString()); }
/** * * @param variable * string * * @param values * assign value * @throws InvalidREvaluationResultException */ public void assign(final String variable, final double[] values) throws InvalidREvaluationResultException { final StringBuffer buf = new StringBuffer(); buf.append(variable) .append(" <- c("); boolean first = true; for (final double item : values) { if (!first) { buf.append(','); } else { first = false; } buf.append(item); } buf.append(')'); this.evalWithR(buf.toString()); }
/** * * @param variable * string * @param values * assign vaules * @throws InvalidREvaluationResultException */ public void assign(final String variable, final Double[] values) throws InvalidREvaluationResultException { final StringBuffer buf = new StringBuffer(); buf.append(variable) .append(" <- c("); boolean first = true; for (final Double item : values) { if (!first) { buf.append(','); } else { first = false; } if ((null == item) || item.isNaN()) { buf.append("NA"); } else { buf.append(item); } } buf.append(')'); this.evalWithR(buf.toString()); }
/** * * @param variable * string * @param values * assign vaules * @throws InvalidREvaluationResultException */ public void assign(final String variable, final Double[] values) throws InvalidREvaluationResultException { final StringBuffer buf = new StringBuffer(); buf.append(variable); buf.append(" <- c("); boolean first = true; for (final Double item : values) { if (!first) { buf.append(','); } else { first = false; } if ((null == item) || item.isNaN()) { buf.append("NA"); } else { buf.append(item); } } buf.append(')'); this.evalWithR(buf.toString()); }
/** * * @param input * inputstring * @return Rdata */ public String eString(final String input) { final String resultOnFailure = ""; try { final Object evaluationResult = this.evalWithR(input); final REXPString stringResult = (REXPString) evaluationResult; return stringResult.toString(); } catch (final InvalidREvaluationResultException exc) { RBridgeControl.LOGGER.error(exc.getMessage(), exc); return resultOnFailure; } }
/** * * @param input * inputstring * @return Rdata */ public double[] eDblArr(final String input) { final double[] resultOnFailure = new double[0]; try { final Object evaluationResult = this.evalWithR(input); final REXPVector vectorResult = (REXPVector) evaluationResult; return vectorResult.asDoubles(); } catch (final REXPMismatchException e) { return resultOnFailure; } catch (final InvalidREvaluationResultException e) { RBridgeControl.LOGGER.error(e.getMessage(), e); return resultOnFailure; } }
/** * * @param input * string * @return {@link Double#NaN} in case of error, else dbvalue */ public double eDbl(final String input) { final double resultOnFailure = Double.NaN; try { final Object evaluationResult = this.evalWithR(input); final REXPDouble doubleResult = (REXPDouble) evaluationResult; return doubleResult.asDouble(); } catch (final REXPMismatchException exc) { RBridgeControl.LOGGER.error("Error casting value from R: {} Cause: {}", input, exc); return resultOnFailure; } catch (final InvalidREvaluationResultException exc) { RBridgeControl.LOGGER.error(exc.getMessage(), exc); return resultOnFailure; } }
/** * * @param input * inputstring * @return Rdata */ public String eString(final String input) { final String resultOnFailure = ""; try { final Object evaluationResult = this.evalWithR(input); final REXPString stringResult = (REXPString) evaluationResult; return stringResult.toString(); } catch (final InvalidREvaluationResultException exc) { RBridgeControl.LOG.error(exc.getMessage(), exc); return resultOnFailure; } }
/** * * @param input * inputstring * @return Rdata */ public double[] eDblArr(final String input) { final double[] resultOnFailure = new double[0]; try { final Object evaluationResult = this.evalWithR(input); final REXPVector vectorResult = (REXPVector) evaluationResult; return vectorResult.asDoubles(); } catch (final REXPMismatchException e) { return resultOnFailure; } catch (final InvalidREvaluationResultException e) { RBridgeControl.LOG.error(e.getMessage(), e); return resultOnFailure; } }
/** * Test of the RBridge, connects to Rserve. * * @throws Exception * If exception is thrown */ @Test public void test() throws Exception { final RBridgeControl r = RBridgeControl.getInstance(); r.evalWithR("measures <- c(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,31.0,41.0,95.0,77.0,29.0,62.0,49.0,NA)"); r.evalWithR("forecasts <- c(NA,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.8181818181818183," + "6.0,12.846153846153847,17.428571428571427,18.2,20.9375,22.58823529411765)"); r.evalWithR("anomalies <- c(NA,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,0.8333333333333333,0.7446808510638298," + "0.761768901569187,0.6308623298033282,0.2288135593220339,0.49510173323285606,0.3689400164338537,NA)"); final Object result = r.evalWithR("combined <- cbind(measures, forecasts, anomalies)"); LOGGER.info(result.toString()); Assert.assertTrue(result != null); Assert.assertTrue(result instanceof org.rosuda.REngine.REXPDouble); }
/** * Test to make sure that InvalidREvaluationResultException within the evalWithR() method are caught correctly. * * @throws InvalidREvaluationResultException * if exceptions are handled correctly. */ @Test(expected = InvalidREvaluationResultException.class) public void testNullPointerEvaluationEvalWithR() { final RBridgeControl r = RBridgeControl.getInstance(); r.evalWithR("accuracy(NULL)[6]"); } }
/** * * @param input * string * @return {@link Double#NaN} in case of error, else dbvalue */ public double eDbl(final String input) { final double resultOnFailure = Double.NaN; try { final Object evaluationResult = this.evalWithR(input); final REXPDouble doubleResult = (REXPDouble) evaluationResult; return doubleResult.asDouble(); } catch (final REXPMismatchException exc) { RBridgeControl.LOG.error("Error casting value from R: " + input + " Cause: " + exc); return resultOnFailure; } catch (final InvalidREvaluationResultException exc) { RBridgeControl.LOG.error(exc.getMessage(), exc); return resultOnFailure; } }
AbstractRForecaster.RBRIDGE.evalWithR(String.format("%s <- %s(%s)", varNameModel, this.modelFunc, params)); AbstractRForecaster.RBRIDGE.evalWithR(String.format("rm(%s)", varNameValues)); AbstractRForecaster.RBRIDGE.evalWithR(String.format("%s <- %s(%s, h=%d)", varNameForecast, this.forecastFunc, varNameModel, numForecastSteps)); } else { AbstractRForecaster.RBRIDGE.evalWithR(String.format("%s <- %s(%s, h=%d, level=c(%d))", varNameForecast, this.forecastFunc, varNameModel, numForecastSteps, this.getConfidenceLevel())); AbstractRForecaster.RBRIDGE.evalWithR(String.format("rm(%s)", varNameModel)); AbstractRForecaster.RBRIDGE.evalWithR(String.format("rm(%s)", varNameValues)); AbstractRForecaster.RBRIDGE.evalWithR(String.format("rm(%s)", varNameForecast));
AbstractRForecaster.RBRIDGE.evalWithR(String.format("%s <- %s(%s)", varNameModel, this.modelFunc, params)); AbstractRForecaster.RBRIDGE.evalWithR(String.format("rm(%s)", varNameValues)); AbstractRForecaster.RBRIDGE.evalWithR(String.format("%s <- %s(%s, h=%d)", varNameForecast, this.forecastFunc, varNameModel, numForecastSteps)); } else { AbstractRForecaster.RBRIDGE.evalWithR(String.format("%s <- %s(%s, h=%d, level=c(%d))", varNameForecast, this.forecastFunc, varNameModel, numForecastSteps, this.getConfidenceLevel())); AbstractRForecaster.RBRIDGE.evalWithR(String.format("rm(%s)", varNameModel)); AbstractRForecaster.RBRIDGE.evalWithR(String.format("rm(%s)", varNameValues)); AbstractRForecaster.RBRIDGE.evalWithR(String.format("rm(%s)", varNameForecast));