因此,我尝试使用RCaller执行以下操作(在伪代码中):

x=simarray(..) // in java

'send x to R'

run y = rlogcon(40000, sort(x)) //in R, rlogcon is part of rlogcondens and the function
produces a numeric vector of length 40000

//send y to java and hold it in a double matrix.


这就是我的主要功能(我改编了我发现的示例之一,但是我肯定犯了一个错误/误解了一些。我在机器上测试了示例,并且按预期工作了):

    RCaller caller = new RCaller();
    RCode code = new RCode();
    double[] x = a.simarraysimbeta(5, 1, 100, 30, 40); //some method
    savesample("simarraysimbeta.txt",x); //testing, it works.

    caller.setRscriptExecutable("C:\\Program Files\\R\\R-3.1.0\\bin\\Rscript.exe");

    code.addDoubleArray("X", x);
    code.addRCode("library(logcondens)"); //rlogcon is part of logcondens library
    code.addRCode("ols <- rlogcon(40,000, sort(X))");


    caller.setRCode(code);

    caller.runAndReturnResult("ols");

    double[] residuals = caller.getParser().getAsDoubleArray("X_star");


以及以下相关事件导入:

import rcaller.RCaller;
import rcaller.RCode;
import rcaller;


我收到以下错误:

 Exception in thread "main" rcaller.exception.ParseException: Can not
 handle R results due to : rcaller.exception.ParseException: Can not
 parse output: The generated file
 C:\Users\jo\AppData\Local\Temp\Routput8804446513483695061 is empty


这是输出:

    concat<-function(to,...){
        to<-paste(to,toString(...),sep="");
        return(to);
    }

    cleanNames<-function(names){
        cleanNames<-paste(unlist(strsplit(names,"\\.")),collapse="_");
   Exception in thread "main" rcaller.exception.ParseException: Can not
   handle R results due to : rcaller.exception.ParseException: Can not
   parse output: The generated file
   C:\Users\jo\AppData\Local\Temp\Routput8804446513483695061 is empty
        cleanNames<-paste(unlist(strsplit(cleanNames,"<")),collapse="");
        at rcaller.RCaller.runAndReturnResult(RCaller.java:409)
        cleanNames<-paste(unlist(strsplit(cleanNames,">")),collapse="");
        at dissertation.Dissertation.main(Dissertation.java:709)
        cleanNames<-paste(unlist(strsplit(cleanNames," ")),collapse="");
        cleanNames<-paste(unlist(strsplit(cleanNames,"\\(")),collapse="");
        cleanNames<-paste(unlist(strsplit(cleanNames,"\\)")),collapse="");
        cleanNames<-paste(unlist(strsplit(cleanNames,"\\[")),collapse="");
        cleanNames<-paste(unlist(strsplit(cleanNames,"\\]")),collapse="");
        cleanNames<-paste(unlist(strsplit(cleanNames,"\\*")),collapse="");
        return(cleanNames);
    }

    makevectorxml<-function(code,objt,name=""){
            xmlcode<-code;
        if(name==""){
                varname<-cleanNames(deparse(substitute(obj)));
            }else{
                    varname<-name;
            }
        obj<-objt;
            n <- 0; m <- 0
            mydim <- dim(obj)
            if(!is.null(mydim)){
                n <- mydim[1]; m <- mydim[2];
            }else{
                n <- length(obj); m <- 1;
            }
        if(is.matrix(obj)) obj<-as.vector(obj);
        if(typeof(obj)=="language") obj<-toString(obj);
        if(typeof(obj)=="logical") obj<-as.character(obj);
                    if(is.vector(obj) && is.numeric(obj)){
                            xmlcode<-paste(xmlcode,"<variable name=\"",varname,"\" type=\"numeric\" n=\"", n, "\"  m=\"", m, "\">",sep="");
                for (i in obj){
                                xmlcode<-paste(xmlcode,"<v>",sep="");
                    xmlcode<-paste(xmlcode,toString(i),sep="");
                                xmlcode<-paste(xmlcode,"</v>",sep="");
                }
                            xmlcode<-paste(xmlcode,"</variable>\n",sep="");
                    }
                    if(is.vector(obj) && is.character(obj)){
                            xmlcode<-paste(xmlcode,"<variable name=\"",varname,"\" type=\"character\">\n",sep="");
                            for (i in obj){
                                    xmlcode<-paste(xmlcode,"<v>",sep="");
                                    xmlcode<-paste(xmlcode,toString(i),sep="");
                                    xmlcode<-paste(xmlcode,"</v>",sep="");
                            }
                xmlcode<-paste(xmlcode,"</variable>\n");
                    }
            return(xmlcode);
    }


    makexml<-function(obj,name=""){
        xmlcode<-"<?xml version=\"1.0\"?>\n";
        xmlcode<-concat(xmlcode,"<root>\n");
        if(!is.list(obj)){
                    xmlcode<-makevectorxml(xmlcode,obj,name);
            }
            else{
                    objnames<-names(obj);
                    for (i in 1:length(obj)){
                xmlcode<-makevectorxml(xmlcode,obj[[i]],cleanNames(objnames[[i]]));
            }
            }
        xmlcode<-concat(xmlcode,"</root>\n");
        return(xmlcode);
    }



    X<-c(##100 doubles);
    library(logcondens)
    ols <- rlogcon(40,000, sort(X))
    cat(makexml(obj=ols, name="ols"), file="C:/Users/##/AppData/Local/Temp/Routput8804446513483695061")

最佳答案

40,000应该是您代码中的40000。

07-24 09:45
查看更多