RCaller Java Premature end of file aka XML file is empty

廉价感情. 提交于 2019-12-11 13:11:00

问题


I am writing program using RCaller 2.1.1-SNAPSHOT. Problem is when I use code from external library it says Routput file is empty. Here is my code:

  Random random = new Random();


  RCaller caller = new RCaller();
  RCode code = new RCode();

  caller.setRscriptExecutable("C:/Program Files/R/R-2.14.2/bin/x64/Rscript.exe");

  double[] data = new double[100];

  for (int i = 0; i < data.length; i++) {
    data[i] = random.nextGaussian();
  }

  code.addDoubleArray("x", data);

  code.addRCode("setwd('C:/Radek/')");
  code.addRCode("wd=list.files()");
  code.addRCode("library(Biobase)");
  //code.addRCode("targets=read.AnnotatedDataFrame('targets.txt',row.names=1,as.is=TRUE)");// WHEN I USE THIS IT CRASH
  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)");

  code.addRCode(
          "my.all<-list(mean=my.mean, variance=my.var, sd=my.sd, min=my.min, max=my.max, std=my.standardized)");
  caller.setRCode(code);
  caller.runAndReturnResult("wd");

  String[] results;
  results = caller.getParser().getAsStringArray("wd");
  System.out.println("Mean is " + results[0]);

I checked this: 1. RUniversal is installed and loaded 2. library for using function is downloaded and installed. 3 When I put rCaller request from file generated by RCaller into R it works. 4. slashes in RScript path are good because I checked results with commented problematic line and it works.

Can someone help me with this?


回答1:


this is generally about the differences of the installer and the loader user of package. The latest and experimental RCaller 2.2.0 does not require Runiversal. If is your problem still current you can try it out and write here if problem still exists.You can follow the links for download at official blog page of RCaller




回答2:


In the part of your code

code.addRCode(
      "my.all<-list(mean=my.mean, variance=my.var, sd=my.sd, min=my.min, max=my.max, std=my.standardized)");
caller.setRCode(code);

String[] results;
results = caller.getParser().getAsStringArray("wd");

change the part

caller.runAndReturnResult("wd");

to

caller.runAndReturnResult("my.all");

so your prepared list is returned from R to Java. Then use the code

double[] results;
results = caller.getParser().getAsDoubleArray("mean");

and, finally print your returned mean to screen

System.out.println("Mean is " + results[0]);


来源:https://stackoverflow.com/questions/16818735/rcaller-java-premature-end-of-file-aka-xml-file-is-empty

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!