Domanda

I want to load a csv file into R from Java using JRI. For some reason when I try and do this, the program terminates and returns no error or result. Here is my Code:

REXP x;
x = re.eval("data<-read.csv(file='C:\\data.csv', head=TRUE, sep=',')");
x = re.eval("data<-data$VALUE");
System.out.println(x);

As I said, nothing happens, the program just terminates. I know there is a connection and the program is terminating due to loading the csv file, as the following code does work:

REXP x;
re.eval("x<-c(10,20,30,50)");
x = re.eval("x<-x+15");
double[] test = x.asDoubleArray();
System.out.println(test[0]);
re.end();

With the result being 25! just for additional information, I have set the environment variables, PATH, R_INCLUDE_DR, R_SHARE_DR, R_DOC_DR.

Thanks in advance,

Aran

È stato utile?

Soluzione

As Carl mentioned, he did touch on the problem in another post. The problem is "\" is the escape character in both R and Java (for windows), so when passing a command from java to R you need multiple backslashes, not the normal two.

Altri suggerimenti

In R studio, i write like this:

yangzhou<-read.csv("D:\\r-workspace\\yangzhou0715.csv")

And in Java , I tried many times, when I wrote like this , it worked fine:

rc.eval("yangzhou<-read.csv('D:\\\\\\\\r-workspace\\\\\\\\yangzhou0715.csv')");

NOTICE THAT: there are 8 backslashes

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top