【发布时间】:2016-01-05 07:08:43
【问题描述】:
我有一个 Rscript myrscript.R ,它的代码如下。
if(exists("e")==FALSE)
{
e = new.env()
}
myfun1<-function(file_path)
{
mydata<-read.csv(file_path)
e$mydata1 =mydata
return("firstfunction")
}
myfun2<-function()
{
e$mydata1
names_mydata<-colnames(e$mydata1)
nm<-names_mydata[1]
return(nm)
}
我正在使用以下代码从 java 调用此脚本。
public class mymainclass {
public static void main(String[] args) throws RserveException, REXPMismatchException {
String file_path1="/home/jayshree/test_data.csv";
mymainclass mm=new mymainclass();
String s = mm.myfun_2(file_path1);
String l3 = mm.myfun_3();
System.out.println(s);
System.out.println(l3);
}
public static String myfun_2(String file_path) throws RserveException, REXPMismatchException
{
RConnection c = new RConnection();
c.eval("source(\"/home/jayshree/myrscript.R\")");
c.assign("file_path",file_path);
String a = c.eval("myfun1(file_path)").asString();
return(a);
}
public static String myfun_3() throws RserveException, REXPMismatchException
{
RConnection c = new RConnection();
c.eval("source(\"/home/jayshree/myrscript.R\")");
String b = c.eval("myfun2()").asString;
return(b);
}
}
从 java 运行时。它抛出不匹配错误。错误来了,因为在调用 R 脚本的第二个函数时。全局变量 e$mydata1 的值没有被初始化,并且为空。但它不应该。我在 R 控制台中运行了脚本文件的代码。它运行良好。但是从java调用时,为什么全局变量不起作用。有没有其他解决方案。
【问题讨论】:
-
myfun2接受参数file-path,这不是一个有效的 R 变量名。 -
抱歉输入错误