diff --git a/libraries-data-2/pom.xml b/libraries-data-2/pom.xml index 0dadcbd8d4..73c5452f77 100644 --- a/libraries-data-2/pom.xml +++ b/libraries-data-2/pom.xml @@ -128,24 +128,21 @@ ${awaitility.version} test - - - org.rosuda.REngine - Rserve - ${rserve.version} - - - - com.github.jbytecode - RCaller - ${rcaller.version} - - - - org.renjin - renjin-script-engine - ${renjin.version} - + + org.rosuda.REngine + Rserve + ${rserve.version} + + + com.github.jbytecode + RCaller + ${rcaller.version} + + + org.renjin + renjin-script-engine + ${renjin.version} + @@ -157,11 +154,11 @@ - - bedatadriven - bedatadriven public repo - https://nexus.bedatadriven.com/content/groups/public/ - + + bedatadriven + bedatadriven public repo + https://nexus.bedatadriven.com/content/groups/public/ + @@ -183,22 +180,22 @@ 1.8.1 - - - - org.apache.maven.plugins - maven-compiler-plugin - - - - com/baeldung/r/FastRMean.java - - - com/baeldung/r/FastRMeanUnitTest.java - - - - - + + + + org.apache.maven.plugins + maven-compiler-plugin + + + + com/baeldung/r/FastRMean.java + + + com/baeldung/r/FastRMeanUnitTest.java + + + + + \ No newline at end of file diff --git a/libraries-data-2/src/main/java/com/baeldung/r/FastRMean.java b/libraries-data-2/src/main/java/com/baeldung/r/FastRMean.java index 52fb2d1506..8348bfa403 100644 --- a/libraries-data-2/src/main/java/com/baeldung/r/FastRMean.java +++ b/libraries-data-2/src/main/java/com/baeldung/r/FastRMean.java @@ -10,19 +10,24 @@ import java.net.URISyntaxException; */ public class FastRMean { - /** - * Invokes the customMean R function passing the given values as arguments. - * - * @param values the input to the mean script - * @return the result of the R script - */ - public double mean(int[] values) { - Context polyglot = Context.newBuilder().allowAllAccess(true).build(); - String meanScriptContent = RUtils.getMeanScriptContent(); - polyglot.eval("R", meanScriptContent); - Value rBindings = polyglot.getBindings("R"); - Value rInput = rBindings.getMember("c").execute(values); - return rBindings.getMember("customMean").execute(rInput).asDouble(); - } + /** + * Invokes the customMean R function passing the given values as arguments. + * + * @param values the input to the mean script + * @return the result of the R script + */ + public double mean(int[] values) { + Context polyglot = Context.newBuilder() + .allowAllAccess(true) + .build(); + String meanScriptContent = RUtils.getMeanScriptContent(); + polyglot.eval("R", meanScriptContent); + Value rBindings = polyglot.getBindings("R"); + Value rInput = rBindings.getMember("c") + .execute(values); + return rBindings.getMember("customMean") + .execute(rInput) + .asDouble(); + } } \ No newline at end of file diff --git a/libraries-data-2/src/main/java/com/baeldung/r/RCallerMean.java b/libraries-data-2/src/main/java/com/baeldung/r/RCallerMean.java index 53e0ab9e31..99edb8c043 100644 --- a/libraries-data-2/src/main/java/com/baeldung/r/RCallerMean.java +++ b/libraries-data-2/src/main/java/com/baeldung/r/RCallerMean.java @@ -14,23 +14,24 @@ import com.github.rcaller.rstuff.RCode; */ public class RCallerMean { - /** - * Invokes the customMean R function passing the given values as arguments. - * - * @param values the input to the mean script - * @return the result of the R script - * @throws IOException if any error occurs - * @throws URISyntaxException if any error occurs - */ - public double mean(int[] values) throws IOException, URISyntaxException { - String fileContent = RUtils.getMeanScriptContent(); - RCode code = RCode.create(); - code.addRCode(fileContent); - code.addIntArray("input", values); - code.addRCode("result <- customMean(input)"); - RCaller caller = RCaller.create(code, RCallerOptions.create()); - caller.runAndReturnResult("result"); - return caller.getParser().getAsDoubleArray("result")[0]; - } + /** + * Invokes the customMean R function passing the given values as arguments. + * + * @param values the input to the mean script + * @return the result of the R script + * @throws IOException if any error occurs + * @throws URISyntaxException if any error occurs + */ + public double mean(int[] values) throws IOException, URISyntaxException { + String fileContent = RUtils.getMeanScriptContent(); + RCode code = RCode.create(); + code.addRCode(fileContent); + code.addIntArray("input", values); + code.addRCode("result <- customMean(input)"); + RCaller caller = RCaller.create(code, RCallerOptions.create()); + caller.runAndReturnResult("result"); + return caller.getParser() + .getAsDoubleArray("result")[0]; + } } \ No newline at end of file diff --git a/libraries-data-2/src/main/java/com/baeldung/r/RUtils.java b/libraries-data-2/src/main/java/com/baeldung/r/RUtils.java index ad16fd5602..a9393cdcc2 100644 --- a/libraries-data-2/src/main/java/com/baeldung/r/RUtils.java +++ b/libraries-data-2/src/main/java/com/baeldung/r/RUtils.java @@ -15,16 +15,19 @@ import java.util.stream.Collectors; */ public class RUtils { - /** - * Loads the script.R and returns its content as a string. - * - * @return the script.R content as a string - * @throws IOException if any error occurs - * @throws URISyntaxException if any error occurs - */ - static String getMeanScriptContent() throws IOException, URISyntaxException { - URI rScriptUri = RUtils.class.getClassLoader().getResource("script.R").toURI(); - Path inputScript = Paths.get(rScriptUri); - return Files.lines(inputScript).collect(Collectors.joining()); - } + /** + * Loads the script.R and returns its content as a string. + * + * @return the script.R content as a string + * @throws IOException if any error occurs + * @throws URISyntaxException if any error occurs + */ + static String getMeanScriptContent() throws IOException, URISyntaxException { + URI rScriptUri = RUtils.class.getClassLoader() + .getResource("script.R") + .toURI(); + Path inputScript = Paths.get(rScriptUri); + return Files.lines(inputScript) + .collect(Collectors.joining()); + } } \ No newline at end of file diff --git a/libraries-data-2/src/main/java/com/baeldung/r/RenjinMean.java b/libraries-data-2/src/main/java/com/baeldung/r/RenjinMean.java index befb7d522f..4576ec5fb4 100644 --- a/libraries-data-2/src/main/java/com/baeldung/r/RenjinMean.java +++ b/libraries-data-2/src/main/java/com/baeldung/r/RenjinMean.java @@ -15,22 +15,22 @@ import org.renjin.sexp.DoubleArrayVector; */ public class RenjinMean { - /** - * Invokes the customMean R function passing the given values as arguments. - * - * @param values the input to the mean script - * @return the result of the R script - * @throws IOException if any error occurs - * @throws URISyntaxException if any error occurs - * @throws ScriptException if any error occurs - */ - public double mean(int[] values) throws IOException, URISyntaxException, ScriptException { - RenjinScriptEngine engine = new RenjinScriptEngine(); - String meanScriptContent = RUtils.getMeanScriptContent(); - engine.put("input", values); - engine.eval(meanScriptContent); - DoubleArrayVector result = (DoubleArrayVector) engine.eval("customMean(input)"); - return result.asReal(); - } + /** + * Invokes the customMean R function passing the given values as arguments. + * + * @param values the input to the mean script + * @return the result of the R script + * @throws IOException if any error occurs + * @throws URISyntaxException if any error occurs + * @throws ScriptException if any error occurs + */ + public double mean(int[] values) throws IOException, URISyntaxException, ScriptException { + RenjinScriptEngine engine = new RenjinScriptEngine(); + String meanScriptContent = RUtils.getMeanScriptContent(); + engine.put("input", values); + engine.eval(meanScriptContent); + DoubleArrayVector result = (DoubleArrayVector) engine.eval("customMean(input)"); + return result.asReal(); + } } \ No newline at end of file diff --git a/libraries-data-2/src/main/java/com/baeldung/r/RserveMean.java b/libraries-data-2/src/main/java/com/baeldung/r/RserveMean.java index 51aaa90648..1aaa7fa847 100644 --- a/libraries-data-2/src/main/java/com/baeldung/r/RserveMean.java +++ b/libraries-data-2/src/main/java/com/baeldung/r/RserveMean.java @@ -11,19 +11,20 @@ import org.rosuda.REngine.Rserve.RConnection; */ public class RserveMean { - /** - * Connects to the Rserve istance listening on 127.0.0.1:6311 and invokes the - * customMean R function passing the given values as arguments. - * - * @param values the input to the mean script - * @return the result of the R script - * @throws REngineException if any error occurs - * @throws REXPMismatchException if any error occurs - */ - public double mean(int[] values) throws REngineException, REXPMismatchException { - RConnection c = new RConnection(); - c.assign("input", values); - return c.eval("customMean(input)").asDouble(); - } + /** + * Connects to the Rserve istance listening on 127.0.0.1:6311 and invokes the + * customMean R function passing the given values as arguments. + * + * @param values the input to the mean script + * @return the result of the R script + * @throws REngineException if any error occurs + * @throws REXPMismatchException if any error occurs + */ + public double mean(int[] values) throws REngineException, REXPMismatchException { + RConnection c = new RConnection(); + c.assign("input", values); + return c.eval("customMean(input)") + .asDouble(); + } } \ No newline at end of file diff --git a/libraries-data-2/src/test/java/com/baeldung/r/FastRMeanUnitTest.java b/libraries-data-2/src/test/java/com/baeldung/r/FastRMeanUnitTest.java index 5cf8c63a56..4e7426b75a 100644 --- a/libraries-data-2/src/test/java/com/baeldung/r/FastRMeanUnitTest.java +++ b/libraries-data-2/src/test/java/com/baeldung/r/FastRMeanUnitTest.java @@ -12,18 +12,18 @@ import org.junit.Test; @Ignore public class FastRMeanUnitTest { - /** - * Object to test. - */ - private FastRMean fastrMean = new FastRMean(); + /** + * Object to test. + */ + private FastRMean fastrMean = new FastRMean(); - /** - * Test for {@link FastRMeanUnitTest#mean(int[])}. - */ - @Test - public void givenValues_whenMean_thenCorrect() { - int[] input = { 1, 2, 3, 4, 5 }; - double result = fastrMean.mean(input); - Assert.assertEquals(3.0, result, 0.000001); - } + /** + * Test for {@link FastRMeanUnitTest#mean(int[])}. + */ + @Test + public void givenValues_whenMean_thenCorrect() { + int[] input = { 1, 2, 3, 4, 5 }; + double result = fastrMean.mean(input); + Assert.assertEquals(3.0, result, 0.000001); + } } \ No newline at end of file diff --git a/libraries-data-2/src/test/java/com/baeldung/r/RCallerMeanIntegrationTest.java b/libraries-data-2/src/test/java/com/baeldung/r/RCallerMeanIntegrationTest.java index b68f259edd..ce6b3a4332 100644 --- a/libraries-data-2/src/test/java/com/baeldung/r/RCallerMeanIntegrationTest.java +++ b/libraries-data-2/src/test/java/com/baeldung/r/RCallerMeanIntegrationTest.java @@ -17,21 +17,21 @@ import org.junit.Test; @Ignore public class RCallerMeanIntegrationTest { - /** - * Object to test. - */ - private RCallerMean rcallerMean = new RCallerMean(); + /** + * Object to test. + */ + private RCallerMean rcallerMean = new RCallerMean(); - /** - * Test for {@link RCallerMeanIntegrationTest#mean(int[])}. - * - * @throws ScriptException if an error occurs - * @throws URISyntaxException if an error occurs - */ - @Test - public void givenValues_whenMean_thenCorrect() throws IOException, URISyntaxException { - int[] input = { 1, 2, 3, 4, 5 }; - double result = rcallerMean.mean(input); - Assert.assertEquals(3.0, result, 0.000001); - } + /** + * Test for {@link RCallerMeanIntegrationTest#mean(int[])}. + * + * @throws ScriptException if an error occurs + * @throws URISyntaxException if an error occurs + */ + @Test + public void givenValues_whenMean_thenCorrect() throws IOException, URISyntaxException { + int[] input = { 1, 2, 3, 4, 5 }; + double result = rcallerMean.mean(input); + Assert.assertEquals(3.0, result, 0.000001); + } } \ No newline at end of file diff --git a/libraries-data-2/src/test/java/com/baeldung/r/RenjinMeanUnitTest.java b/libraries-data-2/src/test/java/com/baeldung/r/RenjinMeanUnitTest.java index e364d54632..f52d37d614 100644 --- a/libraries-data-2/src/test/java/com/baeldung/r/RenjinMeanUnitTest.java +++ b/libraries-data-2/src/test/java/com/baeldung/r/RenjinMeanUnitTest.java @@ -16,22 +16,22 @@ import org.junit.Assert; */ public class RenjinMeanUnitTest { - /** - * Object to test. - */ - private RenjinMean renjinMean = new RenjinMean(); + /** + * Object to test. + */ + private RenjinMean renjinMean = new RenjinMean(); - /** - * Test for {@link RenjinMeanUnitTest#mean(int[])}. - * - * @throws ScriptException if an error occurs - * @throws URISyntaxException if an error occurs - * @throws IOException if an error occurs - */ - @Test - public void givenValues_whenMean_thenCorrect() throws IOException, URISyntaxException, ScriptException { - int[] input = { 1, 2, 3, 4, 5 }; - double result = renjinMean.mean(input); - Assert.assertEquals(3.0, result, 0.000001); - } + /** + * Test for {@link RenjinMeanUnitTest#mean(int[])}. + * + * @throws ScriptException if an error occurs + * @throws URISyntaxException if an error occurs + * @throws IOException if an error occurs + */ + @Test + public void givenValues_whenMean_thenCorrect() throws IOException, URISyntaxException, ScriptException { + int[] input = { 1, 2, 3, 4, 5 }; + double result = renjinMean.mean(input); + Assert.assertEquals(3.0, result, 0.000001); + } } \ No newline at end of file diff --git a/libraries-data-2/src/test/java/com/baeldung/r/RserveMeanIntegrationTest.java b/libraries-data-2/src/test/java/com/baeldung/r/RserveMeanIntegrationTest.java index 95b344cb02..23d42bd8e9 100644 --- a/libraries-data-2/src/test/java/com/baeldung/r/RserveMeanIntegrationTest.java +++ b/libraries-data-2/src/test/java/com/baeldung/r/RserveMeanIntegrationTest.java @@ -14,21 +14,21 @@ import org.rosuda.REngine.REngineException; @Ignore public class RserveMeanIntegrationTest { - /** - * Object to test. - */ - private RserveMean rserveMean = new RserveMean(); + /** + * Object to test. + */ + private RserveMean rserveMean = new RserveMean(); - /** - * Test for {@link RserveMeanIntegrationTest#mean(int[])}. - * - * @throws REXPMismatchException if an error occurs - * @throws REngineException if an error occurs - */ - @Test - public void givenValues_whenMean_thenCorrect() throws REngineException, REXPMismatchException { - int[] input = { 1, 2, 3, 4, 5 }; - double result = rserveMean.mean(input); - Assert.assertEquals(3.0, result, 0.000001); - } + /** + * Test for {@link RserveMeanIntegrationTest#mean(int[])}. + * + * @throws REXPMismatchException if an error occurs + * @throws REngineException if an error occurs + */ + @Test + public void givenValues_whenMean_thenCorrect() throws REngineException, REXPMismatchException { + int[] input = { 1, 2, 3, 4, 5 }; + double result = rserveMean.mean(input); + Assert.assertEquals(3.0, result, 0.000001); + } } \ No newline at end of file