R java

来源:互联网 发布:linux开机挂载分区 编辑:程序博客网 时间:2024/05/18 02:57

 Rcaller 2.0

 

 java 调用R

    sudo apt-get install r-base   此时/usr/bin/Rscript生效

    下载http://code.google.com/p/rcaller/   jar  加入到classpath

    打开shell ,输入R,运行R

    输入:install.packages("Runiversal")

    选北京失败,选加拿大。

    安装完毕

    执行java代码

    try {

                  /*
                   * Creating Java's random number generator
                   */
                  Random random = new Random();

                  /*
                   * Creating RCaller
                   */
                  RCaller caller = new RCaller();
                  RCode code = new RCode();
                  /*
                   * Full path of the Rscript. Rscript is an executable file shipped with R.
                   * It is something like C:\\Program File\\R\\bin.... in Windows
                   */
                  caller.setRscriptExecutable("/usr/bin/Rscript");

                  /*
                   *  We are creating a random data from a normal distribution
                   * with zero mean and unit variance with size of 100
                   */
                  double[] data = new double[100];
                  
                  for (int i = 0; i < data.length; i++) {
                    data[i] = random.nextGaussian();
                  }

                  /*
                   * We are transferring the double array to R
                   */
                  code.addDoubleArray("x", data);

                  /*
                   * Adding R Code
                   */
                  code.addRCode("my.mean<-mean(x)");
                  code.addRCode("my.var<-var(x)");
                  code.addRCode("my.sd<-sd(x)");
                  code.addRCode("my.min<-min(x)");
                  code.addRCode("my.max<-max(x)");
                  code.addRCode("my.standardized<-scale(x)");

                  /*
                   * Combining all of them in a single list() object
                   */
                  code.addRCode(
                          "my.all<-list(mean=my.mean, variance=my.var, sd=my.sd, min=my.min, max=my.max, std=my.standardized)");

                  /*
                   * We want to handle the list 'my.all'
                   */
                  caller.setRCode(code);
                  caller.runAndReturnResult("my.all");
                  
                  double[] results;

                  /*
                   * Retrieving the 'mean' element of list 'my.all'
                   */
                  results = caller.getParser().getAsDoubleArray("mean");
                  System.out.println("Mean is " + results[0]);

                  /*
                   * Retrieving the 'variance' element of list 'my.all'
                   */
                  results = caller.getParser().getAsDoubleArray("variance");
                  System.out.println("Variance is " + results[0]);

                  /*
                   * Retrieving the 'sd' element of list 'my.all'
                   */
                  results = caller.getParser().getAsDoubleArray("sd");
                  System.out.println("Standard deviation is " + results[0]);

                  /*
                   * Retrieving the 'min' element of list 'my.all'
                   */
                  results = caller.getParser().getAsDoubleArray("min");
                  System.out.println("Minimum is " + results[0]);

                  /*
                   * Retrieving the 'max' element of list 'my.all'
                   */
                  results = caller.getParser().getAsDoubleArray("max");
                  System.out.println("Maximum is " + results[0]);

                  /*
                   * Retrieving the 'std' element of list 'my.all'
                   */
                  results = caller.getParser().getAsDoubleArray("std");

                  /*
                   * Now we are retrieving the standardized form of vector x
                   */
                  System.out.println("Standardized x is ");
                  
                  for (int i = 0; i < results.length; i++) {
                    System.out.print(results[i] + ", ");
                  }
                } catch (Exception e) {
                  System.out.println(e.toString());
                }

    http://code.google.com/p/rcaller/



    2.4 JGR:java GUI for R——http://www.rforge.net/JGR/ >>>以Java 为主的同学们, 作图可以考虑 JGR。这个还不错!

    JGR (speak ‘Jaguar’) is a universal and unified Graphical User Interface for R (it actually abbreviates Java Gui for R).

    原创粉丝点击