Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2015
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. import org.rosuda.JRI.Rengine;
  2. public class Temp {
  3.  
  4. public static void main(String a[]) {
  5. // Create an R vector in the form of a string.
  6. String javaVector = "c(1,2,3,4,5)";
  7.  
  8. // Start Rengine.
  9. Rengine engine = new Rengine(new String[] { "--no-save" }, false, null);
  10.  
  11. // The vector that was created in JAVA context is stored in 'rVector' which is a variable in R context.
  12. engine.eval("rVector=" + javaVector);
  13.  
  14. //Calculate MEAN of vector using R syntax.
  15. engine.eval("meanVal=mean(rVector)");
  16.  
  17. //Retrieve MEAN value
  18. double mean = engine.eval("meanVal").asDouble();
  19.  
  20. //Print output values
  21. System.out.println("Mean of given vector is=" + mean);
  22.  
  23. }
  24. }
  25.  
  26. import org.rosuda.REngine.REXPMismatchException;
  27. import org.rosuda.REngine.Rserve.RConnection;
  28. import org.rosuda.REngine.Rserve.RserveException;
  29. public class Temp {
  30.  
  31. public static void main(String a[]) {
  32. RConnection connection = null;
  33. System.out.println("line 10");
  34. try {
  35. // Create a connection to Rserve instance running on default port 6311
  36.  
  37. System.out.println("line 15");
  38. connection = new RConnection();
  39. System.out.println("line 17");
  40. //Note four slashes (\\) in the path
  41. connection.eval("source('D:\\RExamples\\helloworld.R')");
  42. System.out.println("line 19");
  43. int num1=10;
  44. int num2=20;
  45. int sum=connection.eval("myAdd("+num1+","+num2+")").asInteger();
  46. System.out.println("The sum is=" + sum);
  47. } catch (RserveException e) {
  48. e.printStackTrace();
  49. } catch (REXPMismatchException e) {
  50. e.printStackTrace();
  51. }
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement