R language - test report - the use of the R language and Java scripts call each other


First, the training content

  1. Using the R language program call Java project, this experiment include the .java java files compiled and executed using the output of the R language.
  2. Call the R language program in Java. In this study, write Java programs through the eclipse way, call the R language script file transfer value calculation, and returns the result of execution to the Java program.

Second, the experimental subjects goal

  1. Required can be performed in a simple R x64 3.6.1 program workspace switching function, as well as master basic shell.exec () and a system () function is applied.
  2. Master download and method for packet loaded in the R language, as well as methods to start Rserve services.
  3. Master the basic principles and methods to achieve the R language and Java call each other.

Third, experimental platform

  1, system: Windows 10

         Intel(R) Core(TM)i7-7500U CPU @ 2.70GHz 2.90 GHz

    RAM 8.00GB

  2. Tools:

         R x64 3.6.1  

    notepad.exe

    eclipse

    word 2016

Fourth, the implementation steps

(1), R language program call Java project

  1, switch workspaces R language.

    First in the C: \ directory, create a new folder for the workspace, and then open R x64 3.6.1, enter the command: getwd (), to get the current R language program workspace. The input command: setwd ( "C: / workspace"), as the working directory workspace R language, coupled to verify successful handover.

FIG workspace switch 4-1


  2, write Java programs.

    In R x64 3.6.1 program, enter the command: shell.exec ( "notepad"), open Notepad, write the corresponding Java class files, and save it to the C: \ workspace directory named demo.java.

FIG notepad open language 4-2 R

FIG. 4-3 Java code sample


  3, using the R language to compile Java source files.

    First of all, here to demo.java file, although only a .java file formats, but also can be understood as a simple Java project. In R x64 3.6.1 program, enter the command: system ( "javac demo.java"), compiled C: \ demo.java workspace file in the directory.

Figure 4-4 success compile Java files


        When you see [1] 0 in the returned results, it said it had successfully been compiled. As shown in Figure 4-5, in the C: \ workspace directory, has successfully generated demo.class byte file, has once again proven that no errors during compilation.

Figure 4-5 compile the results generated files


  4, using the R language runtime has been compiled demo.class file. In R x64 3.6.1 program, enter the command: system ( "java demo"), performed just compiled demo.class byte file.

Figure 4-6 successful execution


  Obviously, appear in the returned results: "With a successful call to the Java programming language R [1] 0!" Message, which indicates the success of the R language called Java programs compiled project. Here though is a simple, single .java file to do the experiment, but even more Java project files, this method can be used.

(2), the Java programming language to make calls

  1, the installation package Rserve. In R x64 3.6.1 program, enter the command: install.packages ( "Rserve"), download 'Rserve' package. In the pop-up mirrors options, select the server to Shanghai.

Figure 4-7 Rserve download success


  2、加载Rserve包。输入命令:library(Rserve)  ,加载Rserve程序包。

  3、启动服务。输入命令:Rserve()   ,开始启动Rserve服务。

图4-8 Rserve服务开启成功


  4、编写调用R语言的Java程序。打开eclipse,新建项目为demo。同时,在demo项目工程名称上右击,选择“Build Path”-“Configure Build Path..”。在Libraries

    选项卡中,单击Add External JARs.. ,加载2、3步骤中下载的REngine.jarRserve.jar文件

图4-9成功加载Rserve包


  5、  编写demo类文件。具体示例代码如下:

package demo;

import org.rosuda.REngine.Rserve.RConnection;

import org.rosuda.REngine.Rserve.RserveException;

import org.rosuda.REngine.REXPMismatchException;;

public class demo {

        public static void main(String[] args) throws REXPMismatchException {

                 RConnection connection = null;

                 System.out.println("平均值");

                 try {

                         connection = new RConnection();

                         String vetor = "c(1,2,3,4)";

                         connection.eval("meanVal<-mean(" + vetor +")");

                         double mean = connection.eval("meanVal").asDouble();

                         System.out.println("the mean of given vector is=" + mean);

                 } catch (RserveException e) {

                         e.printStackTrace();

                 }

                 System.out.println("执行脚本");

                 try {

                         connection.eval("source('c:/ workspace/myAdd.r')");

                         int num1 = 20;

                         int num2 = 10;

                         int sum = connection.eval("myAdd(" + num1 + "," + num2 + ")").asInteger();

                         System.out.println("the sum=" + sum);

                 } catch (RserveException e) {

                         System.out.println(e);

                         e.printStackTrace();

                 }

                 connection.close();

        }

}



  6、  在c:\workspace目录下新建myAdd.r脚本,脚本内容如图所示:

图4-10 myAdd.r脚本


  7、  在eclipse中运行该工程的主类文件,运行结果如图4-11所示:

图4-11 执行成功


         明显,在返回的结果中,有以下信息:“the mean of given vector is=2.5

    执行脚本 the sum=30 ”,这正是Java程序调用R语言执行后,返回的结果。

五、 实验成果

(1)、R语言调用Java项目程序

     在实验步骤中,利用R对编译的demo.class字节文件进行执行时,成功返回如图5-1所示的内容,即“利用R语言成功调用Java程序!  [1] 0”,表明利用R语言成功运行了Java项目程序。同时,隐含性地表明R语言可以调用Java语言,对.java文件进行编译和执行。

图5-1  R语言成功调用Java


(2)、Java中调用R语言程序

    当在eclipse中运行调用R脚本的主类时,在执行反馈信息中,返回了:“the mean of given vector is=2.5 执行脚本 the sum=30 ”。这计算结果,需通过R语言进行,也就是说Java也成功调用了R语言,即调用R语言脚本进行运算。如图5-2所示:

图5-2 Java调用R语言程序成功


六、 实训总结

本实验的经验收获和总结,可分点总结如下:

  1. 在利用Java调用R语言脚本时,需要加载REngine.jar和Rserve.jar两个jar包。而这两个jar包来源于,在中R x64 3.6.1使用install.packages("Rserve") 的方式进行下载Rserve。
  2. 在服务端计算机安装R之后可以直接使用install.packages(“Rserve”)进行安装。需要使用时在R控制台下加载该包,然后输入命令Rserve(),开启服务器就可以供客户端调用。
  3. 在实现R语言调用java的过程中,也可以使用rjava包,其是一个R语言和Java语言的通信接口,通过底层JNI实现调用,允许R直接调用java对象的方法。而本实验,利用简单的函数system()来实现在R语言直接调用Java程序。
  4. 在R语言调用java的时候,也可以进行传值类型的调用。即:Main函数参数String[] args,它是一个字符串数组,用来接收从命令行输入的参数。

参数之间用空格隔开,java解释器会自动处理,把用空格隔开的每一个参数通过args数组传递给main方法。参数下标从0开始,args[0]就代表1第一个参数。当然输入参数之间可以有多个空格,中间的多个空格会被忽略掉。

Guess you like

Origin www.cnblogs.com/Raodi/p/11932875.html