关于java中调用Rserve包的eval函数中的奇葩bug

由于任务的需要,最近一直在使用Rserve包将java与R这两大利器结合起来使用。

在利用R文件进行项目开发时,往往要设计自己的函数库。


本人设计的其中一个R函数如下:

#方法6:主题数的计算
## 由于java的原因不能执行
topicnum.fun <- function(str){
  if(str == "现象"){
    print("计算开始")
    dtm <- kefu.phenomenon.dtm
    #首先选择最佳的topic number
    startTime=Sys.time()
    best.model <- lapply(seq(10,100, by=10), function(k){LDA(dtm, k)})
    best.model.logLik <- as.data.frame(as.matrix(lapply(best.model, logLik)))
    
    best.model.logLik.df <- data.frame(topics=seq(10,100, by=10), LL=as.numeric(as.matrix(best.model.logLik)))
    
    topicnum<-best.model.logLik.df[which.max(best.model.logLik.df$LL),]
    # 记录下当前程序运行的时间点
    print(Sys.time() - startTime)
    print(topicnum)
    topicnum
    
  }else if(str == "原因"){
    dtm <- kefu.reason.dtm
    #首先选择最佳的topic number
    startTime=Sys.time()
    best.model <- lapply(seq(10,100, by=10), function(k){LDA(dtm, k)})
    best.model.logLik <- as.data.frame(as.matrix(lapply(best.model, logLik)))
    
    best.model.logLik.df <- data.frame(topics=seq(10,100, by=10), LL=as.numeric(as.matrix(best.model.logLik)))
    
    topicnum<-best.model.logLik.df[which.max(best.model.logLik.df$LL),]
    # 记录下当前程序运行的时间点
    print(Sys.time() - startTime)
    print(topicnum)
    topicnum
    
  }
 
}

其中,在java工程中的一个类执行以下程序:

package com.buptshl.r;

import org.rosuda.REngine.Rserve.RConnection;
import org.rosuda.REngine.Rserve.RserveException;

public class TestR08 {

    public static void main(String[] args) {

        RConnection rc = null;
        try {
            rc = new RConnection();
System.out.println("开始运算");
//加载运算过程中需要的函数库
            rc.eval("source(\"E:/tomcat/apache-tomcat-7.0.63/webapps/textmining/text analysis/rsource/test.R\")");
            System.out.println("结束运算");

        } catch (RserveException e) {
            e.printStackTrace();
        }finally{
            rc.close();
        }
    }

}

程序执行时,报错:

经过逐句调试之后,将R程序中if语句的print("计算开始")注释或者删除掉之后。

#方法6:主题数的计算
## 由于java的原因不能执行
topicnum.fun <- function(str){
  if(str == "现象"){
#     print("计算开始")
    dtm <- kefu.phenomenon.dtm
    #首先选择最佳的topic number
    startTime=Sys.time()
    best.model <- lapply(seq(10,100, by=10), function(k){LDA(dtm, k)})
    best.model.logLik <- as.data.frame(as.matrix(lapply(best.model, logLik)))
    
    best.model.logLik.df <- data.frame(topics=seq(10,100, by=10), LL=as.numeric(as.matrix(best.model.logLik)))
    
    topicnum<-best.model.logLik.df[which.max(best.model.logLik.df$LL),]
    # 记录下当前程序运行的时间点
    print(Sys.time() - startTime)
    print(topicnum)
    topicnum
    
  }else if(str == "原因"){
    dtm <- kefu.reason.dtm
    #首先选择最佳的topic number
    startTime=Sys.time()
    best.model <- lapply(seq(10,100, by=10), function(k){LDA(dtm, k)})
    best.model.logLik <- as.data.frame(as.matrix(lapply(best.model, logLik)))
    
    best.model.logLik.df <- data.frame(topics=seq(10,100, by=10), LL=as.numeric(as.matrix(best.model.logLik)))
    
    topicnum<-best.model.logLik.df[which.max(best.model.logLik.df$LL),]
    # 记录下当前程序运行的时间点
    print(Sys.time() - startTime)
    print(topicnum)
    topicnum
    
  }
 
}
比较前后的程序差异,可以看出只有print语句的不同。奇怪的是,执行成功:

除此之外,在语句中尽量不要使用stop()或者cat语句,这些都会导致在利用java调用eval函数时报错。这只是个人实践的记录。

欢迎留言

Java可以通过Rserve这个R语言的远程调用服务来调用R语言的函数。在Java,我们需要使用Rserve连接到R语言的服务器,然后使用RConnection类创建一个与R语言的连接,最后使用RConnection类的assign()方法来调用R语言的赋值函数。 具体的操作步骤如下: 1. 在R语言,使用Rserve启动R语言服务器,可以使用以下命令启动: ```R library(Rserve) Rserve() ``` 2. 在Java,使用Rserve连接到R语言的服务器,可以使用以下代码: ```java import org.rosuda.REngine.Rserve.RConnection; RConnection connection = new RConnection("localhost", 6311); // 连接到Rserve服务器 ``` 3. 调用R语言的赋值函数,例如: ```java connection.assign("x", new double[]{1.0, 2.0, 3.0}); // 将向量[1.0, 2.0, 3.0]赋值给变量x ``` 在这个例子,我们使用RConnection类的assign()方法将一个Java数组赋值给了R语言的变量x,这相当于在R语言执行了以下命令: ```R x <- c(1.0, 2.0, 3.0) ``` 如果您想要调用其他的R语言函数,也可以使用RConnection类的eval()方法来执行R代码。例如,如果您想要调用一个名为myfunction的R函数,可以使用以下代码: ```java connection.eval("myfunction(x)"); // 调用名为myfunction的R函数,并传入变量x作为参数 ``` 其,"myfunction(x)"是一个字符串,表示要在R语言执行的代码。如果myfunction函数返回了一个结果,可以使用RConnection类的get()方法来获取这个结果。例如: ```java double[] result = connection.eval("myfunction(x)").asDoubles(); // 获取myfunction函数的返回结果,并将其转换为Java的double数组 ``` 以上就是Java调用R语言赋值函数的基本方法。需要注意的是,在使用Rserve时需要确保R语言和Rserve都已经正确安装,并且已经启动了Rserve服务器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值