简体   繁体   中英

R, Java, RCaller

So i'm trying to use RCaller to do the following (in psuedo-code):

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.

this is what i've got in my main function (i've adapted one of the examples I found, but I must have made a mistake/misunderstood something. I tested the example on my machine, and it worked as expected):

    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"); 

and the following relevent imports:

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

I get the following error:

 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

here is the output:

    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 shold是40000。

There are two ways in which a file remains empty:

  • there is an error in R code
  • the result is NULL or NA

I recommend you to use tryCach in R

ols <- tryCatch({
   rlogcon(40,000, sort(X))
}, error = function(e) {
   -1
})

And at the end if ols equlas -1 , you'll know that there was an error in R

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM